Java - Interview Questions and Answers for 'Default constructor' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2098 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Default constructor' - 12 question(s) found - Order By Newest

 Q1. What will be the output of following code ?
class BuggyBread2 {       
   private static int counter = 0;     
   void BuggyBread2() {        
      counter = 5;    
   }     
   
   BuggyBread2(int x){
      counter = x;    
   }        

   public static void main(String[] args) {        
      BuggyBread2 bg = new BuggyBread2();        
      System.out.println(counter);    
   } 
}
Core Java
Admin
info@buggybread.com
Ans.  Compile time error as it won't find the constructor matching BuggyBread2(). 
Compiler won't provide default no argument constructor as programmer has already defined one constructor. 
Compiler will treat user defined BuggyBread2() as a method, as return type ( void ) has been specified for that. 

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   code   coding   tricky questions   interesting questions   default constructor   constructor

Try 3 Question(s) Test


Recently asked in Infoview Technologies.
 Q2. What is a Default Constructor ?Core Java
Anonymous
Ans. The no argument constructor provided by Java Compiler if no constructor is specified.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   constructor   default constructor     Infoview Technologies      Basic        frequent

Try 2 Question(s) Test


 Q3. Will Compiler creates a default no argument constructor if we specify only multi argument constructor ?
Anonymous
Ans. No, Compiler will create default constructor only if we don't specify any constructor.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   constructor   default constructor   yes-no      expert        rare

Try 1 Question(s) Test


 Q4. Difference between Class#getInstance() and new operator ?
Admin
info@buggybread.com
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 copiler should provide a default constructor.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   constructor   object creation   default constructor   getinstance   new operator


 Q5. Can we create an object if a Class doesn't have any constructor ( not even the default provided by constructor ) ?
Admin
info@buggybread.com
Ans. Yes , using Class.getInstance.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   constructor   default constructor   class.getinstance


 Q6. Does every class needs to have one non parameterized constructor ?Core Java
Admin
info@buggybread.com
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   constructor   default constructor   yes no      intermediate

Try 3 Question(s) Test


 Q7. Variable of the boolean type is automatically initialized as?Core Java
Admin
info@buggybread.com
Ans. The default value of the boolean type is false.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   data type   boolean   initialization   default object construction   default constructor   default value


 Q8. Why Java provides default constructor ?Core Java
Admin
info@buggybread.com
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   constructor   oops   default constructor   jvm      expert

Try 2 Question(s) Test


 Q9. In a case where there are no instance variables what does the default constructor initialize?Core Java
Admin
info@buggybread.com
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   constructor   object creation   default constructor   super

Try 1 Question(s) Test



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. Is it mandatory to have a default constructor ?Core Java2017-01-11 15:54:39

Ans. No.

Default constructor is provided by compiler if no constructor is provided ( argument or no argument ) by us.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     constructor  default constructor     EMC

Try 1 Question(s) Test


 Q11. Why Java doesn't provide a default constructor if we define an argument constructor ?Core Java2017-03-10 09:58:43

Ans. Java provides a default constructor ( no argument ) if no constructor is provided by the programmer. Java makes a check during pre compilation if there is any constructor provided by the programmer. If any constructor is provided, Java understands that programmer has provided all the mechanism for initialization and construction of the object and may not even intend to create objects by calling a no argument constructor. For example - One may like to have objects created with self defined state otherwise restrict its creation.

We make the constructor private if we want to make a class singleton as we would like to restrict creation of new objects using new operator and default constructor. Similar could be the situation wherein we would like to construct objects in a particular manner only.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     default constructor      expert


 Q12. What will be the output of following code ?

public class BuggyBread {
   
   private int x;
   private int y;
   
   BuggyBread(int x,int y){};
   
   public static void main(String[] args){
      BuggyBread buggybread = new BuggyBread();
      System.out.println(buggybread.x);
   }
}
Core Java
a. 0
b. null
c. compilation error due to uninitialized element
d. compilation error due to constructor

Ans.d. compilation error due to constructor



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
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: