Constructors and Destructors Questions
Practice Constructors and Destructors MCQs with answers and explanations. Page 3 of 3.
Category
C++ Programming
Topic
Constructors and Destructors
Page
3 / 3
Mode
Practice
Questions
Open any question to view the answer and explanation.
C++ object lifetime: constructor prints "Curious", destructor prints "Tab" — what is the output?
#include <iostream.h>
class CuriousTab {
public:
CuriousTab() { cout << "Curious"; }
~CuriousTab() { cout << "Tab"; }
};
int main() {
CuriousTab objTab; // automatic object
return 0; // destructor runs at scope end
}
Open
View answer
C++ deleting a derived object via base pointer without a virtual destructor: what sequence prints?
#include <iostream.h>
class CuriousTabBase {
public:
CuriousTabBase() { cout << "Base OK. "; }
~CuriousTabBase() { cout << "Base DEL. "; }
};
class CuriousTabDerived : public CuriousTabBase {
public:
CuriousTabDerived() { cout << "Derived OK. "; }
~CuriousTabDerived() { cout << "Derived DEL. "; }
};
int main() {
CuriousTabBase basePtr = new CuriousTabDerived();
delete basePtr; // base dtor is non-virtual
return 0;
}
Open
View answer
C++ constructor prints a character from a float via char(yy): what appears on the console?
#include <iostream.h>
class CuriousTab {
int x;
public:
CuriousTab(int xx, float yy) { cout << char(yy); }
};
int main() {
CuriousTab *p = new CuriousTab(35, 99.50f);
return 0;
}
Open
View answer
C++ (constructor/destructor order) — determine the exact output produced by construction of multiple objects and their destruction including a nested scope.
#include<iostream.h>
int val = 0;
class CuriousTab {
public:
CuriousTab() { cout << ++val; }
~CuriousTab() { cout << val--; }
};
int main() {
CuriousTab objCuriousTab1, objCuriousTab2, objCuriousTab3;
{ CuriousTab objCuriousTab4; }
return 0;
}
What character sequence is written to stdout?
Open
View answer
C++ (constructor overload resolution) — which constructor is called for a character literal created via new, and what is printed?
#include<iostream.h>
class CuriousTab {
int x;
public:
CuriousTab(short) { cout << "Short" << endl; }
CuriousTab(int) { cout << "Int" << endl; }
CuriousTab(char) { cout << "Char" << endl; }
~CuriousTab() { cout << "Final"; }
};
int main() {
CuriousTab* ptr = new CuriousTab('B');
return 0;
}
Assume no delete is performed.
Open
View answer
C++ (dynamic allocation and character arithmetic) — determine the printed value from a constructor that sums an int and a character code.
#include<iostream.h>
class CuriousTab {
int *p;
public:
CuriousTab(int xx, char ch) {
p = new int();
*p = xx + int(ch);
cout << *p;
}
~CuriousTab() { delete p; }
};
int main() {
CuriousTab objCuriousTab(10, 'B');
return 0;
}
What value is printed?
Open
View answer
C++ (const member function) — does a const-qualified Show() print the initialized member value?
#include<iostream.h>
class Tab {
int x;
public:
Tab();
void Show() const;
~Tab() {}
};
Tab::Tab() { x = 5; }
void Tab::Show() const { cout << x; }
int main() {
Tab objB; objB.Show();
return 0;
}
Predict the output.
Open
View answer
C++ (constructor/destructor calls and manual destructor invocation) — what is printed when explicitly calling a destructor on an automatic object?
#include<iostream.h>
class CuriousTabBase { public: CuriousTabBase(){ cout << "Base OK. "; } };
class CuriousTabDerived: public CuriousTabBase {
public:
CuriousTabDerived(){ cout << "Derived OK. "; }
~CuriousTabDerived(){ cout << "Derived DEL. "; }
};
int main(){
CuriousTabBase objB; // base object
CuriousTabDerived objD; // derived object
objD.~CuriousTabDerived(); // explicit destructor call (dangerous)
return 0;
}
Choose the observed output.
Open
View answer
C++ (inheritance, default arguments, and overrides) — track member initialization and outputs with pre/post operators.
#include<iostream.h>
class CuriousTabBase {
public:
int x, y;
CuriousTabBase(int xx = 0, int yy = 5) { x = ++xx; y = --yy; }
void Display() { cout << --y; }
~CuriousTabBase() {}
};
class CuriousTabDerived : public CuriousTabBase {
public:
void Increment() { y++; }
void Display() { cout << --y; }
};
int main(){
CuriousTabDerived objCuriousTab; // uses base default ctor
objCuriousTab.Increment();
objCuriousTab.Display();
return 0;
}
What is printed?
Open
View answer
C++ (missing destructor definition) — will this program link and run?
#include<iostream.h>
class Tab {
int x;
public:
Tab();
~Tab();
void Show() const;
};
Tab::Tab() { x = 25; }
void Tab::Show() const { cout << x; }
int main(){ Tab objB; objB.Show(); return 0; }
Choose the correct statement.
Open
View answer
C++ (overload resolution with char literal among short/int/float constructors) — which message is printed?
#include<iostream.h>
class CuriousTab {
int x;
public:
CuriousTab(short) { cout << "Short" << endl; }
CuriousTab(int) { cout << "Int" << endl; }
CuriousTab(float) { cout << "Float" << endl; }
~CuriousTab() { cout << "Final"; }
};
int main(){ CuriousTab* ptr = new CuriousTab('B'); return 0; }
Assume there is no delete.
Open
View answer
C++ (multiple inheritance-like layout: base subobject vs contained member) — trace which x is printed from a derived object with both a base x and a contained base instance.
#include<iostream.h>
class CuriousTabBase {
public:
int x, y;
CuriousTabBase(int xx = 0, int yy = 0) { x = xx; y = yy; }
};
class CuriousTabDerived : public CuriousTabBase {
private:
CuriousTabBase objBase; // contained, separate instance
public:
CuriousTabDerived(int xx, int yy)
: CuriousTabBase(xx), objBase(yy) {
cout << x << " "
<< this->x << " "
<< CuriousTabBase::x << " "
<< this->objBase.x;
}
};
int main(){ CuriousTabDerived objDev(11, 22); return 0; }
What is printed?
Open
View answer
C++ (copy construction and copy initialization) — do all three objects hold the same value and print identically?
#include<iostream.h>
class CuriousTab {
int x;
public:
CuriousTab() { x = 0; }
CuriousTab(int xx) { x = xx; }
CuriousTab(CuriousTab &objB) { x = objB.x; }
void Display() { cout << x << " "; }
};
int main(){
CuriousTab objA(25);
CuriousTab objB(objA); // copy constructor
CuriousTab objC = objA; // copy initialization → copy constructor
objA.Display(); objB.Display(); objC.Display();
return 0;
}
Choose the correct statement.
Open
View answer
In C++ (old iostream.h style), predict the exact output including the destructor print when member x is altered via pointer re-interpretation.
#include<iostream.h>
class CuriousTab {
int x, y;
public:
CuriousTab(int xx) { x = ++xx; }
~CuriousTab() { cout << x - 1 << " "; }
void Display() { cout << --x + 1 << " "; }
};
int main() {
CuriousTab objCuriousTab(5);
objCuriousTab.Display();
int p = (int) &objCuriousTab; // write into first data member (x)
*p = 40; // force x = 40
objCuriousTab.Display(); // destructor runs after main and also prints
return 0;
}
Open
View answer
C++ virtual destructor behavior (legacy iostream.h): when deleting via a base-class pointer, in what order do constructor and destructor messages appear?
#include<iostream.h>
class CuriousTabBase{ public: CuriousTabBase(){ cout << "Base OK. "; } virtual ~CuriousTabBase(){ cout << "Base DEL. "; } };
class CuriousTabDerived: public CuriousTabBase{ public: CuriousTabDerived(){ cout << "Derived OK. "; } ~CuriousTabDerived(){ cout << "Derived DEL. "; } };
int main(){ CuriousTabBase basePtr = new CuriousTabDerived(); delete basePtr; }
Open
View answer
C++ pointer-based “copy constructor” overload: what will this program print when constructing from a heap object pointer?
#include<iostream.h>
class CuriousTab{
int x, y;
public:
CuriousTab(){ x = 0; y = 0; }
CuriousTab(int xx, int yy){ x = xx; y = yy; }
CuriousTab(CuriousTab objB){ x = objB->x; y = objB->y; } // nonstandard pointer-taking ctor
void Display(){ cout << x << " " << y; }
};
int main(){ CuriousTab objCuriousTab( new CuriousTab(20, 40) ); objCuriousTab.Display(); }
Open
View answer
C++ composition and access: given a contained base-like object and this-> usage, what product does Show() print when constructed as shown?
#include<iostream.h>
class CuriousTabBase{ protected: int x, y; public: CuriousTabBase(int xx=0,int yy=0){ x=xx; y=yy; } void Show(){ cout << x * this->y << endl; } };
class CuriousTabDerived{ private: CuriousTabBase objBase; public: CuriousTabDerived(int xx,int yy): objBase(xx,yy){ objBase.Show(); } };
int main(){ CuriousTabDerived objDev(10,20); }
Open
View answer
C++ which constructor is invoked here? One constructor has default arguments allowing zero-argument construction.
#include<iostream.h>
class CuriousTab{
int x, y;
public:
CuriousTab(int xx=10, int yy=20){ x=xx; y=yy; }
void Display(){ cout << x << " " << y << endl; }
};
int main(){ CuriousTab objCuriousTab; objCuriousTab.Display(); }
Open
View answer
C++ terminology check: in this class, what is the technical term for ~CuriousTab() ?
#include<iostream.h>
class CuriousTab{
int x, y;
public:
CuriousTab(int xx=10,int yy=20){ x=xx; y=yy; }
void Display(){ cout << x << " " << y << endl; }
~CuriousTab(){ }
};
int main(){ CuriousTab obj; obj.Display(); }
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.