GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Trace postfix operand stack

5 questions solved the same way, newest first. Back to Data Structures

How these are solved

Push operands; at each binary operator pop right then left, evaluate and push result; stop exactly at the requested prefix or end. Three 2000 snapshots share a stem but ask different states and are not duplicates.

1 mark · MCQ
Report issue

Stacks & Queues

Question 28, GATE 2015: Evaluate the postfix expression whose tokens are: 10 5 + 60 6 / * 8 −. Options: (A) 284; (B) 213; (C) 142; (D) 71.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (10 steps)

1. A stack evaluator pops the right operand first and left operand second. After + the stack contains 15; after / it contains 15,10; after * it contains 150; the final subtraction is 150−8, not 8−150.

2. Postfix evaluation gives 10+5=15, 60/6=10, then 15·10=150, and 150−8=142. Thus C.

Original paper, page 62 · Answer key

2 marks · MCQ
Report issue

Stack after the first multiplication

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

Report issue

Trace first postfix prefix

Stacks & Queues

Question 13(b)(i), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 +.
Question 13(b)(i), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 +.
Question 13(b)(i), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 +.
Tap to enlarge
Answer & solution

Answer: [10, 7], top = 7 (no official key was published)

Step through it (7 steps)

Read left to right. An operand is pushed; an operator pops the right operand first and then the left, computes left operator right, and pushes the result. The successive stacks are [5], [5,2], [10], [10,3], [10,3,4], [10,7]. The multiplication consumes 5 and 2 to produce 10; the addition consumes only 3 and 4 to produce 7. The earlier 10 remains below the new result. Therefore the requested stack is bottom [10,7] top.

Original paper, page 8

Report issue

Trace second postfix prefix

Stacks & Queues

Question 13(b)(ii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 + 5 2.
Question 13(b)(ii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 + 5 2.
Question 13(b)(ii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 + 5 2.
Tap to enlarge
Answer & solution

Answer: [10, 7, 5, 2], top = 2 (no official key was published)

Step through it (9 steps)

The prefix 5 2 * 3 4 + leaves [10,7]: 5*2=10 and 3+4=7 are two independent partial results. The next token 5 is an operand, so push it to obtain [10,7,5]. Push the next operand 2 to obtain [10,7,5,2]. Stop here: the following multiplication has not yet been read, so 5 and 2 must remain separate entries. The top is 2 and the bottom is 10.

Original paper, page 8

Report issue

Finish postfix evaluation

Stacks & Queues

Question 13(b)(iii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack at the end.
Question 13(b)(iii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack at the end.
Question 13(b)(iii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack at the end.
Tap to enlarge
Answer & solution

Answer: [80] (no official key was published)

Step through it (12 steps)

After 5 2 * 3 4 + 5 2 the stack is [10,7,5,2]. The next * replaces the top two entries 5,2 by 10, leaving [10,7,10]. The following * replaces 7,10 by 70, leaving [10,70]. Finally + replaces 10,70 by 80, leaving [80]. There are six operand tokens and five binary operators, so a valid complete evaluation must leave exactly one entry. In infix form the expression is (5*2)+((3+4)*(5*2))=10+7*10=80.

Original paper, page 8