ProgrammingHardAmazon OA · Capgemini

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 a[6] = {3, 2, 2, 3, 4, 3}, k = 0, val = 3
for (each i from 0 to 5)
    if (a[i] != val)
        a[k] = a[i]
        k = k + 1
    end if
end for
Print k, a[0], a[2], a[4]
  1. A. 3 2 4 3
  2. B. 3 2 4 4
  3. C. 3 2 2 4
  4. D. 2 2 4 4

Correct answer: B. 3 2 4 4

The kept values 2, 2, 4 are copied to the front, so k = 3 and a = {2, 2, 4, 3, 4, 3} (the tail keeps its old values). Output 3 2 4 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic