Bird
Raised Fist0
C++programming~20 mins

Common array operations in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of array sum using std::accumulate
What is the output of the following C++ code?
C++
#include <iostream>
#include <numeric>
#include <array>

int main() {
    std::array<int, 5> arr = {1, 2, 3, 4, 5};
    int sum = std::accumulate(arr.begin(), arr.end(), 0);
    std::cout << sum << std::endl;
    return 0;
}
A15
B10
C0
DCompilation error
Attempts:
2 left
💡 Hint
std::accumulate adds all elements starting from the initial value.
❓ Predict Output
intermediate
2:00remaining
Output of array element access and modification
What is the output of this C++ code snippet?
C++
#include <iostream>
#include <array>

int main() {
    std::array<int, 3> arr = {10, 20, 30};
    arr[1] = arr[0] + arr[2];
    std::cout << arr[1] << std::endl;
    return 0;
}
A40
BRuntime error
C30
D20
Attempts:
2 left
💡 Hint
arr[1] is assigned the sum of arr[0] and arr[2].
❓ Predict Output
advanced
2:00remaining
Output of array reverse using std::reverse
What is the output of this C++ program?
C++
#include <iostream>
#include <array>
#include <algorithm>

int main() {
    std::array<int, 4> arr = {1, 2, 3, 4};
    std::reverse(arr.begin(), arr.end());
    for (int x : arr) {
        std::cout << x << " ";
    }
    return 0;
}
A1 2 3 4
BRuntime error
CCompilation error
D4 3 2 1
Attempts:
2 left
💡 Hint
std::reverse reverses the order of elements in the array.
❓ Predict Output
advanced
2:00remaining
Output of array fill and print
What does this C++ code print?
C++
#include <iostream>
#include <array>

int main() {
    std::array<int, 5> arr;
    arr.fill(7);
    for (auto val : arr) {
        std::cout << val << " ";
    }
    return 0;
}
ACompilation error
B0 0 0 0 0
C7 7 7 7 7
DRandom values
Attempts:
2 left
💡 Hint
The fill() method sets all elements to the given value.
🧠 Conceptual
expert
2:30remaining
Behavior of std::array size and data pointers
Consider the following C++ code snippet. What is the output?
C++
#include <iostream>
#include <array>

int main() {
    std::array<int, 3> arr = {5, 10, 15};
    std::cout << arr.size() << " " << *(arr.data() + 1) << std::endl;
    return 0;
}
A3 5
B3 10
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
arr.size() returns the number of elements; arr.data() returns pointer to first element.

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

  1. Step 1: Understand array declaration

    The code declares an integer array named arr with size 5.
  2. Step 2: Check initialization

    The array is initialized with values 1, 2, 3, 4, 5 in order.
  3. Final Answer:

    Creates an array of 5 integers with values 1 to 5 -> Option A
  4. 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

  1. Step 1: Recall zero-based indexing

    Arrays in C++ start at index 0, so the third element is at index 2.
  2. Step 2: Identify correct syntax for access

    Array elements are accessed using square brackets, so arr[2] is correct.
  3. Final Answer:

    arr[2]; -> Option D
  4. 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

  1. Step 1: Identify array elements and loop

    The array has elements 10, 20, 30, 40 and the loop runs from i=0 to 3.
  2. Step 2: Calculate sum of elements

    sum = 10 + 20 + 30 + 40 = 100
  3. Final Answer:

    100 -> Option A
  4. 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

  1. Step 1: Analyze loop indices

    The loop runs i from 1 to 3 inclusive, so i=3 is included.
  2. Step 2: Check array bounds

    Array indices are 0,1,2. Accessing arr[3] is out-of-bounds and causes error.
  3. Final Answer:

    Using <= causes out-of-bounds access -> Option C
  4. 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?
hard
A. arr[3] = 9;
B. for(int i=0; i<6; i++) { if(arr[i] == 8) arr[i] = 9; }
C. for(int i=1; i<=6; i++) { if(arr[i] == 8) arr[i] = 9; }
D. arr[4] = 9;

Solution

  1. Step 1: Understand the problem

    We want to find the element with value 8 and update it to 9 using a loop.
  2. Step 2: Check loop and condition

    Loop from i=0 to 5 checks each element; if arr[i] == 8, assign arr[i] = 9.
  3. Final Answer:

    for(int i=0; i<6; i++) { if(arr[i] == 8) arr[i] = 9; } -> Option B
  4. Quick Check:

    Loop with condition to find and update element [OK]
Hint: Loop through array, check value, then update [OK]
Common Mistakes:
  • Using wrong indices in loop
  • Assigning wrong array index
  • Not using loop to find value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard