Which is the Best Java Web Framework ?

Java - Interview Questions and Answers on "Default Constructor"

Q1.  What is a Default Constructor ?

Ans. The no argument constructor provided by Java Compiler if no constructor is specified.

Q2.  Will Compiler creates a default no argument constructor if we specify only multi argument constructor ?

Ans. No, Compiler will create default constructor only if we don't specify any constructor.

Q3.  Difference between Class#getInstance() and new operator ?

Ans. Class.getInstance doesn't call the constructor whereas if we create an object using new operator , we need to have a matching constructor or compiler should provide a default constructor.

Q4.  Can we create an object if a Class doesn't have any constructor ( not even the default provided by constructor ) ?

Ans. Yes , using Class.getInstance.

Q5.  Does every class needs to have one non parameterized constructor ?

Ans. No. Every Class only needs to have one constructor - With parameters or without parameters. Compiler provides a default non parameterized constructor if no constructors is defined.  

Q6.  Why Java provides default constructor ?

Ans. At the beginning of an object's life, the Java virtual machine (JVM) allocates memory on the heap to accommodate the object's instance variables. When that memory is first allocated, however, the data it contains is unpredictable. If the memory were used as is, the behavior of the object would also be unpredictable. To guard against such a scenario, Java makes certain that memory is initialized, at least to predictable default values before it is used by any code.

Q7.  In a case where there are no instance variables what does the default constructor initialize?

Ans. Java expects the superclass ( Object Class ) constructor to be called while creation of any object. So super constructor is called in case there are no instance variables to initialize.   

Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus




×

Try our new Java Interview Questions Search Tool.

There are more than 1000 questions with practice tests.

It's updated daily and It's Awesome.


×

Try our new Java Practice Test tool.

There are more than 200 questions and expanding quickly. It's updated daily and It's Awesome.