0
0
C++programming~10 mins

delete operator in C++ - Interactive Code Practice

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

Complete the code to correctly delete the dynamically allocated integer.

C++
#include <iostream>
int main() {
    int* ptr = new int(10);
    [1] ptr;
    return 0;
}
Drag options to blanks, or click blank then click option'
Afree
Bdispose
Cremove
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using free() instead of delete for memory allocated with new.
Trying to use remove or dispose which are not C++ operators.
2fill in blank
medium

Complete the code to delete a dynamically allocated array.

C++
#include <iostream>
int main() {
    int* arr = new int[5];
    [1] arr;
    return 0;
}
Drag options to blanks, or click blank then click option'
Adelete[]
Bfree
Cdelete
Ddispose
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete instead of delete[] for arrays.
Using free() which is for C-style allocation.
3fill in blank
hard

Fix the error in the code by choosing the correct delete syntax.

C++
#include <iostream>
int main() {
    double* data = new double(3.14);
    [1] data;
    return 0;
}
Drag options to blanks, or click blank then click option'
Afree
Bdelete()
Cdelete
Ddelete[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete[] on a single object.
Trying to call delete as a function like delete().
4fill in blank
hard

Fill both blanks to correctly delete a dynamically allocated array and avoid memory leaks.

C++
#include <iostream>
int main() {
    char* letters = new char[10];
    [1] [2];
    return 0;
}
Drag options to blanks, or click blank then click option'
Adelete[]
Bdelete
Cletters
Dfree
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete instead of delete[] for arrays.
Forgetting to specify the pointer after delete.
5fill in blank
hard

Fill all three blanks to correctly delete a dynamically allocated object and set the pointer to nullptr.

C++
#include <iostream>
int main() {
    float* value = new float(5.5f);
    [1] [2];
    [3] = nullptr;
    return 0;
}
Drag options to blanks, or click blank then click option'
Adelete
Bvalue
Dptr
Attempts:
3 left
💡 Hint
Common Mistakes
Not setting the pointer to nullptr after deletion.
Using wrong pointer variable names.