Quiz: Constructor And Destructor

To View Tricks: Login Required

Number of Questions: 20

Question: 6 -

Which among the following is called first, automatically, whenever an object is created?

Options:
  1. New

  2. Class

  3. Trigger

  4. Constructor

  5. Answer:

    Constructor

    Solution:

    Constructors are the member functions which are called automatically whenever an object is created. It is a mandatory functions to be called for an object to be created as this helps in initializing the object to a legal initial value for the class.


Question: 7 -

Destructors __________ for automatic objects if the program terminates with a call to function exit or function abort

Options:
  1. Are created

  2. Are called

  3. Are not called

  4. Are inherited

  5. Answer:

    Are not called

    Solution:

    Destructors Are not called for automatic objects if the program terminates with a call to function exit or function abort


Question: 8 -

What will be the output of the following program?

#include <iostream>
using namespace std;
class LFC {
    LFC() { cout << "Constructor called"; }
};

int main()
{
   LFC t1;
   return 0;
}

Options:
  1. Runtime Error

  2. Constructor called

  3. Compiler Error

  4. destructor for id 1

  5. Answer:

    Compiler Error

    Solution:

    By default all members of a class are private. Since no access specifier is there for find(), it becomes private and it is called outside the class when t1 is constructed in main.


Question: 9 -

Which contructor function is designed to copy object of same class type?

Options:
  1. Copy constructor

  2. Create constructor

  3. Dynamic constructor

  4. Object constructor

  5. Answer:

    Copy constructor

    Solution:

    Copy constructor function is designed to copy object of same class type.


Question: 10 -

Which of the following is true about constructors.

i) They cannot be virtual

ii) They cannot be private.

iii) They are automatically called by new operator.

Options:
  1. i & iii

  2. ii & iii

  3. All i,ii,iii

  4. i & ii

  5. Answer:

    i & iii

    Solution:

    i) True: Virtual constructors don't make sense, it is meaningless to the C++ compiler to create an object polymorphically. ii) False: Constructors can be private