ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

Array a = [2, 4, 6, 8, 10]. The loop below sums i a[i] over the 0-based indices i. What is printed?

Array a = [2, 4, 6, 8, 10]. The loop below sums i * a[i] over the 0-based indices i. What is printed?
total = 0
for i = 0 to 4
    total = total + i * a[i]
end for
Print total
  1. A. 80
  2. B. 30
  3. C. 110
  4. D. 10

Correct answer: A. 80

The products are 0, 4, 12, 24 and 40, which sum to 80. Summing the values alone gives 30, using 1-based weights gives 110, and summing the indices gives 10.

More Pseudocode and Code Tracing questions

Exams that ask this topic