ITPEC FE Subject A October 2024 Question 2
ITPEC FE Subject A October 2024 — Question 2 of 60
Postfix Expression Evaluation — evaluate a reverse Polish notation expression using a stack.
Expression: A B 2 × + D B ÷ C × − where A=4, B=3, C=5, D=6.
Step-by-step stack trace:
- Push 4, 3, 2
- × → 3 × 2 = 6 → stack: [4, 6]
- + → 4 + 6 = 10 → stack: [10]
- Push 6, 3
- ÷ → 6 ÷ 3 = 2 → stack: [10, 2]
- Push 5
- × → 2 × 5 = 10 → stack: [10, 10]
- − → 10 − 10 = 0
Result: 0
Infix equivalent: (A + B × 2) − (D ÷ B × C) = (4 + 6) − (2 × 5) = 0
Why not others:
- (a) −3 — does not match any valid evaluation order
- (c) 3 — incorrect arithmetic
- (d) 40/3 — would require different grouping of division
Key rule: In postfix, scan left to right; when you hit an operator, pop two operands (second popped is the left operand), apply the operator, push the result.
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
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.