This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Constructor”.
1. What is true about private constructor?
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
View Answer
Explanation: Object of private constructor can only be created within class. Private constructor is used in singleton pattern.
2. What would be the behaviour if this() and super() used in a method?
a) Runtime error
b) Throws exception
c) compile time error
d) Runs successfully
View Answer
Explanation: this() and super() cannot be used in a method. This throws compile time error.
3. What is false about constructor?
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
View Answer
Explanation: The constructor cannot have a return type. It should create and return new objects. Hence it would give a compilation error.
4. What could be true about Class.getInstance() considering that it defines the getInstance() method?
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
View Answer
Explanation: The method getInstance() can be used to create an object if the class doesn’t have any constructor.
5. What is true about constructor?
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
View Answer
Explanation: 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.
6. Abstract class cannot have a constructor.
a) True
b) False
View Answer
Explanation: No instance can be created of abstract class. Only pointer can hold instance of object.
7. What is true about protected constructor?
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
View Answer
Explanation: Protected access modifier means that constructor can be accessed by child classes of the parent class and classes in the same package.
8. What is not the use of “this” keyword in Java?
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
View Answer
Explanation: “this” is an important keyword in java. It helps to distinguish between local variable and variables passed in the method as parameters.
9. What would be the behaviour if one parameterized constructor is explicitly defined, but no default constructor is provided?
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
View Answer
Explanation: The class compiles successfully. But the object creation of that class gives a compilation error.
10. What would be behaviour if the constructor has a return type?
a) Compilation error
b) Runtime error
c) Compilation and runs successfully
d) Only String return type is allowed
View Answer
Explanation: The constructor cannot have a return type. It should create and return new object. Hence it would give compilation error.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Java Internship
- Practice BCA MCQs
- Practice Information Technology MCQs
- Practice Programming MCQs
- Check Java Books