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
Options
- A9 25
- BCompilation fails.
- C0 0
- D3 5
How the community answered
(47 responses)- A4% (2)
- B4% (2)
- C13% (6)
- D79% (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.xandobj.ydo become 9 and 25 afterinitializeruns, but theprintlnreferences the localx/yinmain, not the instance fields. You'd needobj.xandobj.yto 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
intfields to 0), butinitializedoes 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.