ITPEC FE Morning April 2019 Question 3

Source exam: ITPEC FE Morning April 2019Topic: Basic Theory & Math

ITPEC FE Morning April 2019 — Question 3 of 80

Postfix (Reverse Polish Notation) Evaluation — evaluate an arithmetic expression written in postfix notation using a stack.

To evaluate a postfix expression, scan left to right:
- Operand → push onto the stack

- Operator → pop two operands, apply the operator, push the result

For binary operators: the second popped value is the left operand, and the first popped is the right operand. This matters for non-commutative operations like and ÷.

Given: A=4, B=3, C=5, D=6
Expression: A B 2 × + D B ÷ C × −

TokenActionStack
Apush 4[4]
Bpush 3[4, 3]
2push 2[4, 3, 2]
×3 × 2 = 6[4, 6]
+4 + 6 = 10[10]
Dpush 6[10, 6]
Bpush 3[10, 6, 3]
÷6 ÷ 3 = 2[10, 2]
Cpush 5[10, 2, 5]
×2 × 5 = 10[10, 10]
10 − 10 = 0[0]

Infix equivalent: (A + B × 2) − (D ÷ B × C) = (4 + 6) − (2 × 5) = 10 − 10 = 0

Why not others:
- (a) -3 — arithmetic error or incorrect operand ordering

- (c) 3 — arithmetic error

- (d) 40/3 — incorrect order of operands in division

Key rule: In postfix, scan left to right using a stack. For each operator, the second popped value is the left operand and the first popped is the right operand.

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.