ProgrammingHardAmazon OA · Capgemini

What will be the output of the following pseudocode? (/ is integer division)

What will be the output of the following pseudocode? (/ is integer division)
Stack st = empty
for each token t in "6 2 3 + - 3 8 2 / + *"
    if (t is a number)
        push(st, t)
    else
        a = pop(st)
        b = pop(st)
        push(st, b t a)      // apply operator t as b t a
    end if
end for
Print pop(st)
  1. A. -3
  2. B. 21
  3. C. 7
  4. D. 1

Correct answer: C. 7

6 − (2 + 3) = 1, then 3 + 8 / 2 = 7, and 1 × 7 = 7. Popping in the wrong order (a t b) gives −1 and 3 + 0, i.e. the trap −3.

More Pseudocode and Code Tracing questions

Exams that ask this topic