The attributes of three arithmetic operators in some programming language are…

2016

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

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

Attempted by 581 students.

Show answer & explanation

Correct answer: 9

Key insight: In this language, plus has the highest precedence, minus has medium precedence and is right-associative, and multiplication has the lowest precedence.

  1. Evaluate the highest-precedence operator first: 5 + 1 = 6. This gives the intermediate expression 2 − 6 − 7 * 3.

  2. Next, handle subtraction before multiplication because subtraction has higher precedence than multiplication. Subtraction is right-associative, so evaluate the rightmost subtraction first: 6 − 7 = -1.

  3. Now evaluate the remaining subtraction from left to right as determined by the associativity applied: 2 − (−1) = 3.

  4. Finally, perform the multiplication (lowest precedence): 3 * 3 = 9.

Final answer: 9

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir