LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Predict the Output · Question 2

Sum of a partially initialized array Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <iterator>

int main() {
    int a[6] = {4, 8, 15};
    int sum = 0;
    for (int x : a) sum += x;
    std::cout << sum << ' ' << std::size(a) << ' ' << a[5] << '\n';
    return 0;
}
  • A 27 6 0 Correct
  • B 27 3 0
  • C 27 6 15
  • D Garbage
Correct Answer: Option A

The array has 6 elements; the three not listed are 0, so the sum is 4 + 8 + 15 = 27.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links