Object Oriented Programming using C++ Questions and Answers – Constructors Overloading

«
»

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Constructors Overloading”.

1. Which among the following is true for constructors overloading?
a) Constructors can’t be overloaded
b) Constructors can be overloaded using different signatures
c) Constructors can be overloaded with same signatures
d) Constructors can be overloaded with different return types
View Answer

Answer: b
Explanation: The constructors can be overloaded only if the definitions have different signatures. Constructors doesn’t have any return type so can’t be overloaded using return type. If constructors have same signature then it will produce a compile time error.
advertisement

2. If a constructors should be capable of creating objects without argument and with arguments, which is a good alternative for this purpose?
a) Use zero argument constructor
b) Use constructor with one parameter
c) Use constructor with all default arguments
d) Use default constructor
View Answer

Answer: c
Explanation: The constructor should use all the default arguments. This will allow the constructor to be called even if no arguments are passed. And if arguments are passed, those will be accepted instead of the default values.

3. The Constructors with all the default arguments are similar as default constructors.
a) True
b) False
View Answer

Answer: a
Explanation: The constructors with all the default arguments are similar as the default constructors. Those can be used instead of the default constructors. So defining the default constructor is not mandatory.
advertisement
advertisement

4. Which among the following is true?
a) The constructors overloading can be done by using different names
b) The constructors overloading can be done by using different return types
c) The constructors can be overloaded by using only one argument
d) The constructors must have the same name as that of class
View Answer

Answer: d
Explanation: The constructors must have the same name as that of the class name. This is mandatory because only the constructor functions doesn’t have any return type. Also, for overloading all the functions must have the same name.

5. Which among the following can be used in place of default constructor?
a) constructorName(int x, int y=0)
b) constructorName(int x=0, int y=0)
c) constructorName(int x=0, int y)
d) constructorName(int x, int y)
View Answer

Answer: b
Explanation: For a parameterized constructor to be used as a default constructor, it must have all the default arguments. This makes the constructor to have optional arguments which are not mandatory to be passed.
advertisement

6. Can a class have more than one function with all the default arguments?
a) Yes, always
b) Yes, if argument list is different
c) No, because constructors overloading doesn’t depend on argument list
d) No, never
View Answer

Answer: d
Explanation: A single class can never have more than once constructor with all the default arguments. This is because it will make all those constructors as a default constructor. And when an object is created with zero arguments then it will create ambiguity.

7. Which is the correct syntax for using default arguments with the constructor?
a) default constructorName(default int x=0)
b) constructorName(default int x=0)
c) constructorName(int x=0)
d) constructorName()
View Answer

Answer: c
Explanation: The constructors using the default arguments must initialize the arguments in the argument list. This is to make the constructor use the default value when no arguments are passed. If no arguments are listed then it is a default constructor.
advertisement

8. How many parameters must be passed if only the following prototype is given to a constructor?
Prototype: className(int x, int y, int z=0);
a) 1
b) 2
c) 3
d) Compile time error
View Answer

Answer: b
Explanation: In the prototype given, only 2 arguments are mandatory. Since the third argument is default argument, so it is not mandatory to pass the argument.

9. If the constructors are overloaded by using the default arguments, which problem may arise?
a) The constructors might have all the same arguments except the default arguments
b) The constructors might have same return type
c) The constructors might have same number of arguments
d) The constructors can’t be overloaded with respect to default arguments
View Answer

Answer: a
Explanation: The constructors having same arguments except the default arguments can give rise to errors. If only the mandatory arguments are passed, it will create ambiguity in calling the correct constructor. Hence the mandatory arguments must be different.
advertisement

10. Which among the following is true?
a) More than one constructors with all default arguments is allowed
b) More than one constructors with all default arguments can be defined outside the class
c) More than one constructors can be used with same argument list
d) More than one constructors with all default arguments can’t exist in same class
View Answer

Answer: d
Explanation: The constructors must have different argument list. Along that, if all the arguments are default arguments, there can’t be more than once constructor like that in the same class as that will create ambiguity while constructors are being called.

11. Which constructor among the following will be called if a call is made like className(5,’a’);?
a) className(int x=5,char c=’a’);
b) int className(int x, char c, char d);
c) className(int x, char c, int y);
d) char className(char c,int x);
View Answer

Answer: a
Explanation: The syntax given is passing two parameters to the constructor call. One value is of integer type and another of character type. Hence the constructor with arguments of int and char type should be called. There is only one option that first accepts integer value and then a character value. Hence that constructor will be called.

12. Which constructor definition will produce a compile time error?
a) className(int x=0);
b) className(char c);
c) className(int x=0,char c);
d) className(char c,int x=0);
View Answer

Answer: c
Explanation: The default arguments, just like with member functions, must be listed at last in the argument list. Hence this will produce a compile time error. The compiler doesn’t allow the definition to be executed.

13. If there is a constructor with all the default arguments and arguments are not passed then _________________
a) The default values given will not be used
b) Then all the null values will be used
c) Then all the default values given will be used
d) Then compiler will produce an error
View Answer

Answer: c
Explanation: The constructors will use the default values listed for use. The null values are not used because those are not specified. Though if it was compiler provided default constructor, then it would have initialized the members to null or zero values.

14. Which is the correct statement for default constructors?
a) The constructors with all the default arguments
b) The constructors with all the null and zero values
c) The constructors which can’t be defined by programmer
d) The constructors with zero arguments
View Answer

Answer: d
Explanation: The closest answer to the question is that a default constructor is a constructor with zero arguments. But this is not the actual case. Actually the constructors provided by the compiler are default constructors. And the constructors with zero arguments defined by the programmer are zero argument constructors.

15. Which is a good alternative instead of having one zero argument constructor and one single argument constructor with default argument?
a) No constructor defined
b) One default value constructor
c) Defining the default constructor
d) Using one constructor with two arguments
View Answer

Answer: b
Explanation: The constructor with one default argument can be the best alternative. This is because the constructor with one default value will do the work for both the default constructor and one argument constructor.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.

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!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter