Object Oriented Programming using C++ Questions and Answers – Execution of Constructor or Destructor

«
»

This set of Object Oriented Programming using C++ Questions and Answers for Experienced people focuses on “Execution of Constructor or Destructor”.

1. Which among the following best describes the constructors?
a) A function which is called whenever an object is referenced
b) A function which is called whenever an object is created to initialize the members
c) A function which is called whenever an object is assigned to copy the values
d) A function which is called whenever an object is to be given values for members
View Answer

Answer: b
Explanation: The constructors are special type of functions which are called whenever an object is created. This is to initialize the data members of the class. The constructor allocates memory space for all the data members.
advertisement

2. Which among the following best describes destructor?
a) A function which is called just before the objects are destroyed
b) A function which is called after each reference to the object
c) A function which is called after termination of the program
d) A function which is called before calling any member function
View Answer

Answer: a
Explanation: The Destructors are special functions which are called just before an object is destroyed. This functions is responsible to free all the allocated resources to the object. Objects are destroyed whenever those go out of scope.

3. Which among the following represents correct constructor?
a) ()classname
b) ~classname()
c) –classname()
d) classname()
View Answer

Answer: d
Explanation: The constructors must contain only the class name. The class name is followed by the blank parenthesis or we can have parameters if some values are to be passed.
advertisement
advertisement

4. Which among the following is correct syntax for the destructors?
a) classname()
b) ()classname
c) ~classname()
d) -classname()
View Answer

Answer: c
Explanation: The destructor must have same name as that of the corresponding class. The class name should be preceded by the tilde symbol (~).

5. Which among the following is true?
a) First the constructor of parent classes are called in sequence of inheritance
b) First the constructor of child classes are called in the sequence of inheritance
c) First constructor called is of the object being created
d) Constructors are called randomly
View Answer

Answer: a
Explanation: First the constructor of parent class are called. The order in which the parent class constructors are called is same in the sequence of inheritance used.
advertisement

6. What is the sequence of destructors call?
a) Same order as that of the constructors call
b) Random order
c) According to the priority
d) Revere of the order of constructor call
View Answer

Answer: d
Explanation: The destructors are called in the reverse order as that of the constructors being called. This is done to ensure that all the resources are released in sequence. That is, the derived class destructors will be called first.

7. The destructors _____________________
a) Can have maximum one argument
b) Can’t have any argument
c) Can have more than one argument
d) Can’t have more than 3 arguments
View Answer

Answer: b
Explanation: The destructors doesn’t have any arguments. The destructors have to be called implicitly whenever an object goes out of scope. The user can’t pass argument to the implicit call.
advertisement

8. Destructor calls ________________ (C++)
a) Are only implicit
b) Are only explicit
c) Can be implicit or explicit
d) Are made at end of program only
View Answer

Answer: c
Explanation: The destructors are usually called implicitly whenever an object goes out of scope. The destructors can also be called explicitly if required. The call must be made, implicitly or explicitly.

9. Number of destructors called are ____________
a) Always equal to number of constructors called
b) Always less than the number of constructors called
c) Always greater than the number of constructors called
d) Always less than or equal to number of constructors
View Answer

Answer: a
Explanation: Destructor will be called only to free the resources allocated for an object. The resources are allocated only the constructor for an object is called.
advertisement

10. For explicit call _________________
a) The destructor must be private
b) The destructor must be public
c) The destructor must be protected
d) The destructor must be defined outside the class
View Answer

Answer: b
Explanation: The destructor must be public for explicit calls. If it is made private or protected then it won’t be accessible outside the class. There is no restriction of definition the destructor outside the class.

11. If a class have 4 constructors then it must have 4 destructors also.
a) True
b) False
View Answer

Answer: b
Explanation: Even if the class have 4 constructors, only one would be used. And only one destructor is allowed.

12. Which among the following is true for destructors?
a) Destructors can be overloaded
b) Destructors can be define more than one time
c) Destructors can’t be overloaded
d) Destructors are overloaded in derived classes
View Answer

Answer: c
Explanation: The destructors can never be overloaded. The destructors doesn’t have arguments. And to get overloaded, they must have different signature. This is not possible if arguments are not allowed.

13. The constructor _____________
a) Have a return type
b) May have a return type
c) Of derived classes have return type
d) Doesn’t have a return type
View Answer

Answer: d
Explanation: The constructors doesn’t have any return type. The constructors are intended to allocate the resources for the object. Memory spaces are to be finalized.

14. The destructors ____________
a) Have a return type
b) May have a return type
c) Of derived classes have return type
d) Doesn’t have a return type
View Answer

Answer: d
Explanation: The destructors are intended to free the memory space. And all the resources that were allocated for the object. The return value is not supported since only memory has to be made free.

15. The destructor can be called before the constructor if required.
a) True
b) False
View Answer

Answer: b
Explanation: The destructors can be called only after the constructor calls. It is not a mandatory rule but the deletion can only take place if there is something created using the constructor.

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

To practice all areas of Object Oriented Programming using C++ for Experienced people, 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