Java Coding Program

SQL INTERVIEW QUESTION PART-2

Java Coding Program

Below is the list of the most frequently asked Java Coding Program in the interview.

 

1- Write a java program to find out the missing number in the array.

In order to find out the missing number, we will apply simple logic, At first, we will sum up the given array then we will sum up the actual array, substracting both will provide the missing number.

package javaprograns;
public class Missing_Number_In_Array {
public static void main(String[] args) {
int a[]= {1,2,3,4,6};
int sum=0;
for(int i=0; i<a.lengthi++)
{
sum=sum+a[i];
}
int sum1=0;
for(int j=1;j<=6;j++)
{
sum1=sum1+j;
}
System.out.println("Missing Numner is: "+(sum1-sum));
}
}

 

2. Find out the largest and the smallest number from the given integer array.

In order to find out the largest as well as the smallest number from a given integer array, we will apply a small logic. We will assume the smallest and the largest number to be the number at the initial position of the array and then we will compare the given number with the rest of the number.

package javaprograns;
public class Largest_Smallest_in_Array {
public static void main(String[] args) {
int number[]= {10,0,33,45,6};
int largest=number[0];
int smallestnumber[0];
for(int i=1; i<number.lengthi++)
{
if(number[i]> largest)
{
largest=number[i];
}
else if (number[i]< smallest)
{
smallest=number[i];
}
}
System.out.println("Largest Number is: "largest);
System.out.println("Smallest Number is: "smallest);
}
}

 

3. Can we overload a main() method in Java?

This is the most famous interview question while you are in a tech round. Can we overload a main() method in Java? The answer is YES.

package javaprograns;
public class main_method_overload {
public static void main(String[] args) {
System.out.println("I an the main method A");
main("tact.ai");
main(1);
}
public static void main(String S)
{
System.out.println("I am the main method 2");
}
public static void main(int a)
{
System.out.println("I am main the method 3");
}
}

 

4. Explain Abstraction

Abstraction means hiding implementation and logic. We can create a class as an abstract class to achieve abstraction. There should be at least one abstract method in this class. Child class will extend this abstract class and will override the abstract method. We can not achieve 100% abstraction here since the abstract class can contain normal methods. We cannot create the object of the abstract class. We can define a static, final and non-static variable inside an abstract class.

package Abstraction;
public abstract class Bank {
int a;
static int b;
final int c= 100;
public abstract void loan();
public void interest()
{
System.out.println("Interest from abstract class");
}
}

 

5. Explain Interface

To achieve 100% abstraction we use interface. In the interface, we can only have an abstract method. Child class will implement the interface and will override the methods. We cannot create the object of the interface. We can only define the final and static variable inside the interface.

package Abstraction;
public interface  Car 
{
int wheels= 4;
int petrol= 8;
public void start();
public void stop();
public void refule();
}

Check Java Interview Questions

Wikipedia Java

Leave a Comment

Your email address will not be published.