0
0
C++programming~20 mins

Object lifecycle in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Object Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
ADestructed Constructed Constructed Destructed
BConstructed Destructed Constructed Destructed
CConstructed Destructed Destructed Constructed
DConstructed Constructed Destructed Destructed
Attempts:
2 left
πŸ’‘ Hint
Remember that destructors are called when objects go out of scope.
❓ Predict Output
intermediate
2: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;
}
ADefault Default Copy Assign
BDefault Copy Default Assign
CCopy Default Assign Default
DDefault Assign Copy Default
Attempts:
2 left
πŸ’‘ Hint
Copy constructor is called during initialization, assignment operator during assignment.
πŸ”§ Debug
advanced
2: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;
}
AMemory leak due to missing delete
BDouble free error
CSegmentation fault
DNo error, program runs fine
Attempts:
2 left
πŸ’‘ Hint
Think about what happens when you use new without delete.
πŸ“ Syntax
advanced
2: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) {}
};
APoint(int val) : x(val) {}
BPoint(int val) { x = val; }
CPoint(int val) : x = val {}
DPoint(int val) { x(val); }
Attempts:
2 left
πŸ’‘ Hint
Constructor initialization list uses colon and parentheses without assignment.
πŸš€ Application
expert
3: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;
}
A6
B3
C4
D5
Attempts:
2 left
πŸ’‘ Hint
Count all objects created and when they go out of scope, including global.