Difficulty: Easy
Correct Answer: c
Explanation:
Introduction / Context:
This classic trap distinguishes assignment (=) from comparison (==) inside an if condition. The code assigns false to the variable and then evaluates the new value in the control flow.
Given Data / Assumptions:
Concept / Approach:
In Java, an assignment expression has the value of the assigned result. Thus if (bool = false) assigns false and then tests false, so the first branch does not execute. The chain continues with the updated bool value.
Step-by-Step Solution:
Verification / Alternative check:
Change the first line to if (bool == false) (comparison) and the flow changes. Or write if (!bool) for clarity.
Why Other Options Are Wrong:
Common Pitfalls:
Using assignment in conditions unintentionally; always enable compiler warnings or code style checks to prevent this.
Final Answer:
c
Discussion & Comments