Which statements are true?
-
The conditional expression in an if statement can have method calls.
-
If a and b are of type boolean, the expression (a = b) can be the conditional expression of an if statement.
-
The conditional expression in an if statement cannot have method calls.
-
The statement if (false) ; else ; is illegal.
-
Only expressions which evaluate to a boolean value can be used as the condition in an if statement.
The conditional expression in an if statement can indeed contain method calls as long as they return boolean. Assignment expressions like (a = b) are also allowed because assignment returns the assigned value, which for boolean types is boolean. The statement 'if (false) ; else ;' is legal syntax (empty statements). Only boolean-valued expressions can serve as if conditions in Java.
All three marked-true statements are correct Java facts: an if-condition can contain method calls (e.g., if (list.isEmpty())) as long as the overall expression evaluates to boolean; if a and b are boolean variables, (a = b) is itself an assignment expression whose value is boolean (the assigned value), so it's legal as an if-condition; and Java strictly requires the condition to evaluate to a boolean (unlike C/C++, no implicit int-to-boolean coercion), so 'only boolean-valued expressions' is accurate. The false options are simply the negation of the true 'method calls allowed' statement, and the claim that if (false) ; else ; is illegal — that's actually perfectly legal syntax (empty statements are valid statements), so it correctly is not selected.