How many PUSH and POP operations will be needed to evaluate the following…
2014
How many PUSH and POP operations will be needed to evaluate the following expression by reverse polish notation in a stack machine (A ∗ B) + (C ∗ D/E) ?
- A.
4 PUSH and 3 POP instructions
- B.
5 PUSH and 4 POP instructions
- C.
6 PUSH and 2 POP instructions
- D.
5 PUSH and 3 POP instructions
Attempted by 322 students.
Show answer & explanation
Correct answer: B
Convert to RPN: A B * C D * E / +
Count PUSH operations: one PUSH for each operand. There are five operands (A, B, C, D, E) → 5 PUSH.
Count POP operations: using the counting convention applied here, each binary operator consumes operands from the stack and is counted as one POP per operator. There are four binary operators (two multiplications, one division, one addition) → 4 POP.
Answer: 5 PUSH and 4 POP instructions