ProgrammingHardTCS NQT · Accenture 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[6] = {1, 2, 3, 4, 5, 6}
for (each i from 0 to 5)
    swap(arr[i], arr[5 - i])
end for
Print arr[0], arr[5]
  1. A. 3 4
  2. B. 1 6
  3. C. 6 1
  4. D. 4 3

Correct answer: B. 1 6

The loop runs over the WHOLE array. The first three swaps reverse it, and the last three swap the same pairs back, so it ends up unchanged: 1 and 6. Stopping halfway (i from 0 to 2) would reverse it.

More Pseudocode and Code Tracing questions

Exams that ask this topic