0
0
C++programming~10 mins

One-dimensional arrays 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 declare an array of 5 integers.

C++
int arr[[1]];
Drag options to blanks, or click blank then click option'
A5
B10
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as size which creates an empty array.
Forgetting to specify the size.
2fill in blank
medium

Complete the code to assign the value 10 to the first element of the array.

C++
arr[[1]] = 10;
Drag options to blanks, or click blank then click option'
A5
B0
C1
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as the first index.
Using negative indices which are invalid.
3fill in blank
hard

Fix the error in the code to correctly print the third element of the array.

C++
std::cout << arr[[1]] << std::endl;
Drag options to blanks, or click blank then click option'
A3
B0
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 3 which is the fourth element.
Using index 1 which is the second element.
4fill in blank
hard

Fill both blanks to create an array of 4 integers and assign 7 to its last element.

C++
int arr[[1]];
arr[[2]] = 7;
Drag options to blanks, or click blank then click option'
A4
B3
C5
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 4 which is out of bounds.
Using size 5 instead of 4.
5fill in blank
hard

Fill all three blanks to initialize an array with values 1, 2, 3 and print the second element.

C++
int arr[[1]] = {1, 2, 3};
std::cout << arr[[2]] << std::endl;
int size = [3];
Drag options to blanks, or click blank then click option'
A3
B1
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong size for the array.
Using wrong index to print the second element.