Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Overloading Constructors”.
1. Which among the following best describes constructor overloading?
a) Defining one constructor in each class of a program
b) Defining more than one constructor in single class
c) Defining more than one constructor in single class with different signature
d) Defining destructor with each constructor
Answer: c
Clarification: If more than one constructors are defined in a class with same signature, then that results in error. The signatures must be different. So that the constructors can be differentiated.
2. Can constructors be overloaded in derived class?
a) Yes, always
b) Yes, if derived class has no constructor
c) No, programmer can’t do it
d) No, never
Answer: d
Clarification: The constructor must be having the same name as that of a class. Hence a constructor of one class can’t even be defined in another class. Since the constructors can’t be defined in derived class, it can’t be overloaded too, in derived class.
3. Does constructor overloading include different return types for constructors to be overloaded?
a) Yes, if return types are different, signature becomes different
b) Yes, because return types can differentiate two functions
c) No, return type can’t differentiate two functions
d) No, constructors doesn’t have any return type
Answer: d
Clarification: The constructors doesn’t have any return type. When we can’t have return type of a constructor, overloading based on the return type is not possible. Hence only parameters can be different.
4. Which among the following is possible way to overload constructor?
a) Define default constructor, 1 parameter constructor and 2 parameter constructor
b) Define default constructor, zero argument constructor and 1 parameter constructor
c) Define default constructor, and 2 other parameterized constructors with same signature
d) Define 2 default constructors
Answer: a
Clarification: All the constructors defined in a class must have a different signature in order to be overloaded. Here one default and other parameterized constructors are used, wherein one is of only one parameter and other accepts two. Hence overloading is possible.
5. Which constructor will be called from the object created in the code below?
class A { int i; A() { i=0; cout<<i; } A(int x=0) { i=x; cout<<I; } }; A obj1;
a) Default constructor
b) Parameterized constructor
c) Compile time error
d) Run time error
Answer: c
Clarification: When a default constructor is defined and another constructor with 1 default value argument is defined, creating object without parameter will create ambiguity for the compiler. The compiler won’t be able to decide which constructor should be called, hence compile time error.
6. Which among the following is false for a constructor?
a) Constructors doesn’t have a return value
b) Constructors are always user defined
c) Constructors are overloaded with different signature
d) Constructors may or may not have any arguments being accepted
Answer: b
Clarification: The constructors are not always user defined. The construct will be provided implicitly from the compiler if the used doesn’t defined any constructor. The implicit constructor makes all the string values null and allocates memory space for each data member.
7. When is the constructor called for an object?
a) As soon as overloading is required
b) As soon as class is derived
c) As soon as class is created
d) As soon as object is created
Answer: d
Clarification: The constructor is called as soon as the object is created. The overloading comes into picture as to identify which constructor have to be called depending on arguments passed in the creation of object.
8. Which among the following function can be used to call default constructor implicitly in java?
a) this()
b) that()
c) super()
d) sub()
