CIT 594 Fourth Quiz
Spring 2002, Dave Matuszek
Name __________________________________________
  1. (15 points) If you implemented a deque as a linked list,
    1. Would it be better to use a singly-linked list or a doubly-linked list?
           A doubly-linked list

    2. Why?
           Because insertions and deletions may occur at either end.

  2. (10 points) Suppose a stack of Objects is implemented as an array. When an Object is popped off the stack, should the corresponding array element be set to null? Why or why not?
         Yes, to enable the Object to be garbage-collected.

  3. (20 points) Suppose s1, s2, and s3 have been declared to be of type Set. Write the Java instructions that will:

    1. Set s3 to the union of s1 and s2.
           s3 = new HashSet(s1);     // TreeSet would work as well
    2. Set s3 to the intersection of s1 and s2.
           s3 = new HashSet(s1);
    3. Set s3 to the set difference s1 - s2.
           s3 = new HashSet(s1);
    4. Test if Object obj is an element of the set s1.

  4. (5 points) What is a singleton?

         An immutable set containing a single object.

  5. (30 points) Assuming that books is a Collection of some sort, write code to (1) get an iterator for books, and (2) use the iterator to print out every item in this collection.

         Iterator iter = books.iterator();
         while (iter.hasNext()) {

  6. (10 points) What additional methods does a ListIterator have that an Iterator does not have?
         Most important:                          Also mentioned:
              hasPrevious()                              nextIndex()
              previous()                                   previousIndex()
  7. (10 points) All Collection implementations should have two constructors. Briefly describe these two constructors.
         A no-argument constructor, and
         A constructor that takes an arbitrary Collection as argument.