Core Java - Interview Questions and Answers for 'Constructor' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers - Order By Newest

next 30
 Q11. What is constructor chaining and how is it achieved in Java?Core Java
Ans. A child object constructor always first needs to construct its parent. In Java it is done via an implicit call to the no-args constructor as the first statement

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

   Like         Discuss         Correct / Improve     java   constructor   constructor chaining   oops   call constructor explicitly     Asked in 3 Companies

Try 2 Question(s) Test


 Q12. Why Java provides default constructor ?Core Java
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


 Q13. Which of the following is false about Constructors ?

a. Constructor can be overloaded
b. A no argument constructor is provided by the compiler if we declare only constructors with arguments.
c. Constructors shouldn't have any return types , not even void.
d. If super is not explicitly called, still super() is intrinsically added by the compiler.
Core Java
Ans. A no argument constructor is provided by the compiler if we declare only constructors with arguments.

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

   Like         Discuss         Correct / Improve     java   oops   constructor

Try 2 Question(s) Test


 Q14. Can we instantiate the object of derived class if parent constructor is protected ?Core Java
Ans. Yes, as protected constructor is accessible in sub class.

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

   Like         Discuss         Correct / Improve     java   oops   constructor   access specifier   protected   yes-no  private constructor


 Q15. Can we call constructor explicitly ?
Ans. Yes.

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

   Like         Discuss         Correct / Improve     java   constructor   constructor chaining   oops   call constructor explicitly   yes-no

Try 1 Question(s) Test


 Q16. Does a class inherit the constructor of its super class?
Ans. No.

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

   Like         Discuss         Correct / Improve     java   oops   constructor   inheritence   constructor inheritance   yes no


 Q17. 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
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


 Q18. Does Java provides default copy constructor ?Core Java
Ans. No

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

   Like         Discuss         Correct / Improve     java   constructor   copy constructor   yes-no


Recently asked in Infoview Technologies.
 Q19. What is a Default Constructor ?Core Java
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     Asked in 1 Companies      Basic        frequent

Try 2 Question(s) Test


 Q20. 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.

 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


 Q21. Can we overload constructors ?
Ans. Yes.

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

   Like         Discuss         Correct / Improve     java   oops   constructors   overloading   yes-no   basic interview question


 Q22. What will happen if we make the constructor private ?Core Java
Ans. We can't create the objects directly by invoking new operator.

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

   Like         Discuss         Correct / Improve     java   oops   constructor   access specifier   private     Asked in 1 Companies      basic

Try 2 Question(s) Test


 Q23. 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 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


 Q24. 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.

 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


 Q25. Does every class needs to have one non parameterized constructor ?Core Java
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


 Q26. Variable of the boolean type is automatically initialized as?Core Java
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


 Q27. In a case where there are no instance variables what does the default constructor initialize?Core Java
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


 Q28. Write code for constructor overloadingCore Java
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?&category=code&searchOption&keyword=965

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

   Like         Discuss         Correct / Improve     constructor overloading  code  coding     Asked in 2 Companies      basic


 Q29. Is it advisable to set the member variables through constructors instead of setting them through setters ?
Ans. Yes , If the values to be set are known at the time of initialization and doesn't involve polymorphic behavior.If it's using Dependency Injection , then Constructor injection must be available. If it suffice the above conditions, then definitely its advisable to have them set through constructor as they eagerly load the values into the memory and save it fro multiple values assignment ( one through default constructor and then through assignment )

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

   Like         Discuss         Correct / Improve     constructor  setter vs constructor


 Q30. What will happen if we have our own constructor but only initialize some of the member elements ?
Ans. Its always advisable to initialize all the member elements in case we don't use the compiler's default constructor as leftover member elements will have garbage values and may reflect and unstable state for the object.

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

   Like         Discuss         Correct / Improve     constructor


 Q31. Does Constructor returns the reference of the newly created object ?
Ans. No, New operator actually returns the reference of newly created object. Constructor's purpose is to initialize the default state of the object by setting the initial values of the elements and hence returns nothing.

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

   Like         Discuss         Correct / Improve     Constructor


 Q32. Why doesn't Java even allow void return types for Constructors ?
Ans. Because having a void return type with the Constructor name will make it as a method name. As methods with the Class names are perfectly legit in Java, It needs some way to recognize the constructor and having none return type helps identify that.

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

   Like         Discuss         Correct / Improve     constructors

Try 1 Question(s) Test


 Q33. Is a method name same as Class name permissible in Java ?
Ans. Yes, But the method should have some return type as otherwise it will be treated as a constructor.

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

   Like         Discuss         Correct / Improve     constructor  methods  method names

Try 1 Question(s) Test


 Q34. What is the difference between constructor and instance initialization blocks ?Core Java
Ans. Constructor has the same name as class name whereas instance initialization block just have a body without any name or visibility type.

instance initialization blocks are useful if we want to have some code run regardless of which constructor is used or if we want to do some instance initialization for anonymous classes.

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

   Like         Discuss         Correct / Improve     instance initialization blocks  constructor  constructor vs instance initialization blocks     Asked in 2 Companies

Try 1 Question(s) Test


 Q35. Why do we use a copy constructor ?
Ans. Copy Constructor is used for creating a duplicate of another object. Duplicate object will have the same state of the copied object but will have independent values in different memory locations. Copy Constructor can be useful to create duplicates of immutable objects as the Original cannot be tampered. Moreover It can be useful if base copies are required for individual requests in threading.

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

   Like         Discuss         Correct / Improve     copy constructor  clone  cloning object   immutable  immutability  immutability


 Q36. Have you ever felt the need of keeping the constructor private ?Design
Ans. Yes, When either we don't want an object to be created ( class having all static elements and hence not required ) or object to be created using a static method or static block. One such example could be a situation when we would like app to load objects at start up and we would like to restrict new object creation by any request.

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

   Like         Discuss         Correct / Improve     private constructor  constructor


 Q37. Is it mandatory to have a default constructor ?Core Java
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     Asked in 1 Companies

Try 1 Question(s) Test


 Q38. Why Java doesn't provide a default constructor if we define an argument constructor ?Core Java
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


 Q39. Is it a good Design practice to call methods from a constructor ?Design
Ans. Constructor's objective is to initialize member elements. If we don't deviate from that and don't have methods with logic in them, I don't see an issue with constructor's calling other methods assuming the methods are doing nothing but initialization. There are situations wherein we classify the member elements based on their type and hence having different initialization methods for different element types will give a better abstracted way to initialize them.

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

   Like         Discuss         Correct / Improve     design constructor


 Q40. Is it a good design practice to have programming constructs like if and loops in constructor ?Design
Ans. I would avoid that. If we need to initialize member elements differently on the basis of some condition, I would prefer having overloaded constructors. I don't see a need to have a loop for initializing member elements unless the count of elements is huge and they all need to be initialized with common value.

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

   Like         Discuss         Correct / Improve     design constructor  for loop


next 30

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: