Java Collections
Java Collections
a. The Iterator interface declares only three methods: hasNext, next and remove.
b. The ListIterator interface extends both the List and Iterator interfaces.
c. The ListIterator interface provides forward and backward iteration capabilities.
d. The ListIterator interface provides the ability to modify the List during iteration.
e. The ListIterator interface provides the ability to determine its position in the List.
f. None of the above.
b. The ListIterator interface extends both the List and Iterator interfaces.
The ListIterator interface extends the Iterator interface and declares additional
methods to provide forward and backward iteration capabilities, List modification
capabilities and the ability to determine the position of the iterator in the List.
None of the above. The RandomAccess interface is a marker interface; so it does not
declare any methods. Its purpose is to provide information about the RandomAccess
capabilities of a List implementation. Generic list algorithms can check to see if an
instance of a List implements the RandomAccess marker interface. If not, then the
algorithm can avoid operations that require fast random access. Both Vector and
ArrayList implement the RandomAccess interface. LinkedList does not
implement RandomAccess.
3. In addition to implementing the List interface, which of the following also provides
methods to get, add and remove elements from the head and tail of the list without
specifying an index?
a. Collection
b. ArrayList
c. LinkedList
d. List
e. Vector
f. None of the above
LinkedList .A stack or queue must be implemented using a data structure that stores
the elements based on the order of insertion. Any data structure that is implemented using
a hashtable is not a good choice. The ArrayList and Vector are both implemented
using an internal array. Although an array allows elements to be easily organized based
on the order of insertion, an array does not allow the list of elements to be easily shifted
in memory as elements are appended to the tail of the list and removed from the head of
the list. The LinkedList is implemented using a doubly linked list that allows
elements to be easily appended to the tail of the list, and removed from the head of the
list.
LinkedHashMap .The iteration order of a Map is the order in which an iterator moves
through the elements of the Map. The iteration order of a LinkedHashMap is
determined by the order in which elements are inserted. When a reference to an existing
Map is passed as an argument to the constructor of LinkedHashMap, the
Collection.addAll method will add the elements of the existing Map to the new instance.
Since the iteration order of the LinkedHashMap is determined by the order of
insertion, the iteration order of the new LinkedHashMap must be the same as the
iteration order of the old Map.
7. Suppose that you would like to create a new instance of a class that implements the Set
interface, and you would like the new instance to be initialized with the elements of an
existing Set. If you would like the iteration order of the new Set to be the same as that of
the existing Set, then which concrete implementation of the Set interface should be used
for the new instance?
Hashtable
HashMap
HashSet
LinkedHashSet
TreeMap
TreeSet
None of the above.
9. import java.util.*;
class GFC114 {
public static void main (String[] args) {
Object a = new ArrayList();
System.out.print((a instanceof Collections)+",");
System.out.print((a instanceof Arrays)+",");
System.out.print(a instanceof List);
}}
What is the result of attempting to compile and run the program?
a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: false,true,true
e. Prints: true,false,false
f. Prints: true,false,true
g. Prints: true,true,false
h. Prints: true,true,true
i. None of the above
Prints: false,false,true .The Collections class is not the same as the Collection
interface. The Collections class contains a variety of methods used to work with
collections. For example, Collections.shuffle is used to randomly shuffle the elements of
a Collection. Similarly, the Arrays class provides utility methods for working with
arrays.
None of the above. The Iterator interface declares three methods: hasNext, next
and remove. The ListIterator interface was introduced in Java 1.2 along with the
Iterator interface. The ListIterator interface extends the Iterator interface
and declares additional methods to provide forward and backward iteration capabilities,
List modification capabilities and the ability to determine the position of the iterator in
the List. The ListIterator interface does not extend the List interface.
14. Which of the following are true statements?
a. The Enumeration interface was introduced with the collections framework with
Java 1.2.
b. The Enumeration interface declares only two methods: hasMoreElements and
nextElement.
c. The Iterator interface extends the Enumeration interface.
d. The Iterator interface declares a total of three methods.
15. Which of the following classes allow elements to be accessed in the order that they
were added?
a. HashMap
b. HashSet
c. Hashtable
d. TreeMap
e. TreeSet
f. LinkedHashMap
g. LinkedHashSet