Consider the following code segment. What is printed as a result of executing the code segment?
boolean valid = false;
boolean wait = true;
boolean pass = true;
System.out.println(pass || valid && wait);
truefalsetrue falsevalid && wait first, and pass is true, so the whole expression is true.
Explanation
Operator precedence is the main rule Java uses to group a compound condition. Without parentheses, && is evaluated before ||, so valid && wait is grouped first. An expression using || is true when at least one side is true.
- B is not correct: the left side of the
||ispass, which holdstrue, and onetrueside is already enough for the whole expression. - C is not correct: the compound expression produces a single Boolean result, and one
System.out.printlnstatement prints that one value rather than two. - D is not correct: the code compiles, because each name is declared as a
booleanand&&and||combine Boolean values.