ITPEC FE Subject A October 2024 Question 2

Topic: Basic TheoryAlso asked in April 2019 and Spring 2020

What is the value of the postfix expression below? Here, A, B, C, and D are operands whose values are 4, 3, 5, and 6, respectively. Symbols “+”, “−”, “×”, and “÷” are arithmetic operators for addition, subtraction, multiplication, and division, respectively.

A B 2 × + D B ÷ C × −

Explanation

0 — evaluate the postfix expression with a stack, applying each operator to the two values on top.

Substitute A = 4, B = 3, C = 5, D = 6 and scan left to right:

  • •push 4, 3, 2; apply × → 3 × 2 = 6, stack 4, 6
  • •apply + → 4 + 6 = 10, stack 10
  • •push 6, 3; apply ÷ → 6 ÷ 3 = 2, stack 10, 2
  • •push 5; apply × → 2 × 5 = 10, stack 10, 10
  • •apply − → 10 − 10 = 0

In infix form this is (A + B × 2) − (D ÷ B × C) = 10 − 10 = 0.

Answer (b)

Why not others:

  • •(a) −3 and (c) 3 — both halves of the final subtraction equal 10, so a correct evaluation can only give 0; a nonzero result means an operator was applied to the wrong operands
  • •(d) 40/3 — the only division is 6 ÷ 3 = 2, which is exact, so no fraction can appear in a correct evaluation

Key rule: In postfix, an operator takes the two values on top of the stack and the value popped second is the left operand, which matters for − and ÷.

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.