ProgrammingModerateTCS NQT · Infosys 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] = {3, 1, 4, 1, 5}
for (i = 1 to 4)
    arr[i] = arr[i] + arr[i - 1]
end for
Print arr[4], arr[2]
  1. A. 14 8
  2. B. 6 5
  3. C. 14 5
  4. D. 9 8

Correct answer: A. 14 8

Because arr[i − 1] has already been updated, each slot becomes a running (prefix) sum: 3, 4, 8, 9, 14. So arr[4] = 14 and arr[2] = 8. Using the ORIGINAL left neighbour gives 1 + 5 = 6 and 4 + 1 = 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic