Bird
Raised Fist0
C++programming~10 mins

Destructor role in C++ - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a destructor in a C++ class.

C++
class MyClass {
public:
    ~[1]();
};
Drag options to blanks, or click blank then click option'
Adelete
BmyClass
CDestructor
DMyClass
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different name than the class for the destructor.
Omitting the tilde (~) before the destructor name.
2fill in blank
medium

Complete the code to define a destructor that prints a message when called.

C++
MyClass::~MyClass() {
    std::cout << [1] << std::endl;
}
Drag options to blanks, or click blank then click option'
ADestructor_called
BDestructor called
C"Destructor called"
Ddestructor
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Forgetting the quotes around the message string.
Using a variable name instead of a string literal.
3fill in blank
hard

Fix the error in the destructor declaration.

C++
class MyClass {
public:
    ~MyClass[1];
};
Drag options to blanks, or click blank then click option'
A{}
B()
C;
D[]
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Omitting parentheses in the destructor declaration.
Using braces {} instead of parentheses ().
4fill in blank
hard

Fill both blanks to complete the destructor definition that frees a dynamically allocated array.

C++
class MyClass {
    int* data;
public:
    MyClass() { data = new int[10]; }
    ~MyClass() {
        [1] data;
        data = [2];
    }
};
Drag options to blanks, or click blank then click option'
Adelete[]
Bnullptr
C0
Ddelete
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using delete instead of delete[] for arrays.
Not setting pointer to nullptr after deletion.
5fill in blank
hard

Fill all three blanks to complete the destructor that closes a file and resets the pointer.

C++
class FileHandler {
    FILE* file;
public:
    FileHandler(const char* filename) {
        file = fopen(filename, "r");
    }
    ~FileHandler() {
        if ([1] != nullptr) {
            [2](file);
            file = [3];
        }
    }
};
Drag options to blanks, or click blank then click option'
Afile
Bfclose
Cnullptr
Dopen
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Not checking if file pointer is nullptr before closing.
Using wrong function to close the file.
Not resetting pointer after closing.

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