COP 3530 Section 5 EXAM
1 February 13, 2003
10 points
1. True of False for
Java Classes and/or Interfaces
15
pts.
2. What is the output of the following segment?
String out = "";
List list = new LinkedList();
ListIterator itr = list.listIterator();
itr.add(new Integer(1));
itr.add(new Integer(2));
itr.add(new Integer(3));
itr.add(new Integer(4));
itr = list.listIterator();
System.out.println(list);
itr.next();
itr.next();
itr.add(new Integer(37));
System.out.println(list);
itr.previous();
itr.remove();
itr.next();
println( itr.previous());
itr.set(new Integer(37));
System.out.println(list);
15 points
3. Write an O(n) (i.e. one pass through the list) method
void removeAll(LinkedList l, Object x);
that removes all occurences of x form the Linkedlist l.
15 points
4. Consider
class DNode {
Object data;
DNode previous;
DNode next;
DNode(){}
DNode(Object d, DNode p, DNode n) {
data = d;
previous = p;
next = n;
}
} // end DNode
Suppose that DNode head references the list head in a circular doubly linked list with a list head. Write the method
void removeLast(DNode head);
that removes the last DNode on the list. Include an error check if necessary.
15 points
5. Write a recursive method
int countAnds(ArrayList a, int right);
that returns the number of “and”’s in the ArrayList of Strings a. Do not use any global variables.
5 points
6. What is the running time of the following method?
void problem6(ArrayList a, int right) {
if( right < 0 ) return;
for( int i = 0; i < right; i++ ) {
a.set(i,a.get(i+1));
} // end for
problem6(a,right - 1);
}
15 points
7. Consider the NCAA data file of Assignment Two. Write a main method to output to the screen each winning team along with all the years that team won a game. To read the data you can use
BufferedReader in = new BufferedReader(new FileReader(“ncaa2002.data”);
String aLine;
while( (aLine = in.readLine()) != null ) {
10
pts.
8. Describe how to
construct a set whose iterator would return the teams from problem 7 in order
of the number of years that team won a game from the most to the fewest.