ProgrammingVery hardAmazon OA · TCS Digital

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

What will be the output of the following pseudocode? (/ is integer division)
String tok[9] = {"-", "*", "+", "2", "3", "4", "/", "8", "2"}
Stack st = empty
for (each i from 8 down to 0)
    if (tok[i] is a number)
        push(st, tok[i])
    else
        Integer x = pop(st)
        Integer y = pop(st)
        push(st, x tok[i] y)       // first popped is the LEFT operand
    end if
end for
Print pop(st)
  1. A. 4
  2. B. 16
  3. C. 24
  4. D. 20

Correct answer: B. 16

Scanning a prefix expression from the right: 8 / 2 = 4, 2 + 3 = 5, 5 * 4 = 20, and finally 20 − 4 = 16, i.e. (2 + 3) × 4 − 8 / 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic