ITPEC FE Morning October 2022 Question 4

What is the value of the following arithmetic expression in reverse Polish notation?

9 2 1 + − 3 ×

Explanation

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

TokenActionStack
9, 2, 1push9 2 1
+2 + 1 = 39 3
−9 − 3 = 66
3push6 3
×6 × 3 = 1818

In infix form this is (9 − (2 + 1)) × 3 = 18.

Answer (d)

Why not others:

  • •(a) 0 — gives × priority over −, computing 9 − 3 × 3; in postfix the order of the tokens already says the subtraction is done first
  • •(b) 4 and (c) 10 — neither comes from applying each operator to the top two stack values in turn; 10, for example, is the infix 9 − 2 + 1 × 3, which ignores the postfix order altogether

Key rule: In reverse Polish notation there is no precedence: each operator acts on the two values just below it, with the lower one as the left operand of − and ÷.

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.