1Z0-819 · Question #167
public class Tester { private int x; private int y; public static void main(String[] args) { Tester t1 = new Tester(); t1.x = 2; t1.y = 3; Tester t2 = new Tester(); t2.x = 4; t2.y = 7…
The correct answer is C. 23.47. Option C is correct because the expression t1.x + "" + t1.y uses string concatenation, not arithmetic addition. Java evaluates left to right, so 2 + "" produces the string "2", then "2" + 3 produces "23" - the digits are joined as text, not summed. The same logic applies to t2…
Question
Options
- A2.34.5
- B2.34.7
- C23.47
- D24.3.5
How the community answered
(43 responses)- A7% (3)
- B2% (1)
- C74% (32)
- D16% (7)
Explanation
Option C is correct because the expression t1.x + "" + t1.y uses string concatenation, not arithmetic addition. Java evaluates left to right, so 2 + "" produces the string "2", then "2" + 3 produces "23" - the digits are joined as text, not summed. The same logic applies to t2, producing "47", and since println is called twice, the output appears on two separate lines: 23 then 47.
Options A, B, and D are distractors that mix up values between the two objects or swap field assignments - for example, implying t1.y is 5 or t2.y is 5, neither of which is true based on the code. Option B gets t2.y = 7 right but still shows 2.3 for the first line, which would only be correct if the values were printed with a decimal separator rather than concatenated.
Memory tip: Whenever you see int + "" + int in Java, mentally replace "" with a sticky note - it "sticks" the numbers together as strings rather than adding them mathematically. If the "" weren't there, you'd get arithmetic; with it, you get text glued side by side.
Topics
Community Discussion
No community discussion yet for this question.