Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
OperatorPriority
This program excerpt helps the user to get acquainted with operators' priority.

Download files: OperatorPriority.java
Areas: Primitive types.


OperatorPriority.java
1     int result;
2
3     result = 8+3*-17%5;
4     System.out.println("result="+result);
5
6     result = 8+3*(-17%5);
7     System.out.println("result="+result);
8
9     result = -3+5;
10     System.out.println("result="+result);
Displaying 10 of 10 lines
View full source


Question 1
In the arithmetic expression, 8 + 3 * -17 % 5, which of the operators are unary? Which of them are binary?
Hints Solution

Question 2
Which operators, unary or binary, have higher precedence?
Solution

Question 3
Between the operator + and the operator *, which has a higher precedence?
Solution

Question 4
What does the operator % do? How does the precedence of % compare with that of * ?
Solution

Question 5
What is the value of result after the first assignment?
Hints Solution

Question 6
What is the value of result after the second expression?
Solution

Question 7
What is the result of the third expression?
Solution

Question 8
How can we make the precedence of operators in an expression clearer?
Solution