#### Quiz Solutions, Data Structures and Algorithms, Week 10 -- Thurs Nov 29 2001

Assume the following Java code:

```
class Thing {

// Instance variables

Thing next;

int i;

String s;

// Constructor

public Thing(int x, String msg) { i = x; s = msg; next = null; }

}

public class ThingDemo {

public static void main(String argv[]) {

Thing thing1 = null, thing2 = null;

Thing list = null, temp = null, ref = null;

// code appears in numbered problems below ...

}

}

```

Assume the code fragments in the numbered problems below are executed in the `main` method in the order they appear. The questions in each problem refer to the program state after the fragment executes (and before the next fragement executes).

1. ```
thing1 = new Thing(99, "Hi");

thing2 = thing1;

```

a. How many Thing objects are there?

One. There is just one `new` expression (with one call to the constructor method for the Thing class). Both variables `thing1` and `thing2` refer to the same Thing object.

Write the value of each of the following expressions:
 b.  `thing1.i`     99 c.  `thing1.s`     "Hi" d.  `thing2.i`     99 e.  `thing2.s`     "Hi" f.  `thing1.i == thing2.i`     true g.  `thing1.s == thing2.s`     true h.  `thing1 == thing2`     true

Here `thing1` and `thing2` refer to the same object, so of course they are equal, and so are all their instance variables.

2. ```
thing2.s = "Bye";

```
Write the value of each of the following expressions:
 a.  `thing1.s`     "Bye" b.  `thing2.s`     "Bye" c.  `thing1 == thing2`     true

Here `thing1` and `thing2` refer to the same object, so changing an instance variable in `thing2` also changes it in `thing1`.

3. ```
thing2 = new Thing(99, "Bye");

```

a. How many Thing objects are there?

Two. We have executed a second `new` expression. Now `thing2` refers to the new object, which is different from the object referred to by `thing1`.

Write the value of each of the following expressions:
 b.  `thing1.s`     "Bye" c.  `thing2.s`     "Bye" d.  `thing1 == thing2`     false

Now `thing1 == thing2` is `false` because these two variables refer to different objects, even though the contents of the two objects (the values of all their instance variables) are the same.

4. ```
thing2.s = "Foo";

```
Write the value of each of the following expressions:
 b.  `thing1.s`     "Bye" c.  `thing2.s`     "Foo" d.  `thing1 == thing2`     false

Now `thing1` and `thing2` refer to different objects, so changing an instance variable in `thing2` does not change anything in `thing1`.

5. ```
list = new Thing(1, "A");

temp = new Thing(2, "B");

temp.next = list;

list = temp;

```
Write the value of each of the following expressions:
 a.  `list.i`     2 b.  `list.s`     "B" c.  `list.next.i`     1 c.  `list.next.s`     "A"

The first statement creates a Thing object and attaches it to `list`. The second statement creates another Thing object and the third and fourth statements insert it at the head of the list. Now `list` refers to the newer object, and `list.i` refers to its `i` instance variable. Also, `list.next` refers to the older object, and `list.next.i` refers to its `i` instance variable.

6. ```
temp = new Thing(3, "C");

temp.next = list;

list = temp;

```

This code creates another Thing object and inserts it at the head of the list.

```
System.out.print("list");

for (ref = list; ref != null; ref = ref.next) {

System.out.print(" -> " + ref.s);

}

System.out.println(".");

```

The `for` loop here traverses the list from the head to the tail, printing out the `s` instance variable in each object. The variable `ref` initially points at the head of the list. Each time the program executes the body of the loop, it points `ref` at the next object. When the program reaches the end of the list, `ref` becomes `null` and control exits from the loop.

1. Write the output that this fragment prints.

list -> C -> B -> A.

The object that was created last is at the head of the list, and the object that was created first is at the end of the tail.

2. Write a single Java statement (with no loops) that prints the value of the instance variable `i` in the object at the tail of the list.

System.out.println(list.next.next.i);

You cannot use `ref` to refer to the end of the list because now `ref` is `null`. Any attempt to use `ref` as a reference will cause the program to throw an exception.