At the heart of Java technology lies the JVM ( Java Virtual Machine)… the abstract computer on which all Java program run. JVM is the primary component of the Java system responsible for portability. Java Programs execute whitin the VM itself. The purpose of VM is to allow Java programs to compile to uniform executable format, as defined by the VM, that can run on any platform. Generally, Java is used to refer to the Java programming language, but Java also includes JVM, Java API, and Java class me. It’s very important to learn before we go indepth of Java interview questions.
Java Programs go through five phases:
1) Edit
2) Compile
3) Load
4) Verify
5) Execute
In next Section we will discuss important Java interview questions in list format:
Question 1) Why the execution of a Java program is slow?
Answer) Java is slow because of extra load it takes in housekepping.
Question 2) What is Method Overlaoding and where is it useful give with example?
Answer) Reusing the same method name, with different arguments and with the same or different return type is called Method overloading. The OOP concept, method overloading avoids the usage of several method names that perform closely related jobs( functionality). That’s is to say method overloading eliminates the need for entirely different methods that do essentially the same thing.
Example Program:
Public class OverloadDemo {
Public void area(int x) {
System.out.println(“The area of the circle is “+Math.Pi*x*x”); // first method
}
Public void area( String figure,int x) { // Second Method
If(figure.equals(“circle”))
System.out.println(“The area of the circle is “+Math.Pi*x*x”);
elseif(figure.equals(“square”))
System.out.println(“The area of the circle is “+ x*x);
else
System.out.println(“Please Pass either circle or square only as argument”);
}
Public int area( int l, int b) {
return l*b;
}
Public float area(int a, int b, int c) {
double s= (a+b+c/2;
return s*Math.sqrt((s-a)* (s-b)+ (s-c) ) );
}
Public double area(int a, int b, int c) {
double s= (a+b+c/2;
return s*Math.sqrt((s-a)* (s-b)+ (s-c) ) );
}
Publics static void main(String ars[]) {
OveloadDemo od=new OveloadDemo();
od.area(5);
od.area (“circle”,7);
od.area(“square”, 4);
od.area(“traiangle”, 10);
System.out.println(“The area of rectangle is “ +od.area(20,10)); // 3rd method
System.out.println(“The area of the triangle is “ + Math.ceil(od.area(3,4,5))); // 4th method System.out.println(“The area of the triangle is “ + Math.round(od.area(3,4,5,6,7)));// 5th method
}
}
Out Put of the Java interview questions program
The area of the circle is 78.53981633974483
The area of the circle is 153.9380400258995
The area of the circle is 16
Please Pass either circle or square only as argument
The area of the rectangle is 200
The area of the triangle is 16.0
The area of the triangle is 14
In the above Java interview questions program area() method is overloaded. Math us a class defined in the package java,lang which is imported by default by the JVM. Math class includes many static methods like round(), floor() etc. Student is advised to refer to Java documentation for more methods and for in depth knowledge of Math class.
Question 3) What is inheritance?
Answer) Inheritance is a mechanism that enables one class to inherit all of the behavior (methods) and attributes ( instance variables) of another class. A class that inherits from another class is defines a subclass and the class that gives the inheritance is called supercalss.
The above Java interview questions is frequently asked by the Java interviewer. So friends be well prepared for the interview. This is pure Java next step is to prepare for J2ee interview questions.