ProgrammingModerateAmazon OA · Capgemini

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[4] = {1, 2, 3, 4}, res[4], left = 1, right = 1
for (each i from 0 to 3)
    res[i] = left
    left = left * a[i]
end for
for (each i from 3 down to 0)
    res[i] = res[i] * right
    right = right * a[i]
end for
Print res[0], res[1], res[2], res[3]
  1. A. 24 12 8 6
  2. B. 24 24 24 24
  3. C. 1 2 6 24
  4. D. 24 12 6 4

Correct answer: A. 24 12 8 6

The first pass stores the product of everything to the left of i (1, 1, 2, 6), the second multiplies in the product to the right (24, 12, 4, 1). So res[i] is the product of all elements except a[i]: 24 12 8 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic