This set of Java Multiple Choice Questions & Answers (MCQs) on “Constructor”.
1. What is true about private constructor? Answer: a 2. What would be the behaviour if this() and super() used in a method? Answer: c 3. What is false about constructor? Answer: c 4. What is true about Class.getInstance()? Answer: d 5. What is true about constructor? Answer: b 6. Abstract class cannot have a constructor. Answer: b 7. What is true about protected constructor? Answer: b 8. What is not the use of “this” keyword in Java? Answer: d 9. What would be the behaviour if one parameterized constructor is explicitly defined? Answer: d 10. What would be behaviour if the constructor has a return type? Answer: a
a) Private constructor ensures only one instance of a class exist at any point of time
b) Private constructor ensures multiple instances of a class exist at any point of time
c) Private constructor eases the instantiation of a class
d) Private constructor allows creating objects in other classes
Clarification: Object of private constructor can only be created within class. Private constructor is used in singleton pattern.
a) Runtime error
b) Throws exception
c) compile time error
d) Runs successfully
Clarification: this() and super() cannot be used in a method. This throws compile time error.
a) Constructors cannot be synchronized in Java
b) Java does not provide default copy constructor
c) Constructor can have a return type
d) “this” and “super” can be used in a constructor
Clarification: The constructor cannot have a return type. It should create and return new objects. Hence it would give a compilation error.
a) Class.getInstance calls the constructor
b) Class.getInstance is same as new operator
c) Class.getInstance needs to have matching constructor
d) Class.getInstance creates object if class does not have any constructor
Clarification: Class class provides list of methods for use like getInstance().
a) It can contain return type
b) It can take any number of parameters
c) It can have any non access modifiers
d) Constructor cannot throw an exception
Clarification: Constructor returns a new object with variables defined as in the class. Instance variables are newly created and only one copy of static variables are created.
a) True
b) False
Clarification: No instance can be created of abstract class. Only pointer can hold instance of object.
a) Protected constructor can be called directly
b) Protected constructor can only be called using super()
c) Protected constructor can be used outside package
d) protected constructor can be instantiated even if child is in a different package
Clarification: Protected access modifier means that constructor can be accessed by child classes of the parent class and classes in the same package.
a) Passing itself to another method
b) Calling another constructor in constructor chaining
c) Referring to the instance variable when local variable has the same name
d) Passing itself to method of the same class
Clarification: “this” is an important keyword in java. It helps to distinguish between local variable and variables passed in the method as parameters.
a) Compilation error
b) Compilation succeeds
c) Runtime error
d) Compilation succeeds but at the time of creating object using default constructor, it throws compilation error
Clarification: The class compiles successfully. But the object creation of that class gives a compilation error.
a) Compilation error
b) Runtime error
c) Compilation and runs successfully
d) Only String return type is allowed
Clarification: The constructor cannot have a return type. It should create and return new object. Hence it would give compilation error.