Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to allocate memory for an integer using new.
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++.
Forgetting to use new keyword.
✗ Incorrect
In C++, new is used to allocate memory dynamically.
2fill in blank
mediumComplete the code to deallocate memory allocated for an integer pointer.
C++
delete [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using *ptr instead of ptr in delete.
Using ptr() which is invalid syntax.
✗ Incorrect
Use delete followed by the pointer variable to free memory.
3fill in blank
hardFix the error in the code to allocate an array of 5 integers.
C++
int* arr = [1] int[5];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc instead of new.
Writing new[] without the type.
✗ Incorrect
Use new with square brackets to allocate arrays: new int[5].
4fill in blank
hardFill both blanks to correctly deallocate the array allocated with new.
C++
delete[1] [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete without brackets for arrays.
Using parentheses instead of brackets.
✗ Incorrect
Use delete[] followed by the pointer to free arrays allocated with new[].
5fill in blank
hardFill all three blanks to allocate memory for a double, assign a value, and then deallocate it.
C++
double* [1] = [2] double; *[1] = 3.14; delete [3]; [1] = nullptr;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Forgetting to delete the allocated memory.
✗ Incorrect
First declare pointer ptr, allocate with new, assign value, then delete ptr.