ProgrammingModerateAmazon OA · TCS NQT

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[5] = {0, 1, 0, 3, 12}, k = 0
for (each i from 0 to 4)
    if (a[i] != 0)
        swap(a[k], a[i])
        k = k + 1
    end if
end for
Print a[0], a[1], a[2], a[3], a[4]
  1. A. 1 3 0 12 0
  2. B. 12 3 1 0 0
  3. C. 1 3 12 0 0
  4. D. 0 0 1 3 12

Correct answer: C. 1 3 12 0 0

Each non-zero value is swapped into the next front slot, so the non-zeros keep their order and the zeros drift to the end: 1 3 12 0 0.

More Pseudocode and Code Tracing questions

Exams that ask this topic