What is the value of the postfix expression ? a b c d + – ∗ (where a = 8, b =…
2013
What is the value of the postfix expression ?
a b c d + – ∗ (where a = 8, b = 4, c = 2 and d = 5)
- A.
\(- \frac 3 8\) - B.
\(- \frac 8 3\) - C.
\(24\) - D.
\(- 24\)
Attempted by 489 students.
Show answer & explanation
Correct answer: D
Key idea: Evaluate the postfix expression left to right using a stack: push numbers, and when you see an operator pop the two top values (the second popped is the left operand), apply the operator, then push the result.
Push a = 8. Stack: [8]
Push b = 4. Stack: [8, 4]
Push c = 2. Stack: [8, 4, 2]
Push d = 5. Stack: [8, 4, 2, 5]
Encounter +: pop 5 and 2 → compute 2 + 5 = 7. Push 7. Stack: [8, 4, 7]
Encounter −: pop 7 and 4 → compute 4 − 7 = -3. Push -3. Stack: [8, -3]
Encounter *: pop -3 and 8 → compute 8 × -3 = -24. Push -24. Stack: [-24]
Final result: -24