Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 18:39:12 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 19: Java Data Structures

Sections All -- Comprehensives
1   Which of the data type below does not allow duplicates?

A. Set
B. List
C. Vector
D. Stack
E. LinkedList

2   Which of the data type below could be used to store elements in their natural order based on the compareTo method.

A. HashSet.
B. TreeSet
C. ArrayList.
D. LinkedList.
E. None of the above

3   Which of the following data types does not implement the Collection interface?

A. HashSet.
B. TreeSet
C. ArrayList.
D. LinkedList.
E. Map.

4   Which of the following data types do not have iterators?

A. HashSet.
B. TreeSet
C. ArrayList.
D. LinkedList.
E. Map.

5   If you want to store non-duplicated objects in the order in which they are inserted, you should use ___.

A. HashSet
B. LinkedHashSet
C. TreeSet
D. ArrayList
E. LinkedList

6   To get an iterator from a set, you may use the _______ method.

A. getIterator
B. findIterator
C. iterator
D. iterators

7   The elements in ________ are sorted.

A. TreeSet
B. List
C. TreeMap
D. HashSet
E. a and c

8   Stack is a subclass of __________.

A. ArrayList
B. LinkedList
C. AbstractList
D. Vector

9   Which data type should you use if you want to store duplicate elements and be able to insert or delete elements anywhere efficiently.

A. ArrayList
B. LinkedList
C. Vector
D. Set
E. Stack

10   To empty a Collection or a Map, you use the __________ method.

A. empty
B. clear
C. zero
D. setEmpty

11   The update methods are synchronized in the ___________ classe.

A. ArrayList
B. LinkedList
C. TreeMap
D. Vector
E. HashSet

12   Which of the following is correct to perform the set union of two sets s1 and s2.

A. s1.union(s2)
B. s1 + s2
C. s1.addAll(s2)
D. s1.add(s2)
E. None of the above

13   Which of the following is correct to perform the set difference of two sets s1 and s2.

A. s1.difference(s2)
B. s1 - s2
C. s1.subtract(s2)
D. s1.removeAll(s2)
E. None of the above

14   Which of the following is correct to perform the set intersection of two sets s1 and s2.

A. s1.intersect(s2)
B. s1.join(s2)
C. s1.retainAll(s2)
D. s1.intersection(s2)
E. None of the above

15   Which method do you use to test if an element is a set or list named x?

A. (element instanceof List) || (element instanceof Set)
B. x.in(element)
C. x.contain(element)
D. x.contains(element)
E. x.include(element)

16   Which method do you use to find the number of elements in a set or list named x?

A. x.length()
B. x.count()
C. x.counts()
D. x.size()
E. x.sizes()

17   Which method do you use to remove an element from the a set or list named x?

A. x.delete(element)
B. x.remove(element)
C. x.deletes(element)
D. x.removes(element)
E. None of the above

18   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     HastSet set1 = new HashSet();
     set1.add("red");
     Set set2 = set1.clone();
   }
}

A. Line 5 is wrong because a HashSet object cannot be cloned.
B. Line 5 has a syntax error because set1.clone() returns an Object. You have to cast it to Set in order to compile it.
C. The program will be fine if set1.clone() is replaced by (Set)set1.clone()
D. The program will be fine if set1.clone() is replaced by (Set)(set1.clone())
E. The program will be fine if set1.clone() is replaced by (HashSet)(set1.clone())

19   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     Set set1 = new HashSet();
     set1.add("red");
     Set set2 = set1.clone();
   }
}

A. Line 5 is wrong because the decalred type for set1 is Set and the clone method is protected in an instance of Set.
B. The program will be fine if set1.clone() is replaced by (HashSet)set1.clone()
C. The program will be fine if set1.clone() is replaced by (Set)((HashSet)set1).clone()
D. The program will be fine if set1.clone() is replaced by (HashSet)((HashSet)set1).clone()
E. The program will be fine if set1.clone() is replaced by (LinkedHashSet)((HashSet)set1).clone()

20   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     Map map = new HashMap();
     map.put("123", "John Smith");
     map.put("111", "George Smith");
     map.put("123", "Steve Yao");
     map.put("222", "Steve Yao");
   }
}

A. After all the four entries are added to the map, "123" is a key that corresponds to the value "John Smith".
B. After all the four entries are added to the map, "123" is a key that corresponds to the value "Steve Yao".
C. After all the four entries are added to the map, "Steve Yao" is a key that corresponds to the value "222".
D. After all the four entries are added to the map, "John Smith" is a key that corresponds to the value "123".
E. A runtime error occurs because two entries with the same key "123" are added to the map.

21   To create a set that consists of string elements "red", "green", and "blue", use


A. new HashSet({"red", "green", "blue"})
B. new HashSet(new String[]{"red", "green", "blue"})
C. new HashSet(Arrays.asList(new String[]{"red", "green", "blue"}))
D. new LinkedHashSet(Arrays.asList(new String[]{"red", "green", "blue"}))
E. new Set(Arrays.asList(new String[]{"red", "green", "blue"}))

22   You can use the methods in the Collections class to

A. find the maximum object in a collection based on the compareTo method.
B. find the maximum object in a collection using a Comparator object.
C. sort a collection.
D. shuffle a collection.
E. do a binary search on a collection.

23   To find a maximum object in an array of strings (e.g., String[] names = {"red", "green", "blue"}), use
A. Arrays.max(names)
B. Arrays.sort(names)
C. Collections.max(names)
D. Collections.max(Arrays.asList(names))
E. None of the above