Consider the following left-associative operators, listed in decreasing order…

2010

Consider the following left-associative operators, listed in decreasing order of precedence:

  • − subtraction (highest precedence)

  • * multiplication

  • $ exponentiation (lowest precedence)

What is the result of the following expression?

3 − 2 * 4 $ 1 * 2 $ 3

Answer: D. 4096ConceptPrecedence decides which operator binds its operands first: an operator of higher precedence groups with its operands before any operator of lower…

  1. A.

    −61

  2. B.

    64

  3. C.

    512

  4. D.

    4096

Attempted by 31 students.

Show answer & explanation

Correct answer: D

Concept

Precedence decides which operator binds its operands first: an operator of higher precedence groups with its operands before any operator of lower precedence, no matter where each one appears in the string. Associativity settles the order only among operators of equal precedence — left-associative means the leftmost such operator groups first.

An expression is therefore never read simply left to right. Here the ranking is deliberately inverted from ordinary arithmetic: subtraction binds tightest and exponentiation binds loosest, so the grouping must be rebuilt from the stated ranking rather than from habit.

Application

  1. Subtraction has the highest precedence, so 3 − 2 groups first and evaluates to 1. The string becomes 1 * 4 $ 1 * 2 $ 3.

  2. Multiplication comes next and is left-associative, so the leftmost multiplication groups first: 1 * 4 = 4, leaving 4 $ 1 * 2 $ 3; then 1 * 2 = 2, leaving 4 $ 2 $ 3.

  3. Exponentiation has the lowest precedence and is left-associative, so the two $ operators group as (4 $ 2) $ 3.

  4. 4 $ 2 = 42 = 16, and 16 $ 3 = 163 = 4096.

Fully parenthesised, the expression is (((3 − 2) * 4) $ (1 * 2)) $ 3, whose value is 4096.

Cross-check

  • Under ordinary arithmetic precedence the same string would read 3 − 2·41·23 = 3 − 64 = −61, and the multiplicative part alone would be 64. The stated ranking overrides exactly that habit, which is what the question is testing.

  • Applying the operators strictly in written order instead — 3 − 2 = 1, 1 * 4 = 4, 4 $ 1 = 4, 4 * 2 = 8, 8 $ 3 — would give 83 = 512. Reading order is not the rule; the precedence ranking is.

  • If $ were right-associative the grouping would be 4 $ (2 $ 3) = 48 = 65536. The stated left-associativity is what fixes the grouping as (4 $ 2) $ 3 and gives 4096.

Explore the full course: Mppsc Assistant Professor Computer Science Paper 2

Loading lesson…