The following postfix expression with single digit operands is evaluated using…

200720252025202120122018

The following postfix expression with single digit operands is evaluated using a stack:

8 2 3 ^ / 2 3 * + 5 1 * - 

Note that ^ is the exponentiation operator. After the first * operator has been evaluated, the top two stack elements, listed from top to bottom, are:

Answer: A. 6, 1Concept: A postfix (Reverse Polish) expression is evaluated with one stack by scanning tokens from left to right. Each operand is pushed when encountered. For…

  1. A.

    6, 1

  2. B.

    5, 7

  3. C.

    3, 2

  4. D.

    1, 5

Attempted by 645 students.

Show answer & explanation

Correct answer: A

Concept: A postfix (Reverse Polish) expression is evaluated with one stack by scanning tokens from left to right. Each operand is pushed when encountered.

For a binary operator, pop the top value as b and the next value as a, compute a op b, and push the result. The order matters for subtraction, division, and exponentiation; the current stack after any token prefix is the complete evaluation state at that point.

Application: Process the expression only through the first multiplication, recording the stack from bottom to top after each token.

  1. Push 8 → stack: [8]

  2. Push 2 → stack: [8, 2]

  3. Push 3 → stack: [8, 2, 3]

  4. '^' pops b = 3 and a = 2, computes 2 cubed (23) = 8 → push 8 → stack: [8, 8]

  5. '/' pops b = 8 and a = 8, computes a/b = 8/8 = 1 → push 1 → stack: [1]

  6. Push 2 → stack: [1, 2]

  7. Push 3 → stack: [1, 2, 3]

  8. First '*' pops b = 3 and a = 2, computes a×b = 2×3 = 6 → push 6 → stack: [1, 6]

Result: The stack is [1, 6] from bottom to top, so its top two values listed from top to bottom are 6, 1.

Cross-check: Pair each postfix operator with its two preceding operands to obtain ((8 / 2 cubed) + (2×3)) − (5×1). Direct evaluation gives (8/8 + 6) − 5 = (1 + 6) − 5 = 2. The two subexpressions completed before '+' are 8/2 cubed = 1 and 2×3 = 6, independently confirming that immediately after the first '*' the stack is [1, 6] from bottom to top.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…