Interview Questions on Java - Constructors & Methods
1. What is Constructor?
Constructor is a special kind of method which has same class name and invoked at the time of creation of Object. It is used to assign the values to the variables.
2. What is The first statement of Constructor?
this() or super().If we don't provide anything then compiler will keep super() by default.
3. Does recursion of Constructor allow in java?
4. What are the modifiers not applicable for Constructor?
abstract, final, native, static, or synchronized.
5. Can Constructor return any type?
No, Constructor can not return any type even void, but it can return empty i.e, "return;";
6. Does Constructors inherits to the sub class.
No, but it will call by using super();
7. What are the Difference between Methods and Constructors.
|1. It should have same Class Name||1. It can have any name even class name also|
|2. It should not return any value||2. It can return any type of Value.|
|3. It doesn't Inherit to the sub class||3. It Inherits to the sub class|
8. What is Loacal Variable?
The Variable which is declared inside the method.
9. What is Global Variable?
The Varibale which is declared outside method and inside the class.
10. What are the modifiers that are not applicable for the Local Variable?
static, public, private and protected.
11. What are the modifiers applicable for the Local Variable?
final and no modifier.
12. What is Static variable?
Static variables are class level variables which are common to all Objects. If one Object modify the variable it reflects in to all Objects.
13. Can we use static variable inside non-static methods?
14. Can we use non-static variable inside static methods?
No, but we can access after creating the Object and using that reference.