nerdexam
Oracle

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…

Working with Java Data Types

Question

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; System.out.println(t1.x + "" + t1.y); System.out.println(t2.x + "" + t2.y); } } What is the result?

Options

  • A2.34.5
  • B2.34.7
  • C23.47
  • D24.3.5

How the community answered

(43 responses)
  • A
    7% (3)
  • B
    2% (1)
  • C
    74% (32)
  • D
    16% (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

#string concatenation#type coercion#operator precedence#primitive types

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice