Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to allocate an integer dynamically.
C++
int* ptr = [1] int; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
malloc instead of new in C++.Using
delete to allocate memory.✗ Incorrect
In C++, new is used to allocate memory dynamically.
2fill in blank
mediumComplete the code to free the dynamically allocated memory.
C++
delete [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
delete *ptr which deletes the value, not the memory.Using
delete ptr() which is invalid syntax.✗ Incorrect
To free memory allocated with new, use delete followed by the pointer variable.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for array size.
Using angle brackets which are for templates.
✗ Incorrect
To allocate an array dynamically, use square brackets [] with the size.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
delete instead of delete[] for arrays.Using parentheses instead of square brackets for allocation.
✗ Incorrect
Use [] to allocate an array and delete[] to free it.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
delete instead of delete[] for arrays.Inconsistent pointer variable names.
✗ Incorrect
Declare pointer arr, allocate array with [], and free with delete[].