nerdexam
Oracle

1Z0-809 · Question #149

Given: public class Test { int x, y; public Test(int x, int y) { initialize(x, y); } public void initialize(int x, int y) { this.x = x x; this.y = y y; } public static void main(String[] args) { int…

The correct answer is D. 3 5. D is correct because System.out.println(x + " " + y) in main prints the local variables x and y declared on the line int x = 3, y = 5; - those values are never modified and remain 3 and 5 throughout main. Why the distractors fail: A (9 25) is the trap - obj.x and obj.y do…

Question

Given: public class Test { int x, y; public Test(int x, int y) { initialize(x, y); } public void initialize(int x, int y) { this.x = x * x; this.y = y * y; } public static void main(String[] args) { int x = 3, y = 5; Test obj = new Test(x, y); System.out.println(x + " " + y); } } What is the result?

Options

  • A9 25
  • BCompilation fails.
  • C0 0
  • D3 5

How the community answered

(47 responses)
  • A
    4% (2)
  • B
    4% (2)
  • C
    13% (6)
  • D
    79% (37)

Explanation

D is correct because System.out.println(x + " " + y) in main prints the local variables x and y declared on the line int x = 3, y = 5; - those values are never modified and remain 3 and 5 throughout main.

Why the distractors fail:

  • A (9 25) is the trap - obj.x and obj.y do become 9 and 25 after initialize runs, but the println references the local x/y in main, not the instance fields. You'd need obj.x and obj.y to get that output.
  • B (Compilation fails) is wrong because the code is syntactically valid Java - there are no type mismatches or missing methods.
  • C (0 0) would only apply if the instance fields were printed without ever being initialized (Java defaults int fields to 0), but initialize does assign them.

Memory tip: When you see println in main, ask "whose variables are these?" - local variables shadow instance fields, and x/y without a qualifier inside main always refer to the method-local declarations, never the object's fields.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice