Java Interview Question And Answer | Java Interview Question And Answer For Fresher | Core Java Interview Question And Answare | Set 05

1.What is the difference between constructor and method?
 Ans: Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.

2.What is the difference between an argument and a parameter?
 Ans: While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.

3.What are local variables?
 Ans: Local variables are those which are declared within a block of code like methods. Local variables should be initialized before accessing them.

4.What are instance variables?
 Ans: Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.

5.What is the default value of the local variable?
 Ans: The local variables are not initialized  to any default value, neither primitives nor object references. If you try to use this variables without initializing them explicitly, the java compiler will not compile the code. It will complain about the local variable not being initialized

6.What will be the initial value of an object reference which is defined as an instance variable?
 Ans: The object references are all initialized to null in java. However in order to do anything useful with this references, you must set them to a valid object , else  you will get Null pointerException everywhere you try to use such default initialized references.

7.What happens if you don’t  initialize  an instance variable of any of the primitive types in  java?
 Ans: Java by default initializes it to the default value for that primitive type. Thus an int will be initialized to 0, a Boolean will be initialized to false.

8.How to define a constant variable in Java?
Ans: The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can't be changed also.
        static final int PI = 2.14; is an example for constant.

9.What is the return type of the main() method?
 Ans: Main() method doesn't return anything hence declared void.

10.Why is the main() method declared static?
 Ans: main() method is called by the JVM even before the instantiation of the class hence it is declared as static.


Previous Next


:: Click the links below for similar type Questions and answers ::

0 comments:

Post a Comment

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes