edited by
10,156 views
35 votes
35 votes
The attribute of three arithmetic operators in some programming language are given below.
$$\begin{array}{|c|l|}\hline \textbf{OPERATOR}  &  \textbf{PRECEDENCE} & \textbf{ASSOCIATIVITY} & \textbf{ARITY} \\\hline  \text{$+$} & \text{High} & \text{Left} & \text{Binary} \\\hline  \text{$-$} & \text{Medium} & \text{Right} & \text{Binary} \\\hline  \text{$*$} & \text{Low} & \text{Left} & \text{Binary} \\\hline \end{array}$$
The value of the expression $2-5+1-7*3$ in this language is ________.
edited by

4 Answers

Best answer
85 votes
85 votes
$2 - 5 + 1 - 7 * 3$ will be evaluated according to the precedence and associativity as given in the question as follows:
$((2 - ((5 + 1) - 7)) * 3) \Rightarrow ((2 - (-1))*3) \Rightarrow  9$
edited by
0 votes
0 votes

+ has highest precedence, so it will be evaluated first.
2 − 5 + 1 − 7 * 3 = 2 − (5 + 1) − 7 * 3 = 2 − 6 − 7 * 3
Now, − has more precedence than *, so sub will be evaluated before * and – has right associative so (6 − 7) will be evaluated first.
2 − 6 − 7 * 3 = (2 − (6 − 7)) * 3 = (2 – (−1)) * 3 = 3 * 3 = 9

Answer:

Related questions

69 votes
69 votes
12 answers
1
85 votes
85 votes
18 answers
4
Sandeep Singh asked Feb 12, 2016
35,392 views
Let $G$ be a complete undirected graph on $4$ vertices, having $6$ edges with weights being $1, 2, 3, 4, 5,$ and $6$. The maximum possible weight that a minimum weight s...