Consider the following postfix expression with single digit operands : \(6 \;…

2018

Consider the following postfix expression with single digit operands :

\(6 \; 2 \: 3 \: ^* \: / \: 4 \: 2 \: ^* \; + \: 6 \: 8 \: ^* \: -\)

The top two elements of the stack after the second ∗ is evaluated, are :

  1. A.

    8,2

  2. B.

    8,1

  3. C.

    6,2

  4. D.

    6,3

Attempted by 389 students.

Show answer & explanation

Correct answer: B

Evaluation of the postfix expression 623*/42*+68*- to find the stack state after the second multiplication:

1. Scan 6, 2, 3: Push them onto the stack. Stack: [6, 2, 3]

2. Scan first '*': Pop 3 and 2. Calculate 2 x 3 = 6. Push 6. Stack: [6, 6]

3. Scan '/': Pop 6 and 6. Calculate 6 / 6 = 1. Push 1. Stack: [1]

4. Scan 4, 2: Push them onto the stack. Stack: [1, 4, 2]

5. Scan second '*': Pop 2 and 4. Calculate 4 x 2 = 8. Push 8. Stack: [1, 8]

After the second * is evaluated, the top element is 8 and the element below it is 1. Therefore, the top two elements are 8, 1.

Correct Option: (c) 8, 1

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Coding For Placement