Bird
Raised Fist0
C++programming~20 mins

Object creation and destruction flow in C++ - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following statements about object creation and destruction in C++ is correct?
easy
A. Constructors run only when an object is destroyed.
B. Destructors are called before an object is created.
C. Constructors are called automatically when an object is created.
D. Destructors run only when an object is created.

Solution

  1. Step 1: Understand constructor timing

    Constructors are special functions that run automatically when an object is created to initialize it.
  2. Step 2: Understand destructor timing

    Destructors run when an object is destroyed or goes out of scope, not before creation.
  3. Final Answer:

    Constructors are called automatically when an object is created. -> Option C
  4. Quick Check:

    Constructor = on creation [OK]
Hint: Remember: constructor = create, destructor = destroy [OK]
Common Mistakes:
  • Confusing when constructors and destructors run
  • Thinking destructors run before creation
  • Believing constructors run on destruction
2. Which of the following is the correct syntax to declare a destructor for a class named Car in C++?
easy
A. ~Car();
B. Car();
C. void ~Car();
D. destructor Car();

Solution

  1. Step 1: Recall destructor syntax

    In C++, a destructor is declared with a tilde (~) followed by the class name and parentheses, no return type.
  2. Step 2: Check options

    ~Car(); matches the correct syntax: ~Car();. Others are invalid syntax.
  3. Final Answer:

    ~Car(); -> Option A
  4. Quick Check:

    Destructor syntax = ~ClassName() [OK]
Hint: Destructor always starts with ~ and no return type [OK]
Common Mistakes:
  • Adding return type to destructor
  • Using wrong keywords like 'destructor'
  • Confusing constructor syntax with destructor
3. Consider the following C++ code:
class Test {
public:
  Test() { std::cout << "C"; }
  ~Test() { std::cout << "D"; }
};

int main() {
  Test t1;
  {
    Test t2;
  }
  std::cout << "E";
  return 0;
}

What is the output when this program runs?
medium
A. CCDEE
B. CCDED
C. CDEC
D. CCED

Solution

  1. Step 1: Trace object creation

    t1 is created first, prints 'C'. Then t2 is created inside inner block, prints another 'C'.
  2. Step 2: Trace object destruction and output

    When inner block ends, t2 is destroyed, prints 'D'. Then 'E' is printed. Finally, t1 is destroyed at program end, prints 'D'.
  3. Final Answer:

    CCDED -> Option B
  4. Quick Check:

    Creation C twice, destruction D twice, E in middle [OK]
Hint: Destructors run when objects go out of scope [OK]
Common Mistakes:
  • Forgetting destructor runs at block end
  • Mixing order of destruction
  • Ignoring output inside main
4. Identify the error in the following C++ code snippet related to object destruction:
class Sample {
public:
  Sample() { std::cout << "Created"; }
  void ~Sample() { std::cout << "Destroyed"; }
};

int main() {
  Sample s;
  return 0;
}
medium
A. Destructor has a return type which is not allowed.
B. Destructor name is incorrect; it should be 'destruct'.
C. Constructor is missing a return type.
D. Destructor should be public static.

Solution

  1. Step 1: Check destructor declaration

    Destructor must not have a return type. Here, 'void' before '~Sample()' is invalid syntax.
  2. Step 2: Verify other options

    Destructor name is correct with '~Sample'. Constructors have no return type but that is correct here. Destructors cannot be static.
  3. Final Answer:

    Destructor has a return type which is not allowed. -> Option A
  4. Quick Check:

    Destructor = no return type [OK]
Hint: Destructor never has a return type, not even void [OK]
Common Mistakes:
  • Adding void or other return types to destructor
  • Misnaming destructor
  • Making destructor static
5. You have a class Resource that opens a file in its constructor and closes it in its destructor. Which of the following code snippets correctly ensures the file is closed automatically when the object goes out of scope?
hard
A. void useFile() { Resource* r = new Resource(); // use file delete r; } // file closed here automatically
B. void useFile() { Resource* r = new Resource(); // use file } // file closed here automatically
C. void useFile() { Resource r; // use file delete &r; } // file closed here
D. void useFile() { Resource r; // use file } // file closed here automatically

Solution

  1. Step 1: Understand automatic destruction

    Objects created on stack (like 'Resource r;') are destroyed automatically when going out of scope, calling destructor to close file.
  2. Step 2: Analyze dynamic allocation

    Objects created with 'new' must be deleted manually; otherwise destructor won't run automatically, risking resource leaks.
  3. Final Answer:

    void useFile() { Resource r; // use file } // file closed here automatically -> Option D
  4. Quick Check:

    Stack objects auto-destroyed, heap objects need delete [OK]
Hint: Use stack objects for automatic destructor calls [OK]
Common Mistakes:
  • Forgetting to delete heap objects
  • Trying to delete stack objects
  • Assuming destructor runs without delete on heap
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard