Bird
Raised Fist0
C++programming~20 mins

delete operator in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Delete Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of deleting a dynamically allocated int
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int* p = new int(10);
    std::cout << *p << std::endl;
    delete p;
    // std::cout << *p << std::endl; // Uncommenting this causes undefined behavior
    std::cout << "Deleted" << std::endl;
    return 0;
}
A
Runtime error
B
Deleted
10
C
10
Deleted
D
Compilation error
Attempts:
2 left
💡 Hint
Remember that deleting a pointer frees memory but does not print anything by itself.
❓ Predict Output
intermediate
2:00remaining
Effect of deleting an array allocated with new[]
What is the output of this C++ code?
C++
#include <iostream>
int main() {
    int* arr = new int[3]{1, 2, 3};
    delete arr;
    std::cout << arr[0] << std::endl;
    return 0;
}
A
Runtime error or undefined behavior
B
Compilation error
C
1
D
0
Attempts:
2 left
💡 Hint
Deleting an array allocated with new[] requires a special delete syntax.
❓ Predict Output
advanced
2:00remaining
Output when deleting a nullptr pointer
What happens when you delete a nullptr pointer in C++?
C++
#include <iostream>
int main() {
    int* p = nullptr;
    delete p;
    std::cout << "Deleted nullptr" << std::endl;
    return 0;
}
A
Compilation error
B
Runtime error
C
No output
D
Deleted nullptr
Attempts:
2 left
💡 Hint
Deleting nullptr is safe and does nothing.
❓ Predict Output
advanced
2:00remaining
Output of deleting a pointer twice
What is the output or behavior of this code?
C++
#include <iostream>
int main() {
    int* p = new int(5);
    delete p;
    delete p;
    std::cout << "Done" << std::endl;
    return 0;
}
A
Runtime error (double free)
B
Done
C
Compilation error
D
Undefined output
Attempts:
2 left
💡 Hint
Deleting the same pointer twice causes a runtime error.
🧠 Conceptual
expert
3:00remaining
Which delete operator call is correct for this class?
Given the class below, which delete statement correctly frees the allocated memory without causing undefined behavior?
C++
class MyClass {
public:
    int* data;
    MyClass() { data = new int[5]; }
    ~MyClass() { delete[] data; }
};

int main() {
    MyClass* obj = new MyClass();
    // Which delete is correct here?
    // A) delete obj->data;
    // B) delete[] obj->data;
    // C) delete[] obj;
    // D) delete obj;
    return 0;
}
Adelete[] obj->data;
Bdelete obj;
Cdelete[] obj;
Ddelete obj->data;
Attempts:
2 left
💡 Hint
Deleting the object calls its destructor which deletes the array properly.

Practice

(1/5)
1. What is the main purpose of the delete operator in C++?
easy
A. To allocate new memory
B. To free memory allocated with new
C. To copy objects
D. To declare variables

Solution

  1. Step 1: Understand memory allocation

    The new operator allocates memory dynamically on the heap.
  2. Step 2: Role of delete

    The delete operator frees that allocated memory to avoid leaks.
  3. Final Answer:

    To free memory allocated with new -> Option B
  4. Quick Check:

    delete frees memory [OK]
Hint: Remember: new allocates, delete frees memory [OK]
Common Mistakes:
  • Confusing delete with new
  • Using delete to copy objects
  • Thinking delete declares variables
2. Which of the following is the correct syntax to delete a dynamically allocated array arr?
easy
A. delete arr;
B. delete array arr;
C. delete arr[];
D. delete[] arr;

Solution

  1. Step 1: Identify array deletion syntax

    For arrays allocated with new[], use delete[] to free all elements properly.
  2. Step 2: Match correct syntax

    delete[] arr; correctly deletes the entire array.
  3. Final Answer:

    delete[] arr; -> Option D
  4. Quick Check:

    Use delete[] for arrays [OK]
Hint: Use delete[] for arrays, delete for single objects [OK]
Common Mistakes:
  • Using delete instead of delete[] for arrays
  • Placing brackets after variable name incorrectly
  • Writing delete array arr (invalid syntax)
3. What will be the output of the following code?
int* p = new int(10);
std::cout << *p << std::endl;
delete p;
// std::cout << *p << std::endl; // Uncommenting this line causes what?
medium
A. 10 and then 10 again
B. Compilation error at delete
C. 10 and then undefined behavior
D. Runtime error at new

Solution

  1. Step 1: Analyze output before delete

    The pointer p points to an int with value 10, so *p prints 10.
  2. Step 2: Effect of delete and accessing pointer

    After delete p;, the memory is freed. Accessing *p after delete causes undefined behavior (may crash or print garbage).
  3. Final Answer:

    10 and then undefined behavior -> Option C
  4. Quick Check:

    Access after delete = undefined behavior [OK]
Hint: Don't use pointer after delete; it's unsafe [OK]
Common Mistakes:
  • Assuming deleted pointer still holds valid data
  • Expecting compilation error on delete
  • Ignoring undefined behavior risks
4. Identify the error in this code snippet:
int* arr = new int[5];
delete arr;
medium
A. Should use delete[] instead of delete
B. Should use delete without brackets
C. No error, code is correct
D. Should not delete dynamically allocated arrays

Solution

  1. Step 1: Check allocation type

    arr is allocated with new int[5], which creates an array.
  2. Step 2: Correct deletion method

    Arrays must be deleted with delete[] arr; to properly free all elements.
  3. Final Answer:

    Should use delete[] instead of delete -> Option A
  4. Quick Check:

    Array delete needs delete[] [OK]
Hint: Match new[] with delete[] always [OK]
Common Mistakes:
  • Using delete for arrays causes undefined behavior
  • Thinking delete works for both single and arrays
  • Ignoring memory leaks from wrong delete
5. Consider this code:
int** matrix = new int*[3];
for (int i = 0; i < 3; ++i) {
    matrix[i] = new int[4];
}
// Code to delete matrix properly?
Which is the correct way to delete all allocated memory?
hard
A. for (int i = 0; i < 3; ++i) delete[] matrix[i]; delete[] matrix;
B. for (int i = 0; i < 3; ++i) delete matrix[i]; delete[] matrix;
C. delete[] matrix; delete[] matrix[0];
D. delete matrix; delete matrix[0];

Solution

  1. Step 1: Understand allocation

    Each matrix[i] is an array allocated with new int[4], so must be deleted with delete[].
  2. Step 2: Delete outer array

    The matrix pointer itself is an array of pointers allocated with new int*[3], so delete with delete[] matrix;.
  3. Final Answer:

    for (int i = 0; i < 3; ++i) delete[] matrix[i]; delete[] matrix; -> Option A
  4. Quick Check:

    Match new[] with delete[] at all levels [OK]
Hint: Delete inner arrays with delete[], then outer with delete[] [OK]
Common Mistakes:
  • Using delete instead of delete[] for inner arrays
  • Deleting outer array with delete instead of delete[]
  • Forgetting to delete inner arrays causing leaks
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