ProgrammingModerateCapgemini · 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] = {1, 2, 3, 4, 5}
Integer t
for (each k from 1 to 2)
    t = arr[0]
    for (each i from 0 to 3)
        arr[i] = arr[i + 1]
    end for
    arr[4] = t
end for
Print arr[0], arr[4]
  1. A. 3 2
  2. B. 2 1
  3. C. 5 4
  4. D. 4 3

Correct answer: A. 3 2

Each outer pass rotates the array left by one place. Two passes give {3, 4, 5, 1, 2}, so arr[0] = 3 and arr[4] = 2. A right rotation would give 4 and 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic