What is the result?

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 :

Download Printable PDF. VALID exam to help you PASS.

5 thoughts on “What is the result?

  1. 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!

  2. 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();
    }
    }

    4
    1

Leave a Reply

Your email address will not be published.