Project ForgeSign up free
Compiler Design
Code Generation & Optimization
GATE 2016
Expert

The attributes of three arithmetic operators in some programming language are given below.

Operator | Precedence | Associativity | Arity + | High | Left | Binary − | Medium | Right | Binary * | Low | Left | Binary

The value of the expression 2 − 5 + 1 − 7 * 3 in this language is ____.

9

Explanation

Highest precedence '+' binds first: 5+1=6, giving 2 − 6 − 7 * 3. Next-highest is '−' (medium, beats '*'), and since '−' is right-associative, the chain 2−6−7 groups as 2−(6−7) = 2−(−1) = 3 (note '7' is claimed by '−' before '*' can use it, since '−' has higher precedence). Finally the lowest-precedence '*' combines this result with 3: 3*3 = 9.

Source: GATE CS 2016 (Session 1 / Set A) (2016)

This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.

Start revising free