Given:
public class MyField { int x; int y;
public void doStuff(int x, int y) { this.x = x; y = this.y;
>
public void display () {
System.out.print(x + " " + y + " : ");
}
public static void main(String[] args) { MyField ml = new MyField (); ml.x = 100; ml. y = 200;
MyField m2 = new MyField (); m2 .doStuff(ml.x, ml.y); ml.display{); m2.display{);
}}
What is the result?
A. 100 0 : 100 200:
B. 100 0 : 100 0 :
C. 100200:100 200:
D. 100 200 : 100 0 :
Answer D:
Tricky because in the doStuff() method, it says ” y = this.y;” NOT ” this.y = y;”. This actually means that the code is reassigning the local variable y value, not the instance variable y!
public class Test{
int x;
int y;
public void doStuff(int x, int y) {
this.x = x;
y = this.y;
}
public void display () {
System.out.print(x + ” ” + y + ” : ” );
}
public static void main(String[] args) {
Test m1 = new Test ();
m1.x = 100;
m1. y = 200;
Test m2 = new Test ();
m2.doStuff(m1.x, m1.y);
m1.display();
m2.display();
}
}
Answer is D
Answer is D, after typing it in Netbeans
E