ProgrammingHardTCS NQT · Infosys

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[6] = {1, 2, 3, 4, 5, 6}
Procedure rev(Integer l, Integer r)
    while (l < r)
        swap a[l] and a[r]
        l = l + 1
        r = r - 1
    end while
end procedure
rev(0, 1)
rev(2, 5)
rev(0, 5)
Print a[0], a[1], a[2], a[3], a[4], a[5]
  1. A. 3 4 5 6 1 2
  2. B. 2 1 6 5 4 3
  3. C. 3 4 5 6 2 1
  4. D. 4 3 2 1 6 5

Correct answer: A. 3 4 5 6 1 2

rev(0,1) gives 2 1 3 4 5 6; rev(2,5) gives 2 1 6 5 4 3; rev(0,5) gives 3 4 5 6 1 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic