Bird
Raised Fist0
C++programming~20 mins

Destructor role in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Destructor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this destructor example?

Consider the following C++ code. What will be printed when the program runs?

C++
#include <iostream>
using namespace std;

class Box {
public:
    Box() { cout << "Box created\n"; }
    ~Box() { cout << "Box destroyed\n"; }
};

int main() {
    Box b;
    cout << "Inside main\n";
    return 0;
}
A
Box destroyed
Box created
Inside main
B
Box created
Box destroyed
Inside main
C
Inside main
Box created
Box destroyed
D
Box created
Inside main
Box destroyed
Attempts:
2 left
πŸ’‘ Hint

Think about when constructors and destructors are called in the program flow.

❓ Predict Output
intermediate
2:00remaining
What happens when a destructor frees memory?

Look at this code that uses a destructor to free memory. What is the output?

C++
#include <iostream>
using namespace std;

class ArrayHolder {
    int* arr;
public:
    ArrayHolder(int size) { arr = new int[size]; cout << "Array allocated\n"; }
    ~ArrayHolder() { delete[] arr; cout << "Array freed\n"; }
};

int main() {
    ArrayHolder a(5);
    cout << "Working with array\n";
    return 0;
}
A
Array allocated
Working with array
Array freed
B
Working with array
Array allocated
Array freed
C
Array allocated
Array freed
Working with array
D
Array freed
Array allocated
Working with array
Attempts:
2 left
πŸ’‘ Hint

Remember the order of constructor and destructor calls for local objects.

❓ Predict Output
advanced
2:00remaining
What is the output when a destructor is called for a dynamically allocated object?

Analyze this code. What will be printed when it runs?

C++
#include <iostream>
using namespace std;

class Sample {
public:
    Sample() { cout << "Sample created\n"; }
    ~Sample() { cout << "Sample destroyed\n"; }
};

int main() {
    Sample* s = new Sample();
    cout << "Pointer created\n";
    delete s;
    cout << "Pointer deleted\n";
    return 0;
}
A
Sample created
Pointer created
Sample destroyed
Pointer deleted
B
Pointer created
Sample created
Sample destroyed
Pointer deleted
C
Sample created
Sample destroyed
Pointer created
Pointer deleted
D
Sample created
Pointer created
Pointer deleted
Sample destroyed
Attempts:
2 left
πŸ’‘ Hint

Think about when the destructor runs for objects created with new.

❓ Predict Output
advanced
2:00remaining
What error occurs if destructor tries to delete a null pointer?

Consider this code where the destructor deletes a pointer that might be null. What happens when it runs?

C++
#include <iostream>
using namespace std;

class Holder {
    int* ptr;
public:
    Holder() : ptr(nullptr) { cout << "Holder created\n"; }
    ~Holder() { delete ptr; cout << "Holder destroyed\n"; }
};

int main() {
    Holder h;
    cout << "Main ends\n";
    return 0;
}
A
Holder created
Holder destroyed
Main ends
B
Holder created
Main ends
Holder destroyed
C
Holder created
Main ends
Segmentation fault
D
Compilation error due to deleting nullptr
Attempts:
2 left
πŸ’‘ Hint

Deleting a null pointer is safe in C++.

🧠 Conceptual
expert
1:30remaining
What is the primary role of a destructor in C++?

Choose the best description of what a destructor does in C++.

AIt initializes an object when it is created.
BIt copies data from one object to another.
CIt frees resources and performs cleanup when an object is destroyed.
DIt overloads operators for the class.
Attempts:
1 left
πŸ’‘ Hint

Think about what happens automatically when an object goes out of scope.

Practice

(1/5)
1. What is the main role of a destructor in C++?
easy
A. To clean up resources when an object is destroyed
B. To create new objects
C. To initialize variables
D. To overload operators

Solution

  1. Step 1: Understand the purpose of a destructor

    A destructor is a special function that runs when an object is destroyed to free resources like memory or files.
  2. Step 2: Compare with other options

    Creating objects, initializing variables, and overloading operators are not roles of destructors.
  3. Final Answer:

    To clean up resources when an object is destroyed -> Option A
  4. Quick Check:

    Destructor role = cleanup [OK]
Hint: Destructor always cleans up when object ends [OK]
Common Mistakes:
  • Confusing destructor with constructor
  • Thinking destructor creates objects
  • Believing destructor initializes variables
2. Which of the following is the correct syntax for a destructor in a class named Car?
easy
A. Car~() {}
B. void ~Car() {}
C. ~Car() {}
D. void Car() {}

Solution

  1. Step 1: Recall destructor syntax

    A destructor has the same name as the class with a tilde (~) prefix and no return type.
  2. Step 2: Check each option

    ~Car() {} matches the correct syntax. void ~Car() {} wrongly includes 'void'. Car~() {} has wrong order. void Car() {} lacks tilde and looks like a constructor.
  3. Final Answer:

    ~Car() {} -> Option C
  4. Quick Check:

    Destructor syntax = ~ClassName() [OK]
Hint: Destructor name = ~ + class name, no return type [OK]
Common Mistakes:
  • Adding return type to destructor
  • Placing tilde after class name
  • Confusing destructor with constructor syntax
3. What will be the output of this C++ code?
class Box {
public:
  Box() { std::cout << "Created\n"; }
  ~Box() { std::cout << "Destroyed\n"; }
};

int main() {
  Box b;
  return 0;
}
medium
A. Destroyed\nCreated\n
B. Created\nDestroyed\n
C. Created\n
D. No output

Solution

  1. Step 1: Trace object creation

    When b is created, constructor prints "Created".
  2. Step 2: Trace object destruction

    At end of main(), b is destroyed, destructor prints "Destroyed".
  3. Final Answer:

    Created\nDestroyed\n -> Option B
  4. Quick Check:

    Constructor then destructor output [OK]
Hint: Constructor runs first, destructor runs last [OK]
Common Mistakes:
  • Assuming destructor runs before constructor
  • Expecting no output
  • Confusing order of prints
4. Identify the error in this destructor code:
class Sample {
public:
  ~Sample(int x) { }
};
medium
A. Destructor cannot have parameters
B. Destructor must return int
C. Destructor name should not have ~
D. Destructor must be static

Solution

  1. Step 1: Check destructor parameters

    Destructors cannot have parameters; they must have empty parentheses.
  2. Step 2: Verify other rules

    Destructor has no return type, must have ~ prefix, and cannot be static.
  3. Final Answer:

    Destructor cannot have parameters -> Option A
  4. Quick Check:

    Destructor params = none [OK]
Hint: Destructor always has empty parentheses [OK]
Common Mistakes:
  • Adding parameters to destructor
  • Adding return type
  • Removing ~ from destructor name
5. Consider a class FileHandler that opens a file in its constructor. Which is the best way to ensure the file is closed properly when the object is no longer needed?
hard
A. Do nothing; files close automatically when program ends
B. Manually call a close method before deleting the object
C. Rely on the operating system to close the file automatically
D. Write a destructor that closes the file

Solution

  1. Step 1: Understand resource management

    Files opened by an object should be closed when the object is destroyed to avoid resource leaks.
  2. Step 2: Use destructor for cleanup

    Destructor runs automatically when object ends, so closing the file there ensures proper cleanup.
  3. Final Answer:

    Write a destructor that closes the file -> Option D
  4. Quick Check:

    Destructor cleans resources automatically [OK]
Hint: Use destructor to free resources automatically [OK]
Common Mistakes:
  • Forgetting to close files causing leaks
  • Relying on manual close calls
  • Assuming OS always cleans up immediately
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