Given:public class Main {
public static void main(String[] args) {
int a = 2, b = 3, c = 4;
int r = a + b * c % 5 - c / a;
System.out.println(r);
}
}What is the result?
Choose one.
Multiplicative operators (* / %) have higher precedence than additive ones (+ -), and operators of equal precedence associate left to right.
The expression groups as a + ((b * c) % 5) - (c / a). Each wrong value comes from one grouping mistake: % before * (12), + before * (-2), or % applied to the whole sum (4).
- Group the multiplicative operators first: (b * c) % 5 and c / a.
- Within that level, go left to right: 12, then 12 % 5 = 2.
- c / a = 2.
- Finish left to right: 2 + 2 - 2 = 2.
Exam tip: * / % bind tighter than + -, and equal-precedence operators run left to right.
Java Primitives, Wrapper Classes, Operators and Casting (1Z0-831) — the lesson that teaches this.