C++ Programming MCQ – Constructors and Destructors – 2

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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 <iostream>
#include <string>
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"A's Constructor called\n";
	}
	~A(){
		cout<<"A's Destructor called\n";
	}
};
class B{
	A a;
public:
	B(){
		cout<<"B's Constructor called\n";
	}
	~B(){
		cout<<"B's Destructor called\n";
	}
};
int main(int argc, char const *argv[])
{
	B b1;
}

a)

A's Constructor called
B's Constructor called

b)

advertisement
advertisement
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)

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
A's Constructor called
B's Constructor called
A's Destructor called
B's Destructor called
View Answer
Answer: c
Explanation: 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?

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

a)

advertisement
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
Explanation: 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 <iostream>
#include <string>
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"A's Constructor called\n";
	}
	~A(){
		cout<<"A's Destructor called\n";
	}
};
class B: public A{
	A a;
public:
	B(){
		cout<<"B's Constructor called\n";
	}
	~B(){
		cout<<"B's Destructor called\n";
	}
};
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
Explanation: 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 <iostream>
#include <string>
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"A's Constructor called\n";
	}
	~A(){
		cout<<"A's Destructor called\n";
	}
};
class B{
	static A a;
public:
	B(){
		cout<<"B's Constructor called\n";
	}
	~B(){
		cout<<"B's Destructor called\n";
	}
};
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
Explanation: 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 <iostream>
#include <string>
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"A's Constructor called\n";
	}
	~A(){
		cout<<"A's Destructor called\n";
	}
};
 
class B{
	static A a;
public:
	B(){
		cout<<"B's Constructor called\n";
	}
	~B(){
		cout<<"B's Destructor called\n";
	}
};
 
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
Explanation: 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();
View Answer

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

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.