Difficulty: Medium
Correct Answer: 72 7 34 foo34 7foo
Explanation:
Introduction / Context:
This Java question tests how the + operator behaves with strings versus integers, and how evaluation order affects the final output when mixing numeric addition and string concatenation.
Given Data / Assumptions:
Concept / Approach:
In Java, + performs numeric addition for numeric operands, but if either operand is a String, the operation becomes string concatenation from that point onward. Evaluation proceeds left to right.
Step-by-Step Solution:
Verification / Alternative check:
Manually run a small snippet or mentally simulate left-to-right concatenation rules to confirm each segment, ensuring where numeric addition ends and string concatenation begins.
Why Other Options Are Wrong:
Common Pitfalls:
Forgetting that once a String is involved, all subsequent + operations in that expression become concatenations, and misreading whitespace in the printed tokens.
Final Answer:
72 7 34 foo34 7foo
Discussion & Comments