The following post-fix expression with single digit operands is evaluated…

2025

The following post-fix expression with single digit operands is evaluated using a stack:

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

Note that ^ is the exponentiation operator. The top two elements of the stack after the first * is evaluated are:

  1. A.

    6, 1

  2. B.

    5, 7

  3. C.

    3, 2

  4. D.

    1, 5

Show answer & explanation

Correct answer: A

Postfix (Reverse Polish) evaluation rule: scan the expression left to right; push every operand onto the stack; on reading an operator, pop the required number of top values (for a binary operator, the top two), apply the operator to them in the order they were popped -- for postfix, the FIRST value popped is the right-hand operand and the SECOND value popped is the left-hand operand -- and push the result back onto the stack.

  1. Push 8 -> stack: 8

  2. Push 2 -> stack: 8, 2

  3. Push 3 -> stack: 8, 2, 3

  4. Read ^: pop 3 (right operand) and 2 (left operand); compute 23 = 8; push 8 -> stack: 8, 8

  5. Read /: pop 8 (right) and 8 (left); compute 8/8 = 1; push 1 -> stack: 1

  6. Push 2 -> stack: 1, 2

  7. Push 3 -> stack: 1, 2, 3

  8. Read the first *: pop 3 (right) and 2 (left); compute 2x3 = 6; push 6 -> stack: 1, 6 (6 on top)

Cross-check: continuing with the same rule -- + pops 6 and 1 giving 7; push 5, then 1; * pops 1 and 5 giving 5; - pops 5 and 7 giving 2 -- every remaining operator always finds exactly two operands available and the scan ends with exactly one final value, confirming the trace was applied consistently up to the first *.

So immediately after the first * is evaluated, the stack -- listed top element first -- holds 6, 1.

Explore the full course: Coding For Placement