Java Boolean Operator Precedence
The program’s intention is to evaluate a compound condition correctly, but it produces unexpected results. What is the mistake in the boolean expression?
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
Missing parentheses lead to unintended operator precedence, so the condition does not evaluate as intended.
B
The values of b and c are swapped in the expression.
C
The comparison a > b is incorrect.
D
The operators || and && should be swapped.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE