- On the request of many friends we added java multiple questions with answers.
- Java mcq questions with answers on constructors.
- We will add all java topics multiple questions with answers for freshers and experienced.
1.Can we define method with same name of class?
We can define a method name as class name then there will not be any no compile time error or run time errors.We can define any name as method name. But as per coding standards of java it is not recommended to use class name as method name
2.What happens if we add return type to constructor?
Compile time error
Run time Error
JVM treats that as method
Constructor does not have return type. if we add any return type then it will become a normal method. JVM treats that as method
3.We can declare constructor as private?
We can declare constructor as private.Usage: In order to restrict object creation outside of class we should define constructor as private
4.What is default accessibility modifier of a default constructor
Same as class accessibility modifier
When default constructor is created the its accessibility modifier will be assigned as same as class accessibility modifier
How are you Hello Hi
Hi Hello How are you
How are you Hi Hello
when constructor called A() will be called and first statement this(0) control will go to constructor with int argument A(int x) and in this constructor first statement is this(0,0)control will go to constructor with int argument A(int x,int y). and will execute print statement and control get back to A(int x) and completes constructor execution and will go to A().