IAE Academy

GATE 2024 CS1 Q.33: solution and answer

GATE 2024 · CS1Q.33 · 1 mark · NAT

Consider the operator precedence and associativity rules for the integer arithmetic operators given in the table below.

| Operator | Precedence | Associativity |
|---|---|---|
| + | Highest | Left |
| − | High | Right |
| * | Medium | Right |
| / | Low | Right |

The value of the expression as per the above rules is ____

GATE 2024 CS1 question 33: A 4-row table (plus header) listing four arithmetic operators with their precedence level and associativity: '+' →…Tap to open full size
Figure from the GATE 2024 CS1 paper. It is part of the question.
Figure description

A 4-row table (plus header) listing four arithmetic operators with their precedence level and associativity: '+' → Highest, Left; '−' → High, Right; '*' → Medium, Right; '/' → Low, Right.

Answer and solution

ANSWER

The value of the expression, under the stated (non-standard) precedence/associativity rules, is 6.

Official answer: 6Source: Official answer key, GATE 2024 CS1 (IISc Bengaluru)

  1. Step 1 — Identify precedence order from the table (tighter-binding to loosest): + (Highest) > − (High) > (Medium) > / (Low). Associativity: + Left, − Right, Right, / Right.

  2. Step 2 — The lowest-precedence operator (/) forms the outermost split of the whole expression. Two '/' occur, and / is right-associative, so the expression is A / (B / C), where A = '3+1+5*2', B = '7+2-4-7-6', C = '2'.

  3. Step 3 — Evaluate A: it contains only + and ; since + binds tighter than here, all the + operations collapse first: 3+1+5 = 9, then the remaining * applies: A = 9*2 = 18.

  4. Step 4 — Evaluate B: it contains + and −; + binds tighter, so 7+2 = 9 collapses first, leaving the right-associative chain 9−4−7−6 = 9−(4−(7−6)) = 9−(4−1) = 9−3 = 6, so B = 6.

  5. Step 5 — C = 2.

  6. Step 6 — Combine at the top level (right-associative /): A / (B / C) = 18 / (6 / 2) = 18 / 3 = 6.

KEY IDEA

Precedence decides which operator's operand-groups are formed first (tighter binding = evaluated/collapsed first); the lowest-precedence operator present at any level is the one whose split happens outermost/last. Associativity only matters for resolving a chain of equal-precedence operators.

WATCH OUT FOR

Assuming "+" being listed first / "Highest" automatically means it is evaluated last (as in standard math where low precedence like + is outermost); here the table explicitly reassigns precedence so + is tightest-binding and / is loosest-binding — using everyday PEMDAS intuition instead of the table gives a different (wrong) grouping and answer.

From the GATE 2024 CS1 paper. Every question of this paper · Download the full paper · All 7 C basics questions, with notes

More C basics questions