ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode? (Arrays are indexed from 0.)

What will be the output of the following pseudocode? (Arrays are indexed from 0.)
Integer arr[5] = {1, 2, 3, 4, 5}
Integer s = 0
for (each i from 1 to 5)
    s = s + arr[i - 1]
end for
Print s
  1. A. 10
  2. B. 15
  3. C. 20
  4. D. 12

Correct answer: B. 15

The loop runs i=1..5, and arr[i-1] correctly accesses arr[0] through arr[4] (compensating for 1-based counting against a 0-indexed array), summing the whole array: 1+2+3+4+5=15.

More Pseudocode and Code Tracing questions

Exams that ask this topic