Multiple choice technology programming languages

Which statements are true?

  1. The conditional expression in an if statement can have method calls.

  2. If a and b are of type boolean, the expression (a = b) can be the conditional expression of an if statement.

  3. The conditional expression in an if statement cannot have method calls.

  4. The statement if (false) ; else ; is illegal.

  5. Only expressions which evaluate to a boolean value can be used as the condition in an if statement.

Reveal answer Fill a bubble to check yourself
A,B,E Correct answer
Explanation

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.

AI explanation

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.