| preferred AP College board partner for AP classes
easy Solved by 2 students
Java Boolean Operator Precedence
< Prev
Next >

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.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy