250+ TOP MCQs on Constructors and Destructors – 2 and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Constructors and Destructors – 2”.

1. What is the difference between constructors and destructors?
a) They have a different function name
b) Constructors does not have return type whereas destructors do have
c) Constructors allow function parameters whereas destructors do not
d) Constructors does not function parameters
Answer: c
Clarification: Both the constructors and destructors have the same function name and both of them do not have return type but constructors allow function parameters whereas destructors do not.

2. How many Destructors are allowed in a Class?
a) 1
b) 2
c) 3
d) Any number
Answer: a
Clarification: A class in C++ allows only one destructor, which is called whenever the lifetime of an object ends.

3. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"A's Constructor calledn";
	}
	~A(){
		cout<<"A's Destructor calledn";
	}
};
class B{
	A a;
public:
	B(){
		cout<<"B's Constructor calledn";
	}
	~B(){
		cout<<"B's Destructor calledn";
	}
};
int main(int argc, char const *argv[])
{
	B b1;
}

a)

A's Constructor called
B's Constructor called

b)

A's Destructor called
B's Destructor called

c)

A's Constructor called
B's Constructor called
B's Destructor called
A's Destructor called

d)

A's Constructor called
B's Constructor called
A's Destructor called
B's Destructor called

View Answer

Answer: c
Clarification: The destructors for an object is called before the destructor of its data members or bases.

 
 

4. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"A's Constructor calledn";
	}
	~A(){
		cout<<"A's Destructor calledn";
	}
};
class B: public A{
public:
	B(){
		cout<<"B's Constructor calledn";
	}
	~B(){
		cout<<"B's Destructor calledn";
	}
};
int main(int argc, char const *argv[])
{
	B b1;
}

a)

A's Constructor called
B's Constructor called

b)

A's Destructor called
B's Destructor called

c)

A's Constructor called
B's Constructor called
B's Destructor called
A's Destructor called

d)

A's Constructor called
B's Constructor called
A's Destructor called
B's Destructor called

View Answer

Answer: c
Clarification: Though B class have no data member of the class but as class B is derived from class A, the destructor of class A will be called to destroy the data inherited from class A to class B.

 
 

5. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"A's Constructor calledn";
	}
	~A(){
		cout<<"A's Destructor calledn";
	}
};
class B: public A{
	A a;
public:
	B(){
		cout<<"B's Constructor calledn";
	}
	~B(){
		cout<<"B's Destructor calledn";
	}
};
int main(int argc, char const *argv[])
{
	B b1;
}

a)

A's Constructor called
B's Constructor called

b)

A's Destructor called
B's Destructor called

c)

A's Constructor called
A's Constructor called
B's Constructor called
B's Destructor called
A's Destructor called
A's Destructor called

d)

A's Constructor called
B's Constructor called
A's Destructor called
B's Destructor called

View Answer

Answer: c
Clarification: There are two calls to constructor of class A, one is for the data member of class B and second because class B is derived from class A. Similarly two destructor calls.

 
 

6. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"A's Constructor calledn";
	}
	~A(){
		cout<<"A's Destructor calledn";
	}
};
class B{
	static A a;
public:
	B(){
		cout<<"B's Constructor calledn";
	}
	~B(){
		cout<<"B's Destructor calledn";
	}
};
int main(int argc, char const *argv[])
{
	B b1;
}

a)

A's Constructor called
B's Constructor called

b)

B's Constructor called
B's Destructor called

c)

A's Constructor called
B's Constructor called
B's Destructor called
A's Destructor called

d)

A's Constructor called
B's Constructor called
A's Destructor called
B's Destructor called

View Answer

Answer: b
Clarification: Here as ‘a’ is a static member of class B and as all static members should be initialized separately as no object creation initializes static member and as ‘a’ is not initialized, hence no call will be made to the constructor of class A.

 
 

7. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"A's Constructor calledn";
	}
	~A(){
		cout<<"A's Destructor calledn";
	}
};
 
class B{
	static A a;
public:
	B(){
		cout<<"B's Constructor calledn";
	}
	~B(){
		cout<<"B's Destructor calledn";
	}
};
 
A B::a;
 
int main(int argc, char const *argv[])
{
	return 0;
}

a)

A's Constructor called
A's Destructor called

b)

B's Constructor called
B's Destructor called

c)

A's Constructor called
B's Constructor called
B's Destructor called
A's Destructor called

d)

A's Constructor called
B's Constructor called
A's Destructor called
B's Destructor called

View Answer

Answer: a
Clarification: Here as no object of B is declared so no call to B’s constructor but as we have initialised the static member ‘a’ of class B, hence A’s constructor and destructor will be called once.

 
 

8. Which of the following represents the correct explicit call to a constructor of class A?

class A{
		int a;
	        public:
		A(int i)
                {
			a = i;
		}
       }

a) A a(5);
b) A a;
c) A a = A(5);
d) A a = A();
Answer: c
Clarification: Explicit call represents the programmer by himself mentioning the type name. So A a = A(5); is the correct explicit call as we are mentioning typename A(5) from our side, whereas A a = A(); is not the correct call because no such constructor is there in class A.

contest

Leave a Reply

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