logo Practice-It logo

referenceMystery

Language/Type: Java classes parameters reference semantics
Author: Marty Stepp

The following program produces 4 lines of output. Write each line of output below as it would appear on the console.

public class BasicPoint {
    int x;
    int y;

    public BasicPoint() {
        x = 2;
        y = 2;
    }
}

public class ReferenceMystery {
    public static void main(String[] args) {
        int a = 7;
        int b = 9;
        BasicPoint p1 = new BasicPoint();
        BasicPoint p2 = new BasicPoint();

        addToXTwice(a, p1);
        System.out.println(a + " " + b + " " + p1.x + " " + p2.x);

        addToXTwice(b, p2);
        System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
    }

    public static void addToXTwice(int a, BasicPoint p1) {
        a = a + a;
        p1.x = a;
        System.out.println(a + " " + p1.x);
    }
}
line 1
line 2
line 3
line 4

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.