Self Quiz 5

Which of these statements is true?

a. The hash code of an object is an integer that tells where that object should be stored in a hash table.

b. A hash table is an array of linked lists. When an object is stored in a hash table, it is added to one of these linked lists.

c. The object's hash code is the index of the position in the array where the object is stored.

d. All objects with the same hash code go into the same linked list.

e. In Java, every object obj has a method obj.hashCode() that is used to compute hash codes for the object.

f. If the object is to be stored in a hash table of size N, then the hash code that is used for the object is Math.abs(obj.hashCode())%N.

Select one or more:
a. Correct
b. Correct
c. Correct
d. Correct
e. Correct
f. Correct

The correct answer is: a., b., c., d., e., f.



Question 2

Which of the data types below does not allow duplicates?

Select one:
a. List
b. Vector
c. Stack
d. Set
e. LinkedList

The correct answer is: Set



Question 3

Which of the following data types do not have iterators?

Select one:
a. HashSet
b. TreeSet
c. Map
d. ArrayList
e. LinkedList

The correct answer is: Map



Question 4

Given 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");
   }
}
Which statement is correct?

Select one:

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.

The correct answer is: After all the four entries are added to the map, "123" is a key that corresponds to the value "Steve Yao".



Question 5

You can use the methods in the Collections class to:

Select one or more:

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.

The correct answer is: find the maximum object in a collection based on the compareTo method., find the maximum object in a collection using a Comparator object.



Question 6

The Collection interface is the base interface for …

Select one or more:

a. Set 
b. List 
c. ArrayList 
d. LinkedList 
e. Map

The correct answer is: Set, List, ArrayList, LinkedList



Question 7

The Map is the base interface for …

Select one or more:

a. TreeMap 
b. HashMap 
c. LinkedHashMap 
d. ArrayList
e. LinkedList

The correct answer is: TreeMap, HashMap, LinkedHashMap



Question 8

Which of the following statements are true?

Select one or more:

a. The Collection interface is the root interface for manipulating a collection of objects. 
b. The Collection interface provides the basic operations for adding and removing elements in a collection. 
c. The AbstractCollection class is a convenience class that provides partial implementation for the Collection interface. 
d. Some of the methods in the Collection interface cannot be implemented in the concrete subclass. In this case, the method would throw java.lang.UnsupportedOperationException, a subclass of RuntimeException. 
e. All interfaces and classes in the Collections framework are declared using generic type in JDK 1.5. 

The correct answer is: The Collection interface is the root interface for manipulating a collection of objects., The Collection interface provides the basic operations for adding and removing elements in a collection., The AbstractCollection class is a convenience class that provides partial implementation for the Collection interface., Some of the methods in the Collection interface cannot be implemented in the concrete subclass. In this case, the method would throw java.lang.UnsupportedOperationException, a subclass of RuntimeException., All interfaces and classes in the Collections framework are declared using generic type in JDK 1.5.



Question 9

To store non-duplicated objects in the order in which they are inserted, use ….

Select one:

a. HashSet
b. LinkedHashSet 
c. TreeSet
d. ArrayList
e. LinkedList

The correct answer is: LinkedHashSet



Question 10

Which of the following statements are true?

Select one or more:

a. The Comparable interface contains the compareTo method with the signature "public int compareTo(Object)". 
b. The Comparator interface contains the compare method with the signature "public int compare(Object, Object)". 
c. A Comparable object can compare this object with the other object. 
d. A Comparator object contains the compare method that compares two objects. 

The correct answer is: The Comparable interface contains the compareTo method with the signature "public int compareTo(Object)"., The Comparator interface contains the compare method with the signature "public int compare(Object, Object)"., A Comparable object can compare this object with the other object., A Comparator object contains the compare method that compares two objects.



Question 11

Which of the following statements are true?

Select one or more:

a. An ArrayList can grow automatically. 
b. An ArrayList can shrink automatically.
c. You can reduce the capacity of an ArrayList by invoking the trimToSize() method on the list. 
d. You can reduce the capacity of a LinkedList by invoking the trimToSize() method on the list.

The correct answer is: An ArrayList can grow automatically., You can reduce the capacity of an ArrayList by invoking the trimToSize() method on the list.



Question 12

Which of the following are correct methods in Map?

Select one or more:

a. put(Object key, Object value) 
b. put(Object value, Object key)
c. get(Object key) 
d. get(int index)

The correct answer is: put(Object key, Object value), get(Object key)


No comments:

Post a Comment