250+ TOP MCQs on Execution of Constructor or Destructor and Answers

Object Oriented Programming Questions Experienced people 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
Answer: b
Clarification: 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.

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
Answer: a
Clarification: 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()
Answer: d
Clarification: 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.

4. Which among the following is correct syntax for the destructors?
a) classname()
b) ()classname
c) ~classname()
d) -classname()
Answer: c
Clarification: 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
Answer: a
Clarification: 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.

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
Answer: d
Clarification: 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
Answer: b
Clarification: 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.

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
Answer: c
Clarification: 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
Answer: a
Clarification: 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.

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
Answer: b
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: 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
Answer: d
Clarification: 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
Answer: d
Clarification: 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
Answer: b
Clarification: 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.

Object Oriented Programming for Experienced people,

Leave a Reply

Your email address will not be published. Required fields are marked *