Difficulty: Easy
Correct Answer: x = 7
Explanation:
Introduction / Context:
Compound assignment operators in Java (like +=
) combine an arithmetic operation with an assignment, improving code brevity while preserving type rules and evaluation order.
Given Data / Assumptions:
Concept / Approach:
Evaluate x + y first, then store the result back into x. Since both x and y are ints, there is no implicit narrowing issue in this case.
Step-by-Step Solution:
Verification / Alternative check:
In Java, x += y
is exactly equivalent to x = (type of x)(x + y)
; with both ints, this is the same as plain addition then assignment.
Why Other Options Are Wrong:
Common Pitfalls:
byte
), where implicit cast occurs. Not relevant here but good to remember.
Final Answer:
int
range of −32768 to +32767)?
Discussion & Comments