41)Can an application have multiple classes having main method?
A)Yes. While starting the application we mention the class name to be run. The JVM will look for the main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.
42)When is static variable loaded? Is it at compile time or runtime? When exactly a static block is loaded in Java?
A)Static variable are loaded when classloader brings the class to the JVM. It is not necessary that an object has to be created. Static variables will be allocated memory space when they have been loaded. The code in a static block is loaded/executed only once i.e. when the class is first initialized. A class can have any number of static blocks. Static block is not member of a class, they do not have a return statement and they cannot be called directly. Cannot contain this or super. They are primarily used to initialize static fields.
43)Can I have multiple main methods in the same class?
A)We can have multiple overloaded main methods but there can be only one main method with the following signature :
public static void main(String[] args) {}
No the program fails to compile. The compiler says that the main method is already defined in the class.
44)Explain working of Java Virtual Machine (JVM)?
A)JVM is an abstract computing machine like any other real computing machine which first converts .java file into .class file by using Compiler (.class is nothing but byte code file.) and Interpreter reads byte codes.
45)How can I swap two variables without using a third variable?
A)Add two variables and assign the value into First variable. Subtract the Second value with the result Value. and assign to Second variable. Subtract the Result of First Variable With Result of Second Variable and Assign to First Variable. Example:
int a=5,b=10;a=a+b; b=a-b; a=a-b;
An other approach to the same question
You use an XOR swap.
for example:
int a = 5; int b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
If you think that an important java interview question is missing or some answers are wrong in the site please contribute it to rafisaleem@gmail.com
A)Yes. While starting the application we mention the class name to be run. The JVM will look for the main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.
42)When is static variable loaded? Is it at compile time or runtime? When exactly a static block is loaded in Java?
A)Static variable are loaded when classloader brings the class to the JVM. It is not necessary that an object has to be created. Static variables will be allocated memory space when they have been loaded. The code in a static block is loaded/executed only once i.e. when the class is first initialized. A class can have any number of static blocks. Static block is not member of a class, they do not have a return statement and they cannot be called directly. Cannot contain this or super. They are primarily used to initialize static fields.
43)Can I have multiple main methods in the same class?
A)We can have multiple overloaded main methods but there can be only one main method with the following signature :
public static void main(String[] args) {}
No the program fails to compile. The compiler says that the main method is already defined in the class.
44)Explain working of Java Virtual Machine (JVM)?
A)JVM is an abstract computing machine like any other real computing machine which first converts .java file into .class file by using Compiler (.class is nothing but byte code file.) and Interpreter reads byte codes.
45)How can I swap two variables without using a third variable?
A)Add two variables and assign the value into First variable. Subtract the Second value with the result Value. and assign to Second variable. Subtract the Result of First Variable With Result of Second Variable and Assign to First Variable. Example:
int a=5,b=10;a=a+b; b=a-b; a=a-b;
An other approach to the same question
You use an XOR swap.
for example:
int a = 5; int b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
If you think that an important java interview question is missing or some answers are wrong in the site please contribute it to rafisaleem@gmail.com
No comments:
Post a Comment