ProgrammingModerateTCS NQT · Accenture pattern

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}, n = 6
for (i = 0 to n/2 - 1)
    swap(a[i], a[n - 1 - i])
end for
Print a[1], a[2], a[3]
  1. A. 2 3 4
  2. B. 5 4 3
  3. C. 4 3 2
  4. D. 5 3 4

Correct answer: B. 5 4 3

Swapping a[i] with its mirror for the first half reverses the array: 6 5 4 3 2 1, so indices 1..3 hold 5, 4, 3. Looping all the way to n − 1 would swap every pair twice and restore the original order.

More Pseudocode and Code Tracing questions

Exams that ask this topic