0
0
C++programming~20 mins

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

Choose your learning style9 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.