Java Questions & Answers – Constructor

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

Answer: a
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

Answer: c
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

Answer: c
Explanation: The constructor cannot have a return type. It should create and return new objects. Hence it would give a compilation error.
advertisement
advertisement

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

Answer: d
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

Answer: b
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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. Abstract class cannot have a constructor.
a) True
b) False
View Answer

Answer: b
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

Answer: b
Explanation: Protected access modifier means that constructor can be accessed by child classes of the parent class and classes in the same package.
advertisement

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

Answer: d
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

Answer: d
Explanation: The class compiles successfully. But the object creation of that class gives a compilation error.
advertisement

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

Answer: a
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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.