Given:
What is the result?
Given:
What is the result?
The method doStuff assigns the parameters x and y to the local variables x and this.y respectively. However, in Java, the statement x = x; assigns the parameter x to itself, which does not impact the instance variable x. Similarly, y = this.y; assigns the current value of the instance variable y to itself, so no actual change happens. Therefore, the variables x and y for the instance m2 are not modified and retain their default values of 0, leading to the output being 100 200 : 0 0 :.
The correct is A, because m2 object variables x and y are not assigned so, these have default zero values. so the answer is 100:200:0:0 and you can test the code: public class MyField { int x; int y; public void doStuffy(int x, int y) { x = x; y = this.y; } public void display () { System.out.print(x + " " + y + " : "); } public static void main(String[] args) { MyField m1 = new MyField (); m1.x = 100; m1.y = 200; MyField m2 = new MyField(); m2.doStuffy(m1.x, m1.y); m1.display(); m2.display(); } }
Thanks
m2 object variables x and y are not assigned so, these have default zero values. so the answer is 100:200:0:0
Tested and agreed
A. 100 200 : 0 0 because x =x; if this.x=x then answer is B
Answer is A
A is correct
100 200 : 0 0 :
doStuff method only assign local variables, not affecting instance variables
A is the correct one
100 200 : 0 0 : public class MyField { int x; int y; public void doStuffy(int x, int y) { x = x; y = this.y; } public void display () { System.out.print(x + " " + y + " : "); } public static void main(String[] args) { MyField m1 = new MyField (); m1.x = 100; m1.y = 200; MyField m2 = new MyField(); m2.doStuffy(m1.x, m1.y); m1.display(); m2.display(); } }
B. 100 200 : 100 0
Answer is A.
100 200 : 0 0 :
As below (iSnover comment), Answer is A.