ITPEC FE Morning April 2022 Question 4
Which of the following is an expression in reverse Polish notation that has the same value as the expression below when evaluated? Here, numbers are given as decimals, and the symbols − and × are subtraction and multiplication operators, respectively.
3 4 − 5 ×
Explanation
5 3 4 − × — the given expression is (3 − 4) × 5 = −5, and this one computes 5 × (3 − 4), the same value.
Evaluate RPN with a stack: push operands, and on an operator pop two values, the first popped being the right operand.
- •3 4 − 5 ×: 3 − 4 = −1, then −1 × 5 = −5
- •5 3 4 − ×: push 5, then 3 − 4 = −1, then 5 × (−1) = −5
Multiplication is commutative, so the factor 5 may come first, but the subtraction must stay 3 − 4 in that order.
Answer (c)
Why not others:
- •(a) 3 4 5 × − — this is 3 − 4 × 5 = −17; the multiplication happens before the subtraction
- •(b) 4 5 − 3 × — this is (4 − 5) × 3 = −3, different operands for both operators
- •(d) 5 4 3 − × — this is 5 × (4 − 3) = 5; the subtraction's operands are reversed
Key rule: Commutative operators (+, ×) let you swap their operands in RPN; for − and ÷ the operand order must be kept.
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.