Quiz |
Posted (Thursdays) |
Given in class (Thursdays) |
| 24 | Feb |
2 | Mar |
Information about quizzes:
- On the date a quiz is given, a die will be thrown by a student in
the class.
- Books and notes are put away.
- A question very similar to the chosen published question will be written
on the board.
- You have 5 minutes to answer the question.
The questions are intended to be straightforward. If you keep up with
the material in the class, almost no preparation may be necessary.
The Collaboration Policy allows you to
study
in groups for the quizzes, but you are on your own when you take the quiz.
You will fare better on the quiz if you
try working the problems before looking at
the solutions. If you don't understand the question or its answer,
please get help.
Directions:
Consider the following class definition (as presented in lecture) and
then answer the questions below.
class ListItem {
public int number;
public ListItem next;
ListItem(int number, ListItem next) {
this.number = number;
this.next = next;
}
public String toString() {
if (next == null)
return (" " + number);
else
return (" " + number + next);
}
}
- Carefully draw an object and reference diagram corresponding to
the result of executing the following code.
If an object is "shared" by two or more lists, draw it only once
with multiple arrows pointing to it.
Remember that Java evaluates expressions left to right, but always
evaluates the parameters before making a procedure call.
ListItem a, b, c, d;
a = new ListItem(1,null);
b = new ListItem(2,null);
b.next = a;
c = new ListItem(3,b);
d = new ListItem(4, new ListItem(5, new ListItem(6, b)));
c = c.next;
Terminal.println("a = (" + a + ")");
Terminal.println("b = (" + b + ")");
Terminal.println("c = (" + c + ")");
Terminal.println("d = (" + d + ")");
- What output would be printed as the result of executing the
code given in Problem 1?
- A ListItem object (as defined above) is stored in memory as two
contiguous memory cells (contiguous means that they are consecutive,
one immediately following the other).
The first cell of the object corresponds to the number being held by the
ListItem, and the second cell contains the memory address of the next
ListItem. Draw an object and reference diagram for ListItems being
held in the following memory. There are two lists, one starting
at memory location 112, and the other starting at memory location 104.
In memory, the null value is represented as 0.
Memory Address | Memory Contents |
100 | 107 |
101 | 0 |
102 | 112 |
103 | 116 |
104 | 134 |
105 | 118 |
106 | 109 |
107 | 100 |
108 | 103 |
109 | 114 |
110 | 116 |
111 | 106 |
112 | 111 |
113 | 102 |
114 | 116 |
115 | 0 |
116 | 122 |
117 | 114 |
118 | 0 |
119 | 110 |
- In problem 3, which memory locations are not used
in either list?
- Suppose we store the value 118 into memory location 117.
Now what would the object and reference diagram look like?
Now which memory locations are not used in either list?
- Assuming that memory locations are allocated in order starting
from address 200, draw a memory table like the one in Problem 3
that would result at the end of the execution of the code in Problem
1. Which memory locations are not used in any of the lists?
[ solution ]
Last modified 21:14:11 CST 01 March 1999
by Ron K. Cytron