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
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
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)
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 calledView Answer
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?
#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 calledView Answer
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 calledView Answer
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 calledView Answer
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 calledView Answer
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
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.
- Check Computer Science Books
- Practice Programming MCQs
- Apply for C++ Internship
- Apply for Computer Science Internship
- Check Programming Books