Challenge - 5 Problems
Object Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of constructor and destructor calls
What is the output of this C++ program when executed?
C++
#include <iostream> class Test { public: Test() { std::cout << "Constructor\n"; } ~Test() { std::cout << "Destructor\n"; } }; int main() { Test t1; { Test t2; } return 0; }
Attempts:
2 left
π‘ Hint
Remember that destructors are called when objects go out of scope.
β Incorrect
The first object t1 is created, printing 'Constructor'. Then inside the inner block, t2 is created, printing another 'Constructor'. When the inner block ends, t2 is destroyed, printing 'Destructor'. Finally, when main ends, t1 is destroyed, printing the last 'Destructor'.
π§ Conceptual
intermediate1:30remaining
Order of constructor calls in inheritance
Given a base class and a derived class, in which order are constructors called when creating a derived class object?
Attempts:
2 left
π‘ Hint
Think about how the base part of the object must be ready before the derived part.
β Incorrect
When creating a derived class object, the base class constructor runs first to initialize the base part, then the derived class constructor runs to initialize the derived part.
β Predict Output
advanced2:00remaining
Destructor call order in inheritance
What is the output of this program regarding destructor calls?
C++
#include <iostream> class Base { public: ~Base() { std::cout << "Base destructor\n"; } }; class Derived : public Base { public: ~Derived() { std::cout << "Derived destructor\n"; } }; int main() { Base* obj = new Derived(); delete obj; return 0; }
Attempts:
2 left
π‘ Hint
Check if the base class destructor is virtual or not.
β Incorrect
Because the base class destructor is not virtual, deleting a Derived object through a Base pointer calls only the Base destructor, causing a potential resource leak.
π Syntax
advanced1:30remaining
Identify the syntax error in constructor initialization
Which option contains the correct syntax for initializing member variables in a constructor initialization list?
C++
class Point { int x, y; public: Point(int a, int b) : x(a), y(b) {} };
Attempts:
2 left
π‘ Hint
Look carefully at the colon and commas in the initialization list.
β Incorrect
Option C correctly uses the constructor initialization list syntax with colon and commas. Option C uses assignment inside the constructor body, which is valid but not initialization list. Options C and D have syntax errors in the initialization list.
π Application
expert2:30remaining
Predict object lifetime and output with dynamic allocation and scope
What is the output of this program considering object creation and destruction flow?
C++
#include <iostream> class Demo { public: Demo() { std::cout << "Created\n"; } ~Demo() { std::cout << "Destroyed\n"; } }; int main() { Demo* d1 = new Demo(); { Demo d2; Demo* d3 = new Demo(); delete d3; } delete d1; return 0; }
Attempts:
2 left
π‘ Hint
Remember when destructors are called for stack and heap objects.
β Incorrect
d1 is created on heap (Created), then d2 on stack (Created), then d3 on heap (Created). d3 is deleted inside the block (Destroyed), then block ends and d2 is destroyed (Destroyed), finally d1 is deleted (Destroyed). Total 3 creations and 3 destructions in order.