GATE / CS ARCHIVE

GATE CSE 2007 / QUESTION 38

Stack after the first multiplication

Data Structures · Stacks & Queues · Full GATE CSE 2007 paper

GATE 2007, Q38 2 marks · MCQ
Report issue

Stacks & Queues

Question 38, GATE 2007: Evaluate the postfix expression 8 2 3 ^ / 2 3 * + 5 1 * − using a stack; ^ denotes exponentiation. What are the top two stack elements, top first, immediately after the first * is evaluated?
Options:
(A) 6,1
(B) 5,7
(C) 3,2
(D) 1,5
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step through it (9 steps)

1. Approach. For each operand, push; for each binary operator, pop right operand then left operand, compute left op right, and push the result. Stop at the first multiplication rather than evaluating the whole expression.

2. Derivation and interpretation. Read 8,2,3, then exponentiation replaces 2,3 with 8; division replaces 8,8 with 1. Push 2 and 3; the first multiplication replaces them with 6. The stack is now [1,6] from bottom to top, so its top two elements are 6,1, A.

Original paper, page 9