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.
Questions
Which of the following is not a characteristic of destructor function used in C++?
- Destructor must have the same name as the class preceded by a tilde (~).
- Destructor has a return type.
- Destructor cannot take arguments; so it cannot be overloaded.
- There can be only one destructor in each class.
- Destructor cannot be inherited.
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?
- Parameterized constructor
- Copy constructor
- Default constructor
- Constructor overloading
- Destructor
A constructor function can be declared in ____________ section only.
- private
- protected
- public
- private protected
- default
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?
- 100 and 10
- 100 and 200
- 10 and 100
- 200 and 100
- No values are assigned.
What is the main purpose of using default constructor in a C++ program?
- It is compulsorily required in every program.
- It is used to access data members of a class.
- It is used to initialise the object of a class.
- It is used to avoid any confusion when overloaded constructors are used.
- It is of no use.
Which of the following special member functions is executed when an object of that class is destroyed?
- Copy constructor
- Default constructor
- Constructor with default arguments
- Destructor
- Parameterized constructor
Which of the following is not a characteristic of a constructor?
- It should be declared in the public section.
- It can be invoked automatically when the objects are created.
- It cannot return any value not even void type.
- Constructors can be virtual.
- Constructors can make implicit calls to the operators new and delete when memory allocation is required.
Which of the following variables can be used as an argument to copy constructor?
- Static variable
- Pointer variable
- Reference variable
- new
- delete
Which of the following functions is created by the compiler when no constructor is created or supplied by the user?
- Friend function
- Inline function
- Default constructor
- this pointer
- Static member function
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?
- Default constructor
- Parameterized constructor
- Copy constructor
- Overloaded constructor
- Constructor with default value
Consider the following C++ code and select its output from the given options:
class A{ int a; public: A(int x) {cout<< Base Constructor:<
- Derived constructor:Base constructor:
- Base constructor:Derived constructor:
- Default constructor:Base constructor:
- Derived constructor:Default constructor:
- Base constructor:Base constructor:
What do you think would be the main advantage of creating constructor function in a class?
- It avoids creating private members in a class.
- Constuctor functions are automatically invoked when objects are created.
- It avoids creating friend function in a class.
- It avoids creating destructor function in a class.
- It avoids creating pointers to objects.
Which of the following is one of the special properties of a destructor function?
- There can be only one destructor function for a class.
- Destructor functions are not invoked automatically when the objects are destroyed.
- Arguments can be provided to a destructor.
- A destructor function can return value.
- It can be declared in the private section of a class.
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
- explicit call
- implicit call
- pointer call
- reference call
- call by value
When two or more constructor functions with the same name but with different function signatures act in the same class, it is called
- default constructor
- copy constructor
- constructor overloading
- parameterized constructor
- exit()