C++ Programming MCQ – Constructors and Destructors – 2

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

Q 1. What is the difference between constructors and destructors?
A. They have a different function name
B. Constructors allow function parameters whereas destructors do not
C. Constructors does not have return type whereas destructors do have
D. Constructors does not function parameters

Show Answer Answer:-B. Constructors allow function parameters whereas destructors do not
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.

Q 2. How many Destructors are allowed in a Class?
A. 1
B. 2
C. 3
D. Any number

Show Answer Answer:-A. 1
Explanation A class in C++ allows only one destructor, which is called whenever the lifetime of an object ends.

Q 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. 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

Show Answer Answer:-C. A’s Constructor called B’s Constructor called B’s Destructor called A’s Destructor called
Explanation The destructors for an object is called before the destructor of its data members or bases.

Q 4. 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{
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
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

Show Answer Answer:-C. A’s Constructor called B’s Constructor called B’s Destructor called A’s Destructor called
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.

Q 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

Show Answer Answer:-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
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.

Q 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. A’s Constructor called
B’s Constructor called
B’s Destructor called
A’s Destructor called

C. B’s Constructor called
B’s Destructor called

D. A’s Constructor called
B’s Constructor called
A’s Destructor called
B’s Destructor called

Show Answer Answer:-C. B’s Constructor called B’s Destructor called
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.

Q 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

Show Answer Answer:-A. A’s Constructor called A’s Destructor called
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.

Q 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();

Show Answer Answer:-C. A a = A(5);
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.


Posted

in

by

Comments

Leave a Reply

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

You cannot copy content of this page