Multiple choice

What is the output of C++ program given below?

#include<iostream> using namespace std; class Test { public: Test() { cout<<"constructor of Testn"; } ~Test() { cout<<"destructor of Testn"; } }; int main() { Test a; Test b; }

  1. constructor of Test constructor of Test destructor of Test destructor of Test

  2. constructor of Test destructor of Test constructor of Test destructor of Test

  3. constructor of Test constructor of Test

  4. constructor of Test destructor of Test

  5. None of the above

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

 Given class Test contains a default constructor & default destructor. We know constructor is called when we create instance for a class. Destructor is called when the instance variable doesnt point to any object or simply when it gets dereferenced. Test a;  //prints constructor of Test Test b;  //prints constructor of Test Now after end of the program, created instances are de-referenced. Hence prints destructor of Test (for instance 'a') prints destructor of Test (for instance 'b')