Evaluate the following infix expression using algorithm and choose the correct…

2023

Evaluate the following infix expression using algorithm and choose the correct answer. a+b*c-d/e^f where a=1, b=2, c=3, d=4, e=2, f=2.

  1. A.

    6

  2. B.

    8

  3. C.

    9

  4. D.

    7

Show answer & explanation

Correct answer: A

Concept: In an infix expression, operators are evaluated in a fixed precedence order rather than strictly left to right across all operators. Exponentiation (^) is applied first, then multiplication and division are applied left to right, and finally addition and subtraction are applied left to right.

  1. Substitute a=1, b=2, c=3, d=4, e=2, f=2 into a+b*c-d/e^f to get 1+2*3-4/2^2.

  2. Evaluate the exponent first, since ^ has the highest precedence: ef = 22 = 4, so the expression becomes 1+2*3-4/4.

  3. Evaluate multiplication and division next, left to right: b*c = 2*3 = 6, and d/(e^f) = 4/4 = 1, so the expression becomes 1+6-1.

  4. Evaluate addition and subtraction last, left to right: 1+6 = 7, then 7-1 = 6.

Cross-check: fully parenthesizing by precedence gives a + (b*c) - (d/(e^f)) = 1 + 6 - 1 = 6, the same result obtained above.

Hence, the value of the expression is 6.

Explore the full course: Coding For Placement

Loading lesson…