GATE 2016 CS – Question 55
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 ________.
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: 9
Explanation
Apply the highest precedence first, so $5 + 1 = 6$ and the expression becomes $2 - 6 - 7 * 3$. Next comes $-$, which is right associative, so $2 - 6 - 7$ means $2 - (6 - 7) = 2 - (-1) = 3$. Finally the lowest precedence operator $*$ gives $3 * 3 = 9$.