Difficulty: Medium
Correct Answer: 6 3
Explanation:
Introduction / Context:
This Java question checks your understanding of pre-increment (++x) and the short-circuit behavior of the logical AND operator (&&) inside a loop. The trick is tracking when the right-hand side (++y) executes and when it is skipped.
Given Data / Assumptions:
Concept / Approach:
For &&, the right side is evaluated only if the left side is true. Therefore ++y happens only on iterations where ++x > 2 is true. Pre-increment changes the variable before comparison.
Step-by-Step Solution:
Verification / Alternative check:
Manually simulate or add trace prints inside the loop to confirm increments of x and y match the above sequence.
Why Other Options Are Wrong:
They assume that y increments on every iteration or that the right-hand side runs even when the left side is false, which contradicts short-circuit AND semantics.
Common Pitfalls:
Confusing pre-increment with post-increment and forgetting that with && the second operand does not always execute.
Final Answer:
6 3
Discussion & Comments