Question
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 in this language is _____.
Official answer and solution
- Official answer
- 9
- From
- Official GATE 2016 answer key
Solution
Evaluate the arithmetic expression following the given precedence and associativity rules step-by-step.
The attributes of the operators are:
+: High precedence, Left-associative-: Medium precedence, Right-associative*: Low precedence, Left-associative
Expression to evaluate:
Step 1: Evaluate the highest precedence operator +.
- The sub-expression is .
- Substituting this gives: .
Step 2: Evaluate the medium precedence operator -.
- The operator
-is RIGHT-associative. Therefore, must be parenthesized from the right: . - Evaluating inner subtraction: .
- Evaluating outer subtraction: .
- Substituting this back into the expression yields: .
Step 3: Evaluate the lowest precedence operator *.
- .
9
Treating - as left-associative, which would calculate , resulting in .
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2016 Computer Science and Information Technology (CS) Session 1
- Question
- 55 of 65, printed as CS Q.45
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.