0
0
C++programming~10 mins

Why dynamic memory is needed in C++ - Test Your Understanding

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

Complete the code to allocate an integer dynamically.

C++
int* ptr = [1] int;
Drag options to blanks, or click blank then click option'
Aauto
Bmalloc
Cdelete
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc instead of new in C++.
Using delete to allocate memory.
2fill in blank
medium

Complete the code to free the dynamically allocated memory.

C++
delete [1];
Drag options to blanks, or click blank then click option'
Aptr
B*ptr
Cptr()
D&ptr
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete *ptr which deletes the value, not the memory.
Using delete ptr() which is invalid syntax.
3fill in blank
hard

Fix the error in the code to allocate an array dynamically.

C++
int* arr = new int[1]10;
Drag options to blanks, or click blank then click option'
A{}
B()
C[]
D<>
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for array size.
Using angle brackets which are for templates.
4fill in blank
hard

Fill both blanks to create a dynamic array and delete it properly.

C++
int* arr = new int[1]5;
[2] arr;
Drag options to blanks, or click blank then click option'
A[]
Bdelete[]
Cdelete
D()
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete instead of delete[] for arrays.
Using parentheses instead of square brackets for allocation.
5fill in blank
hard

Fill all three blanks to allocate, use, and free dynamic memory correctly.

C++
int* [1] = new int[2]3;
[1][0] = 10;
[3] [1];
Drag options to blanks, or click blank then click option'
Aarr
B[]
Cdelete[]
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete instead of delete[] for arrays.
Inconsistent pointer variable names.