0
0
C++programming~20 mins

Object creation and destruction flow 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 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;
}
A
Constructor
Constructor
Destructor
Destructor
B
Constructor
Destructor
Constructor
Destructor
C
Constructor
Destructor
Destructor
Constructor
D
Constructor
Constructor
Destructor
Attempts:
2 left
πŸ’‘ Hint
Remember that destructors are called when objects go out of scope.
🧠 Conceptual
intermediate
1: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?
AOnly derived class constructor is called
BDerived class constructor first, then base class constructor
CBoth constructors called simultaneously
DBase class constructor first, then derived class constructor
Attempts:
2 left
πŸ’‘ Hint
Think about how the base part of the object must be ready before the derived part.
❓ Predict Output
advanced
2: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;
}
A
Only Base destructor
B
Base destructor
Derived destructor
C
Derived destructor
Base destructor
DNo output
Attempts:
2 left
πŸ’‘ Hint
Check if the base class destructor is virtual or not.
πŸ“ Syntax
advanced
1: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) {}
};
APoint(int a, int b) { x = a; y = b; }
BPoint(int a, int b) : x = a, y = b {}
CPoint(int a, int b) : x(a), y(b) {}
DPoint(int a, int b) : (x(a), y(b)) {}
Attempts:
2 left
πŸ’‘ Hint
Look carefully at the colon and commas in the initialization list.
πŸš€ Application
expert
2: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;
}
A
Created
Destroyed
Created
Destroyed
Destroyed
B
Created
Created
Destroyed
Destroyed
Destroyed
C
Created
Created
Destroyed
Destroyed
D
Created
Created
Destroyed
Created
Destroyed
Attempts:
2 left
πŸ’‘ Hint
Remember when destructors are called for stack and heap objects.