Curioustab
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Constructors and Destructors Questions
C++ object lifetime: constructor prints "Curious", destructor prints "Tab" — what is the output? #include
class CuriousTab { public: CuriousTab() { cout << "Curious"; } ~CuriousTab() { cout << "Tab"; } }; int main() { CuriousTab objTab; // automatic object return 0; // destructor runs at scope end }
C++ deleting a derived object via base pointer without a virtual destructor: what sequence prints? #include
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; }
C++ constructor prints a character from a float via char(yy): what appears on the console? #include
class CuriousTab { int x; public: CuriousTab(int xx, float yy) { cout << char(yy); } }; int main() { CuriousTab *p = new CuriousTab(35, 99.50f); return 0; }
C++ (constructor/destructor order) — determine the exact output produced by construction of multiple objects and their destruction including a nested scope. #include
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?
C++ (constructor overload resolution) — which constructor is called for a character literal created via new, and what is printed? #include
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.
C++ (dynamic allocation and character arithmetic) — determine the printed value from a constructor that sums an int and a character code. #include
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?
C++ (const member function) — does a const-qualified Show() print the initialized member value? #include
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.
C++ (constructor/destructor calls and manual destructor invocation) — what is printed when explicitly calling a destructor on an automatic object? #include
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.
C++ (inheritance, default arguments, and overrides) — track member initialization and outputs with pre/post operators. #include
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?
C++ (missing destructor definition) — will this program link and run? #include
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.
C++ (overload resolution with char literal among short/int/float constructors) — which message is printed? #include
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.
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
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?
C++ (copy construction and copy initialization) — do all three objects hold the same value and print identically? #include
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.
In C++ (old iostream.h style), predict the exact output including the destructor print when member x is altered via pointer re-interpretation. #include
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; }
C++ virtual destructor behavior (legacy iostream.h): when deleting via a base-class pointer, in what order do constructor and destructor messages appear? #include
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; }
C++ pointer-based “copy constructor” overload: what will this program print when constructing from a heap object pointer? #include
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(); }
C++ composition and access: given a contained base-like object and this-> usage, what product does Show() print when constructed as shown? #include
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); }
C++ which constructor is invoked here? One constructor has default arguments allowing zero-argument construction. #include
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(); }
C++ terminology check: in this class, what is the technical term for ~CuriousTab() ? #include
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(); }
1
2
3