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 regarding constructor and destructor calls?
C++
#include <iostream> class Test { public: Test() { std::cout << "Constructed "; } ~Test() { std::cout << "Destructed "; } }; 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 constructed, then inside the inner block t2 is constructed. When the inner block ends, t2 is destructed. Finally, when main ends, t1 is destructed.
β Predict Output
intermediate2:00remaining
Output of copy constructor and assignment operator
What is the output of this C++ program that uses copy constructor and assignment operator?
C++
#include <iostream> class Sample { public: Sample() { std::cout << "Default "; } Sample(const Sample&) { std::cout << "Copy "; } Sample& operator=(const Sample&) { std::cout << "Assign "; return *this; } }; int main() { Sample a; Sample b = a; Sample c; c = a; return 0; }
Attempts:
2 left
π‘ Hint
Copy constructor is called during initialization, assignment operator during assignment.
β Incorrect
Object a is created with default constructor. Object b is initialized from a, so copy constructor is called. Object c is default constructed, then assigned from a, so assignment operator is called.
π§ Debug
advanced2:00remaining
Identify the error in object lifecycle management
What error does this C++ code produce related to object lifecycle?
C++
#include <iostream> class Demo { public: Demo() { std::cout << "Created "; } ~Demo() { std::cout << "Destroyed "; } }; int main() { Demo* d = new Demo(); // Missing delete return 0; }
Attempts:
2 left
π‘ Hint
Think about what happens when you use new without delete.
β Incorrect
The object created with new is never deleted, so its destructor is never called and memory is leaked.
π Syntax
advanced2:00remaining
Identify the syntax error in constructor initialization
Which option contains the correct constructor initialization syntax for member variable 'x'?
C++
class Point { int x; public: Point(int val) : x(val) {} };
Attempts:
2 left
π‘ Hint
Constructor initialization list uses colon and parentheses without assignment.
β Incorrect
Option A uses correct member initializer list syntax. Option A is valid but uses assignment inside constructor body, not initialization list. Option A and D have syntax errors.
π Application
expert3:00remaining
Number of times destructor is called in complex object lifecycle
How many times is the destructor called when this program runs?
C++
#include <iostream> class Obj { public: Obj() { std::cout << "C"; } ~Obj() { std::cout << "D"; } }; Obj globalObj; int main() { Obj localObj1; { Obj localObj2; Obj localObj3 = localObj2; } return 0; }
Attempts:
2 left
π‘ Hint
Count all objects created and when they go out of scope, including global.
β Incorrect
Objects created: globalObj (1), localObj1 (2), localObj2 (3), localObj3 (4, copy of localObj2). Destructors called for localObj3 and localObj2 at inner block end, localObj1 at main end, and globalObj at program end. Total 4 destructor calls.