ITPEC FE Subject A April 2024 Question 3

The expression below is represented in postfix (reverse Polish) notation. What is the resulting value of this expression? Here, the numbers are in decimal, and the symbols ‘+’, ‘−’, ‘*’, and ‘/’ represent addition, subtraction, multiplication, and division operators, respectively.

100 5 6 2 + * 12 4 / − − 36 −

Explanation

27 — evaluate the postfix expression with a stack: push each number, and let each operator pop two values and push the result.

  • •Push 100, 5, 6, 2; + gives 6 + 2 = 8
  • •× gives 5 × 8 = 40; the stack is 100, 40
  • •Push 12, 4; / gives 12 ÷ 4 = 3; the stack is 100, 40, 3
  • •− gives 40 − 3 = 37; the stack is 100, 37
  • •− gives 100 − 37 = 63
  • •Push 36; − gives 63 − 36 = 27

In infix form: 100 − (5 × (6 + 2) − 12 ÷ 4) − 36 = 27.

Answer (d)

Why not others:

  • •(c) 21 — drops the grouping the postfix order creates and computes 100 − 40 − 3 − 36; the 3 is subtracted from 40 first, so it is effectively added back to 100
  • •(a) 5 and (b) 18 — no consistent stack evaluation gives these; each operator acts on the two values directly beneath it, and the trace leaves 27

Key rule: In postfix, an operator acts on the two most recent values with the second-popped value on the left, so "a b −" means a − b, and the parentheses are implied by the order.

Basic Theory · AI-written, may contain errors

This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.