Lab Quiz Solution, Foundations of Computing, Spring, Week 5 -- Thurs May 3 2001

Name:


A file contains the following Java code (exactly as it appears here, there is nothing else in the file). The file is compiled by the Java compiler and the resulting Java program is run.

class Thing {
    int x;
    public Thing(int i) { x = i; }
}

public class CopyDemo {
    public static void main(String[] argv) {
	int i = 0, j = 1;
	System.out.println("1. i is " + i + "  j is " + j);
	j = i;
	System.out.println("2. i is " + i + "  j is " + j);
	i = i + 1;
	System.out.println("3. i is " + i + "  j is " + j);

	Thing a = new Thing(0); Thing b = new Thing(1);
	System.out.println("4. a.x is " + a.x + "  b.x is " + b.x);
	b = a;
	System.out.println("5. a.x is " + a.x + "  b.x is " + b.x);
	a.x = a.x + 1;
	System.out.println("6. a.x is " + a.x + "  b.x is " + b.x);
    }
}
  1. What is (are) the name(s) of the instance variable(s) in the Thing class?     x

  2. What is (are) the name(s) of the primitive variable(s) in the main method?     i, j

  3. What is (are) the name(s) of the reference variable(s) in the main method?     a, b

  4. What output does the program produce?
1. i is 0  j is 1
2. i is 0  j is 0
3. i is 1  j is 0
4. a.x is 0  b.x is 1
5. a.x is 0  b.x is 0
6. a.x is 1  b.x is 1