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

2007

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. 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

Attempted by 354 students.

Show answer & explanation

Correct answer: A

Key steps: evaluate the expression left to right using a stack.

  1. Push 8 -> stack: [8]

  2. Push 2 -> stack: [8, 2]

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

  4. Apply ^: pop 3 and 2, compute 2^3 = 8 -> push 8 -> stack: [8, 8]

  5. Apply /: pop 8 and 8, compute 8/8 = 1 -> push 1 -> stack: [1]

  6. Push 2 -> stack: [1, 2]

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

  8. Apply first *: pop 3 and 2, compute 2*3 = 6 -> push 6 -> stack: [1, 6]. The top two elements are 6 and 1.

  9. Apply +: pop 6 and 1, compute 1 + 6 = 7 -> push 7 -> stack: [7]

  10. Push 5 -> stack: [7, 5]

  11. Push 1 -> stack: [7, 5, 1]

  12. Apply second *: pop 1 and 5, compute 5*1 = 5 -> push 5 -> stack: [7, 5]

  13. Apply -: pop 5 and 7, compute 7 - 5 = 2 -> push 2 -> stack: [2] Final result = 2

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

Explore the full course: Gate Guidance By Sanchit Sir