0
0
C++programming~20 mins

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

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