Java Boolean Operator Precedence
The following code segment produces an unexpected result. Which statement best explains the logical error in the if statement’s condition?
public class ParenthesesBug {
public static void main(String[] args) {
int a = 6, b = 4, c = 5;
if(a > b || b < c && a < c)
System.out.println("Valid range");
else
System.out.println("Invalid range");
}
}
A
The comparison a > b is incorrect.
B
The operators || and && should be swapped.
C
Missing parentheses lead to unintended operator precedence, so the condition does not evaluate as intended.
D
The values of b and c are swapped in the expression.
Question Leaderboard
Not enough data yet to show leaderboard.
