ProgrammingModerateTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[] = {10, 20, 30, 40}
Integer i = 0, s = 0
while (i < 4)
    s = s + a[i]
    i = i + 2
    if (i == 4) i = 1
end while
Print s
  1. A. 40
  2. B. 60
  3. C. 100
  4. D. 80

Correct answer: C. 100

Indices visited: 0, 2, then i becomes 4 and is reset to 1, then 3, then 5 ends the loop. All four elements are added: 100. Without the reset only a[0] + a[2] = 40.

More Pseudocode and Code Tracing questions

Exams that ask this topic