192). 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
Show Answer
Correct Answer: Private constructor ensures only one instance of a class exist at any point of time
Notes:
Answer: a
Explanation: Object of private constructor can only be created within class. Private constructor is used in singleton pattern.
193). 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
Show Answer
Correct Answer: compile time error
Notes:
Answer: c
Explanation: this() and super() cannot be used in a method. This throws compile time error.
194). 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
Show Answer
Correct Answer: Constructor can have a return type
Notes:
Answer: c
Explanation: The constructor cannot have a return type. It should create and return new objects. Hence it would give a compilation error.
195). 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
Show Answer
Correct Answer: Class.getInstance creates object if class does not have any constructor
Notes:
Answer: d
Explanation: Class class provides list of methods for use like getInstance().
196). 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
Show Answer
Correct Answer: It can take any number of parameters
Notes:
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.
197). Abstract class cannot have a constructor.
[A]True
[B]False
[C]none
[D]none
Show Answer
Correct Answer: False
Notes:
Answer: b
Explanation: No instance can be created of abstract class. Only pointer can hold instance of object.
198). 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
Show Answer
Correct Answer: Protected constructor can only be called using super()
Notes:
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.
199). 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
Show Answer
Correct Answer: Passing itself to method of the same class
Notes:
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.
200). 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
Show Answer
Correct Answer: Compilation succeeds but at the time of creating object using default constructor, it throws compilation error
Notes:
Answer: d
Explanation: The class compiles successfully. But the object creation of that class gives a compilation error.
201). 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
Show Answer
Correct Answer: Compilation error
Notes:
Answer: a
Explanation: The constructor cannot have a return type. It should create and return new object. Hence it would give compilation error.