C++ Constructors and Destructors

Test your knowledge of C++ special member functions including constructors, destructors, their types, characteristics, and usage in object initialization and cleanup.

15 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

Which of the following is not a characteristic of destructor function used in C++?

  1. Destructor must have the same name as the class preceded by a tilde (~).
  2. Destructor has a return type.
  3. Destructor cannot take arguments; so it cannot be overloaded.
  4. There can be only one destructor in each class.
  5. Destructor cannot be inherited.
Question 2 Multiple Choice (Single Answer)

Which of the following types of constructor is a special member function that is invoked by the C++ compiler without any arguments for initialising the objets of a class?

  1. Parameterized constructor
  2. Copy constructor
  3. Default constructor
  4. Constructor overloading
  5. Destructor
Question 3 Multiple Choice (Single Answer)

A constructor function can be declared in ____________ section only.

  1. private
  2. protected
  3. public
  4. private protected
  5. default
Question 4 Multiple Choice (Single Answer)

Consider the following C++ code:
class Name{ int x,y; public: Name(int a, b=10) { x=a; }};

If an object of this class is created as Name obj1(100); then what would be the values assigned to x and y?

  1. 100 and 10
  2. 100 and 200
  3. 10 and 100
  4. 200 and 100
  5. No values are assigned.
Question 5 Multiple Choice (Single Answer)

What is the main purpose of using default constructor in a C++ program?

  1. It is compulsorily required in every program.
  2. It is used to access data members of a class.
  3. It is used to initialise the object of a class.
  4. It is used to avoid any confusion when overloaded constructors are used.
  5. It is of no use.
Question 6 Multiple Choice (Single Answer)

Which of the following special member functions is executed when an object of that class is destroyed?

  1. Copy constructor
  2. Default constructor
  3. Constructor with default arguments
  4. Destructor
  5. Parameterized constructor
Question 7 Multiple Choice (Single Answer)

Which of the following is not a characteristic of a constructor?

  1. It should be declared in the public section.
  2. It can be invoked automatically when the objects are created.
  3. It cannot return any value not even void type.
  4. Constructors can be virtual.
  5. Constructors can make implicit calls to the operators new and delete when memory allocation is required.
Question 8 Multiple Choice (Single Answer)

Which of the following variables can be used as an argument to copy constructor?

  1. Static variable
  2. Pointer variable
  3. Reference variable
  4. new
  5. delete
Question 9 Multiple Choice (Single Answer)

Which of the following functions is created by the compiler when no constructor is created or supplied by the user?

  1. Friend function
  2. Inline function
  3. Default constructor
  4. this pointer
  5. Static member function
Question 10 Multiple Choice (Single Answer)

Which of the following types of constructor function should be used to initialise the various data elements of different objects with different values when they are created?

  1. Default constructor
  2. Parameterized constructor
  3. Copy constructor
  4. Overloaded constructor
  5. Constructor with default value
Question 11 Multiple Choice (Single Answer)

Consider the following C++ code and select its output from the given options:

class A{ int a; public: A(int x) {cout<< Base Constructor:<

  1. Derived constructor:Base constructor:
  2. Base constructor:Derived constructor:
  3. Default constructor:Base constructor:
  4. Derived constructor:Default constructor:
  5. Base constructor:Base constructor:
Question 12 Multiple Choice (Single Answer)

What do you think would be the main advantage of creating constructor function in a class?

  1. It avoids creating private members in a class.
  2. Constuctor functions are automatically invoked when objects are created.
  3. It avoids creating friend function in a class.
  4. It avoids creating destructor function in a class.
  5. It avoids creating pointers to objects.
Question 13 Multiple Choice (Single Answer)

Which of the following is one of the special properties of a destructor function?

  1. There can be only one destructor function for a class.
  2. Destructor functions are not invoked automatically when the objects are destroyed.
  3. Arguments can be provided to a destructor.
  4. A destructor function can return value.
  5. It can be declared in the private section of a class.
Question 14 Multiple Choice (Single Answer)

Consider the following C++ code:

Number num1 = Number(100,200);

If Number refers to a class, then this method of calling constructor function is called

  1. explicit call
  2. implicit call
  3. pointer call
  4. reference call
  5. call by value
Question 15 Multiple Choice (Single Answer)

When two or more constructor functions with the same name but with different function signatures act in the same class, it is called

  1. default constructor
  2. copy constructor
  3. constructor overloading
  4. parameterized constructor
  5. exit()