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:
- A.
6, 1
- B.
5, 7
- C.
3, 2
- D.
1, 5
Attempted by 354 students.
Show answer & explanation
Correct answer: A
Key steps: evaluate the expression left to right using a stack.
Push 8 -> stack: [8]
Push 2 -> stack: [8, 2]
Push 3 -> stack: [8, 2, 3]
Apply ^: pop 3 and 2, compute 2^3 = 8 -> push 8 -> stack: [8, 8]
Apply /: pop 8 and 8, compute 8/8 = 1 -> push 1 -> stack: [1]
Push 2 -> stack: [1, 2]
Push 3 -> stack: [1, 2, 3]
Apply first *: pop 3 and 2, compute 2*3 = 6 -> push 6 -> stack: [1, 6]. The top two elements are 6 and 1.
Apply +: pop 6 and 1, compute 1 + 6 = 7 -> push 7 -> stack: [7]
Push 5 -> stack: [7, 5]
Push 1 -> stack: [7, 5, 1]
Apply second *: pop 1 and 5, compute 5*1 = 5 -> push 5 -> stack: [7, 5]
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.