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.
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 :
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 :
Constructor provided by Java if no constructor is declared
Constructor with empty body
All of the above
Default Constructor is provided by Java ...
To Reserve Memory
To provide at least one instance method
To Make it look good
To initialize the object state
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.
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 :
How can we create objects if we make the constructor private ?
We can't create objects if constructor is private
We can only create objects if we follow singleton pattern
We can only create one object
We can create new object through static method or static block
What will be the output of following code ?
public class BuggyBread {
private int x;
private Integer y;
private BuggyBread(int x,int y){};
public static void main(String[] args){
BuggyBread buggybread = new BuggyBread(1,2);
System.out.println(buggybread.x);
}
}
compilation error due to private constructor
compilation error due to uninitialized elements
0 null
0 0
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
public static void main(String[] args){
System.out.println("Main Method");
}
}
Static Block
Main Method
Static Block
Instance Initialization Block
Main Method
Static Block
Constructor
Main Method
Static Block
Instance Initialization Block
Constructor
Main Method
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 :
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 :
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 :
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 :
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 :