In This blog we will discussed java interview questions for fresher as well as experience java developer:
1. What is Java byte code ?
Ans : When we compile our Java source code, compiler generate byte code for our source code. We can say byte code is low-level version of your program that runs on the Java virtual machine. This level of abstraction makes it easier for the developers of Java to ensure that our program runs on variety of device. Java byte code make Java language platform independent.
2. How can I initialised a double variable to infinity ?
Ans : Java has build-in constant available for this purpose: like Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY .
3. Can you compare a double to an int ?
Ans : Not without doing a type conversion, But remember that Java usually does the requisite type conversion automatically. For example, if x is an int with the value 3, then the expression (x < 4.2) is true - Java converts x to double (because 3.1 is a double literal) before performing the comparison.
4.What happens if I use a variable before initialising it to a value ?
Ans : If we use local variable before initialising Java will report a compile-time error, because there is a rule for local variable that says local variable must be initialised before it's use.
5. What are the value of 1/0 and 1.0 / 0.0 as Java expression ?
Ans : The first generate a runtime exception for divide by zero and second has the value Infinity.
6. What is difference between array declaration int a[] and int [] a ?
Ans: In Java both are legal and equivalent. The former is how arrays are declared in C. The later is preferred style in Java since the type of variable int [] more clearly indicates that it is an array of integers.
7. Why do array indices start with 0 instead of 1 ?
Ans : This convention originated with machine-language programming, where the address of an array element would be computed by adding the index to the address of the beginning of an array. Starting indices at 1 would entail either a waste of space at the beginning of the array or a waste of time to subtract the 1.
8. What is null ?
Ans : It is literal value that refers to no object. Invoking a method using null reference is meaningless and result in NullPointerException .
9. Can we declare a class as a final ?
Ans : Yes, we can declared a class as a final. If we declared a class as a final it can not be extended. That means you can not declare subclass of a final class. This imply that one can not override any method declared in such a class. In other words, the class behaviour cannot be changed by extending the class. Only a class whose definition is complete can be declared final.
10. Can an abstract class be declared as a final ?
Ans : No, Only a class whose definition is complete can be declared final. whereas abstract class is considered incomplete. Therefore classes can not be both final and abstract at the same time. Interface are inherently abstract, as they can only specify method that are abstract, and therefore can not be declared final.
An enum type is also implicitly final, and can not be explicitly declared with the keyword final.
Nice tutorial. Thanks you Santosh sir.
ReplyDeleteThank you very much @Rakesh Sir
ReplyDelete