0
0
C++programming~10 mins

new 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 allocate memory for an integer using the new operator.

C++
int* ptr = [1] int;
Drag options to blanks, or click blank then click option'
Anew
Bmalloc
Calloc
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc instead of new in C++.
Forgetting to use the new operator before the type.
2fill in blank
medium

Complete the code to allocate an array of 5 doubles using the new operator.

C++
double* arr = [1] double[5];
Drag options to blanks, or click blank then click option'
Anew
Bmalloc
Ccreate
Dalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc which is a C function, not recommended in C++.
Forgetting the brackets for array allocation.
3fill in blank
hard

Fix the error in the code to correctly allocate a single float using new.

C++
float* fptr = [1] float();
Drag options to blanks, or click blank then click option'
Amalloc
Balloc
Ccreate
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc which returns void* and requires casting.
Using incorrect keywords like create or alloc.
4fill in blank
hard

Fill both blanks to allocate and initialize an array of 3 integers with zeros using new.

C++
int* arr = [1] int[2];
Drag options to blanks, or click blank then click option'
Anew
B[3]{}
C()
D{}
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of brackets for arrays.
Forgetting to use new keyword.
5fill in blank
hard

Fill all three blanks to allocate an array of 4 chars and initialize them to zero using new.

C++
char* buffer = [1] char[2] [3];
Drag options to blanks, or click blank then click option'
Anew
B[4]
C()
D{}
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of braces for initialization.
Forgetting to use new or brackets.