ProgrammingHardAmazon OA · Capgemini

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 a[5] = {10, 20, 30, 40, 50}, b[5], k = 2, n = 5
for (each i from 0 to n - 1)
    b[(i + k) mod n] = a[i]
end for
Print b[0], b[1], b[4]
  1. A. 30 40 20
  2. B. 10 20 50
  3. C. 50 10 40
  4. D. 40 50 30

Correct answer: D. 40 50 30

Each a[i] moves k = 2 places to the right, wrapping round: b = {40, 50, 10, 20, 30}, so the output is 40 50 30. A left shift by 2 would give 30 40 20.

More Pseudocode and Code Tracing questions

Exams that ask this topic