What is the syntax to declare an array of 5 integers in C++?
You declare it like this: int arr[5]; This creates an array named arr that can hold 5 integers.
Click to reveal answer
beginner
How do you access the third element of an array named arr?
You use the index 2 because counting starts at 0. So, arr[2] gives you the third element.
Click to reveal answer
intermediate
How can you find the size of a statically declared array arr in C++?
Use sizeof(arr) / sizeof(arr[0]). This divides the total bytes of the array by the bytes of one element to get the number of elements.
Click to reveal answer
intermediate
What is the difference between arr[i] and *(arr + i)?
Both access the element at index i. arr[i] is array indexing syntax, while *(arr + i) uses pointer arithmetic. They are equivalent.
Click to reveal answer
intermediate
How do you copy elements from one array to another in C++?
You can use a loop to copy each element one by one, like: for(int i = 0; i < size; i++) { dest[i] = src[i]; } Or use std::copy from the <algorithm> header.
Click to reveal answer
What is the index of the first element in a C++ array?
A0
B1
C-1
DDepends on the array size
✗ Incorrect
In C++, array indexing starts at 0, so the first element is at index 0.
Which expression correctly gives the number of elements in a statically declared array arr?
Asizeof(arr)
Bsizeof(arr) / sizeof(arr[0])
Csizeof(arr) * sizeof(arr[0])
Dsizeof(arr[0]) / sizeof(arr)
✗ Incorrect
Dividing the total size of the array by the size of one element gives the number of elements.
What does arr[3] mean in C++?
AThe fourth element of the array
BThe third element of the array
CThe element at index 2
DAn invalid syntax
✗ Incorrect
Indexing starts at 0, so arr[3] is the fourth element.
Which of the following copies elements from one array to another correctly?
Acopy(dest, src);
Bdest = src;
Cdest.copy(src);
Dfor(int i=0; i<size; i++) dest[i] = src[i];
✗ Incorrect
Arrays cannot be assigned directly; you must copy elements one by one or use std::copy.
What is the result of *(arr + 2)?
AThe second element of the array
BThe first element of the array
CThe third element of the array
DA pointer to the array
✗ Incorrect
Pointer arithmetic moves the pointer by 2 elements, so dereferencing gives the third element.
Explain how to declare, access, and find the size of a static array in C++.
Think about how memory size relates to element count.
You got /3 concepts.
Describe two ways to copy elements from one array to another in C++.
Remember arrays cannot be assigned directly.
You got /2 concepts.
Practice
(1/5)
1. What does the following C++ code do? int arr[5] = {1, 2, 3, 4, 5};
easy
A. Creates an array of 5 integers with values 1 to 5
B. Creates a vector of 5 integers with values 1 to 5
C. Creates a pointer to an integer array
D. Declares an uninitialized array of size 5
Solution
Step 1: Understand array declaration
The code declares an integer array named arr with size 5.
Step 2: Check initialization
The array is initialized with values 1, 2, 3, 4, 5 in order.
Final Answer:
Creates an array of 5 integers with values 1 to 5 -> Option A
Quick Check:
Array declaration with initialization = Creates array with values [OK]
Hint: Array size and values in braces means initialized array [OK]
Common Mistakes:
Confusing array with vector
Thinking it's a pointer declaration
Assuming array is uninitialized
2. Which of the following is the correct syntax to access the third element of an array named arr in C++?
easy
A. arr[3];
B. arr(3);
C. arr{3};
D. arr[2];
Solution
Step 1: Recall zero-based indexing
Arrays in C++ start at index 0, so the third element is at index 2.
Step 2: Identify correct syntax for access
Array elements are accessed using square brackets, so arr[2] is correct.
Final Answer:
arr[2]; -> Option D
Quick Check:
Third element index = 2, use square brackets [OK]
Hint: Remember arrays start at 0, so third element is index 2 [OK]
Common Mistakes:
Using parentheses instead of brackets
Using 1-based indexing
Using curly braces for access
3. What is the output of this C++ code?
int arr[4] = {10, 20, 30, 40};
int sum = 0;
for(int i = 0; i < 4; i++) {
sum += arr[i];
}
std::cout << sum;
medium
A. 100
B. 90
C. 40
D. 10
Solution
Step 1: Identify array elements and loop
The array has elements 10, 20, 30, 40 and the loop runs from i=0 to 3.
Step 2: Calculate sum of elements
sum = 10 + 20 + 30 + 40 = 100
Final Answer:
100 -> Option A
Quick Check:
Sum of all elements = 100 [OK]
Hint: Add all elements in loop to get sum [OK]
Common Mistakes:
Stopping loop early
Using wrong indices
Printing sum before loop
4. Find the error in this C++ code that tries to print all elements of an array:
int arr[3] = {5, 10, 15};
for(int i = 1; i <= 3; i++) {
std::cout << arr[i] << " ";
}
medium
A. Loop index starts at 1 instead of 0
B. Array size is incorrect
C. Using <= causes out-of-bounds access
D. Missing semicolon after cout statement
Solution
Step 1: Analyze loop indices
The loop runs i from 1 to 3 inclusive, so i=3 is included.
Step 2: Check array bounds
Array indices are 0,1,2. Accessing arr[3] is out-of-bounds and causes error.
Final Answer:
Using <= causes out-of-bounds access -> Option C
Quick Check:
Loop must use i < size, not <= [OK]
Hint: Use i < size, not i <= size in loops [OK]
Common Mistakes:
Starting loop at 1 instead of 0 (not error but unusual)
Ignoring out-of-bounds access
Assuming array size is wrong
5. You have an array int arr[6] = {2, 4, 6, 8, 10, 12};. How do you update the element with value 8 to 9 using a loop?