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)

  1. A.

    \(- \frac 3 8\)

  2. B.

    \(- \frac 8 3\)

  3. C.

    \(24\)

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

  1. Push a = 8. Stack: [8]

  2. Push b = 4. Stack: [8, 4]

  3. Push c = 2. Stack: [8, 4, 2]

  4. Push d = 5. Stack: [8, 4, 2, 5]

  5. Encounter +: pop 5 and 2 → compute 2 + 5 = 7. Push 7. Stack: [8, 4, 7]

  6. Encounter −: pop 7 and 4 → compute 4 − 7 = -3. Push -3. Stack: [8, -3]

  7. Encounter *: pop -3 and 8 → compute 8 × -3 = -24. Push -24. Stack: [-24]

Final result: -24

Explore the full course: Coding For Placement