ITPEC FE Morning April 2020 Question 3

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

ITPEC FE Morning April 2020 — Question 3 of 80

Postfix Expression Evaluation — evaluate a postfix (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 A(4), B(3), 2 → stack: [4, 3, 2]
  • ×3 × 2 = 6 → stack: [4, 6]
  • +4 + 6 = 10 → stack: [10]
  • Push D(6), B(3) → stack: [10, 6, 3]
  • ÷6 ÷ 3 = 2 → stack: [10, 2]
  • Push C(5) → stack: [10, 2, 5]
  • ×2 × 5 = 10 → stack: [10, 10]
  • 10 − 10 = 0 → stack: [0]

Result: 0

Why not others:
- (a) −3 — incorrect operand order in subtraction or division

- (c) 3 — arithmetic error in intermediate steps

- (d) 40/3 — fails to evaluate B × 2 before addition

Key rule: In postfix, read left to right; when you hit an operator, pop two operands (first popped = right operand, second popped = 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.