ITPEC FE Morning October 2023 Question 5
Which of the following expressions in postfix notation is equivalent to the expression 2+5×3+4 in infix notation? Here, the operator “×” has higher precedence than “+”.
Explanation
2 5 3 × + 4 + — convert by precedence: the multiplication is done first, then the additions left to right.
With precedence made explicit the expression is (2 + (5 × 3)) + 4:
- •5 × 3 becomes 5 3 ×
- •2 + that becomes 2 5 3 × +
- •adding 4 gives 2 5 3 × + 4 +
Check with a stack: push 2, 5, 3; × gives 15; + gives 2 + 15 = 17; push 4; + gives 21, the value of 2 + 5 × 3 + 4.
Answer (d)
Why not others:
- •(a) 2 5 3 4 × + + — evaluates 2 + (5 + 3 × 4) = 19: the × binds 3 and 4 instead of 5 and 3
- •(b) 2 5 + 3 4 × + — evaluates (2 + 5) + 3 × 4 = 19, again multiplying 3 by 4
- •(c) 2 5 3 + × 4 + — evaluates 2 × (5 + 3) + 4 = 20: swaps the two operators, so + is applied before ×
Key rule: In postfix each operator follows its two operands in the order it is evaluated; parenthesise the infix by precedence, convert the innermost part first, and verify with a stack.
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.