Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Chapter Quizzes  arrow Chapter 16

Chapter 16

Collections and Iterators

This activity contains 15 questions.

Question 1.
A ListIterator is an example of an Iterator. What additional methods does the ListIterator interface provide?


Open Hint for Question 1 in a new window.
 
End of Question 1


Question 2.
Comparing List and Set Collections and the order of elements within...


Open Hint for Question 2 in a new window.
 
End of Question 2


Question 3.
Comparing the List and Set Collections...


Open Hint for Question 3 in a new window.
 
End of Question 3


Question 4.
Considering the Collections classes, would it be possible to implement the containsAll method using the contains method (and possibly others)?


Open Hint for Question 4 in a new window.
 
End of Question 4


Question 5.
In the ListIterator Collection the next and previous methods return references. What might be the advantage of this fact?


Open Hint for Question 5 in a new window.
 
End of Question 5


Question 6.
Say you need an array that has the ability to be extended and contracted during execution. You decide to use one of the Collection classes in lieu of your array. What difficulties might you encounter?


Open Hint for Question 6 in a new window.
 
End of Question 6


Question 7.

What does this code print?
import java.util.HashSet;
import java.util.Iterator;

public class Iter {

	Iter() {
		HashSet s = new HashSet();
		for (int i=1; i<7; i++)
			for (int j=0; j

Open Hint for Question 7 in a new window.
 
End of Question 7


Question 8.

What does this code print?
import java.util.Vector;
import java.util.Iterator;

public class Vect {

	Vect() {
		Vector v = new Vector();
		for (int i=0; i<4; i++)
			v.add(i, "st" + 4*i);

		Iterator i = v.iterator();
		int n = 0;
		while (i.hasNext()) {
			n += ((String)i.next()).
								   length();
		}
		System.out.println(n);
	}

	public static void main(String[] args) {
		new Vect();
	}

}

Open Hint for Question 8 in a new window.
 
End of Question 8


Question 9.

What does this program print?
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;

public class Iter {

	Iter() {
		ArrayList lst = new ArrayList();
		for (int i=1; i<=7; i++)
			lst.add(new Integer(i));
		lst = massage(lst);
		lst = massage(lst);
		System.out.println(lst.get(3));
	}

	private ArrayList massage(List lst) {
		ListIterator i = lst.listIterator();
		ArrayList lst2 = new ArrayList();
		while (i.hasNext()) {
			Object obj = i.next();
			lst2.add(0, obj);
		}
		return lst2;
	}

	public static void main(String[] args) {
		new Iter();
	}

}

Open Hint for Question 9 in a new window.
 
End of Question 9


Question 10.
What is printed by the following code?
import java.util.Vector;

public class Vect {

	Vect() {
		Vector v = new Vector(4);
		for (int i=1; i<5; i++)
			v.add("st" + i);
		System.out.println(v.get(1));
	}

	public static void main(String[] args) {
		new Vect();
	}

}


Open Hint for Question 10 in a new window.
 
End of Question 10


Question 11.

What is printed by this code?
import java.util.Vector;

public class Vect {

	Vect() {
		Vector v = new Vector(10);
		for (int i=0; i<10; i++)
			v.add(null);
		for (int i=0; i<10; i+=2) {
			v.set(i, "st" + i);
			v.set(i+1, "uv" + (i+1));
		}
		System.out.println(
			(String) v.get(4) + v.get(5));
	}

	public static void main(String[] args) {
		new Vect();
	}

}

Open Hint for Question 11 in a new window.
 
End of Question 11


Question 12.
What is printed by this code?
import java.util.Vector;

public class Vect {

	Vect() {
		Vector v = new Vector();
		for (int i=0; i<10; i++)
			v.add(0, "st" + i);

		System.out.println(
			(String) v.get(4) + v.get(5));
	}

	public static void main(String[] args) {
		new Vect();
	}

}


Open Hint for Question 12 in a new window.
 
End of Question 12


Question 13.

What is printed by this code?
import java.util.Vector;

public class Vect {

	Vect() {
		int[] a = { 3, 1, 4, 1, 
					5, 9, 2, 6 };
		Vector v = new Vector();
		for (int i=0; i

Open Hint for Question 13 in a new window.
 
End of Question 13


Question 14.

What is printed by this code?
import java.util.Vector;
import java.util.Iterator;

public class Vect {

	Vect() {
		Vector v = new Vector();
		for (int i=0; i<5; i++)
			v.add(i, "st" + 4*i);

		Iterator i = v.iterator();
		int n = 0;
		boolean bFlag = false;
		while (i.hasNext()) {
			if (bFlag)
				v.set(v.indexOf(i.next(),
							0),
							"uv" + --n);
			else
				i.next();
			bFlag = ! bFlag;
		}
		System.out.println(
				(String) v.get(2) +
 								  v.get(3));
	}

	public static void main(String[] args) {
		new Vect();
	}

}

Open Hint for Question 14 in a new window.
 
End of Question 14


Question 15.
What is the principal advantage (property) of the TreeSet class?


Open Hint for Question 15 in a new window.
 
End of Question 15





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page