Java Questions and Answers Part-5

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

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

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

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

4. What is true about Class.getInstance()?
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

Answer: d
Explanation: Class class provides list of methods for use like getInstance().

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

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.

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

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

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.

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

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?
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

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

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