Challenge - 5 Problems
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
std::accumulate adds all elements starting from the initial value.
✗ Incorrect
The std::accumulate function sums all elements in the array starting from 0, so 1+2+3+4+5 = 15.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
arr[1] is assigned the sum of arr[0] and arr[2].
✗ Incorrect
arr[0] is 10 and arr[2] is 30, so arr[1] becomes 40. The output is 40.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
std::reverse reverses the order of elements in the array.
✗ Incorrect
The std::reverse function reverses the array elements, so the output is '4 3 2 1 '.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
The fill() method sets all elements to the given value.
✗ Incorrect
arr.fill(7) sets every element in the array to 7, so the output is '7 7 7 7 7 '.
🧠 Conceptual
expert2: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; }
Attempts:
2 left
💡 Hint
arr.size() returns the number of elements; arr.data() returns pointer to first element.
✗ Incorrect
arr.size() returns 3 because the array has 3 elements. arr.data() points to the first element (5), so *(arr.data() + 1) is the second element (10).