Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Chapter 16 Collections Maps and Iterators – absolute java test bank


 

Download  file with the answers

Not a member!
Create a FREE account here to get access and download this file with answers


Chapter 16
Collections, Maps, and Iterators
 Multiple Choice
1) A ____________ is a data structure for holding elements.
(a) Collection
(b) Iterator
(c) Vector
(d) Wildcard

2) A ___________ is used to specify a wide range of parameters.
(a) Collection
(b) Iterator
(c) Vector
(d) Wildcard

3) All of the following methods are part of the Collection interface except:
(a) Contains()
(b) toArray()
(c) retainAll()
(d) toString()

4) To use Java’s Vector class, you must import the ___________ package.
(a) java.Math
(b) java.Exception
(c) java.util
(d) java.lang

5) The primary interface/interfaces for a collection class is/are:
(a) Collection
(b) Set
(c) List
(d) All of the above

6) To change an existing element of a vector you must use the __________ method.
(a) add()
(b) set()
(c) change()
(d) mutate()

7) The method ___________ can be used to determine the number of elements in an ArrayList.
(a) length()
(b) vectorLength()
(c) size()
(d) vectorSize()

8) The method contains(Object o) of the ArrayList class returns a value of type:
(a) int
(b) char
(c) byte
(d) boolean

9) Memory management methods of the vector class include all of the following except:
(a) remove()
(b) trimToSize()
(c) capacity()
(d) size()

10) All of the following are members of Java’s Collection class except:
(a) Queue
(b) LinkedList
(c) Vector
(d) Set

11) The Collection class or interface that allows only unique elements is the ___________ class or interface.
(a) List
(b) Set
(c) Vector
(d) all of the above

12) If you want a class that implements the Set interface and do not need any methods beyond those in the Set interface, you can use the concrete class:
(a) Vector
(b) LinkedList
(c) HashSet
(d) TreeSet

13) If you want a class that implements the List interface and do not need any methods beyond those in the List interface, you can use the ____________ class.
(a) Vector
(b) LinkedList
(c) HashSet
(d) TreeSet

14) If you do not need efficient random access but need to efficiently move sequentially through the list, then use the _____________ class.
(a) Vector
(b) LinkedList
(c) HashSet
(d) TreeSet

15) The interface SortedSet and the concrete class _________ is designed for implementations of the Set interface that provide for rapid retrieval of elements.
(a) Vector
(b) LinkedList
(c) HashSet
(d) TreeSet

16) All of the following are methods of the Iterator interface except:
(a) clear()
(b) hasNext()
(c) next()
(d) remove()

17) The hasNext() method of the Iterator interface has a return value of:
(a) byte
(b) int
(c) boolean
(d) none of the above

18) If you want a class that implements the Map interface and do not need any methods beyond those in the Map interface, you can use the ____________ class.
(a) Vector
(b) AbstractMap<K,V>
(c) HashMap<K,V>
(d) HashSet

 True/False
1) A collection is a data structure for holding elements.

2) A Java collection is any class that implements the Collection interface.

3) is a wildcard used to specify a wide range of parameters.

4) All Collection classes are part of the java.util package.

5) Classes that implement the Set interface allows duplicate elements to occur.

6) The List interface is for collections that allow repetition of elements and do not impose an order on their elements.

7) The difference between the List interface and the Set interface is that the Set interface does not impose an order on its unique elements.

8) When an interface lists a method as “optional,” you do not need to implement it when defining a class that implements the interface.

9) For-each loops can not be used with Collection classes.

10) Elements of a vector must be of a primitive type, such as int, double, or char.

11) The AbstractSequentialList class provides efficient implementation of sequentially moving through the list at the expense of having inefficient implementation of random access to elements.

12) A vector can potentially encounter an ArrayIndexOutOfBoundsException.

13) An Iterator is an object that allows your code to produce the elements in a vector or other container one after the other, exactly once.

14) The V parameter in a HashMap<K,V> may be any object, but the K parameter must be a String.

 

15) The number of entries that may be added to a HashMap<K,V> object is limited to what is specified as the initial capacity in the constructor for the HashMap<K,V> object.

 Short Answer/Essay
1) Write a Java statement to create a vector with initial capacity of 50.
Answer: Vector v = new Vector(50);
2) Create a for loop to initially set the values of the vector created in number 1 above to the value of negative 1.0.
Answer:
for(int i=0; i < 49; i++)
v.add(i, new Double(-1.0));
3) Write a Java statement to resize the resulting vector in number 2 above to 100.
Answer: v.setSize(100);
4) Write a Java statement that changes the value of element 27 to 5.0 in the resulting vector in number 3 above.
Answer: v.set(26, new Double(5.0));
5) Write a Java statement to recover the unused memory in the resulting vector in number 4 above.
Answer: v.trimToSize();
6) What is the difference between a vector’s size and a vector’s capacity?
Answer: A vector’s size refers to the actual number of elements with values in the vector, whereas the vector’s capacity refers to the total number of memory locations reserved for the vector’s use.
7) What must you do before adding a primitive type to a vector?
Answer: The primitive type must be converted to its corresponding wrapper type. A primitive integer value of 37 can easily converted to a corresponding Integer object: new Integer(37);
8) Compare and contrast vectors and arrays.
Answer: Vectors have many built in features that help track the number of elements as well as provide memory management capabilities. A corresponding array structure would require you to write much of your own code to accomplish the same tasks. The biggest advantage of vectors over arrays is the capability to increase and decrease size as necessary. Arrays have a fixed size.
Arrays do not carry the restrictions on base type that vectors impose. The base type of an array can be of any type; the base type of a Vector is always of type Object. Therefore, storing primitive values requires a little more work because the appropriate wrapper class needs to be used.
9) What is the purpose of the Java Iterator interface?
Answer: The Iterator interface provides common methods for traversing, possibly modifying elements along the way, in a collection in some sequential order.
10) Write a method named displayList() that has one parameter of type Vector and has a return value of void. The method uses an iterator to display the contents of the Vector.
Answer:
public static void displayList(Vector vct)
{
Iterator i = vct.iterator();
while(i.hasNext())
System.out.println(i.next());
}
11) How can one retrieve all the values of a HashMap object?
Answer: h.values( ) returns all values as a Collection object.

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!