Difficulty: Medium
Correct Answer: j = 4
Explanation:
Introduction / Context:
This evaluates your grasp of boolean operators, specifically the difference between non–short-circuit | and short-circuit ||, and how side-effect methods behave inside such expressions. It also touches on do-while loop termination.
Given Data / Assumptions:
i = 0, so i < 10 is true.methodB(k) adds k to static j and returns true.!b is true.
Concept / Approach:| always evaluates both operands; || skips the right operand if the left is true. Therefore, side effects in the right operand may or may not execute depending on operator choice.
Step-by-Step Solution:
First assignment: b = (i < 10) | methodB(4) ⇒ true | methodB(4). Even though left is true, | evaluates right ⇒ j += 4, b becomes true.Second assignment: b = (i < 10) || methodB(8) ⇒ left true, so right not evaluated ⇒ j unchanged at 4, b remains true.Loop condition: !b is false, so the loop runs once.Printed result: j = 4.
Verification / Alternative check:
Swap operators to || in both lines and observe that j remains 0; swap to | in both and see j become 12.
Why Other Options Are Wrong:j = 0 ignores the non–short-circuit evaluation; j = 8 assumes the second methodB executes; 12 double-counts; “no output” is false.
Common Pitfalls:
Forgetting that | on booleans evaluates both sides; misreading do-while's post-test nature.
Final Answer:
j = 4
Discussion & Comments