This post contains a set of 50 multiple-choice questions covering various aspects of the Java Collections Framework, including classes, concurrent classes, and interfaces. Take this Java Collections Quiz to test your knowledge on Java collections framework.
1. Which interface represents a resizable array of objects in Java Collections?
Answer:
Explanation:
The List interface represents an ordered collection or sequence, and it is typically implemented by classes like ArrayList and LinkedList, which are resizable arrays of objects.
2. Which of these is not a method defined in the Map interface?
Answer:
Explanation:
The add() method is not defined in the Map interface. Map interface includes methods like put(), get(), and remove() for manipulating key-value pairs.
3. What is the primary difference between HashSet and TreeSet in Java Collections?
Answer:
Explanation:
The main difference between HashSet and TreeSet is that TreeSet maintains its elements in a sorted order (either natural ordering or according to a Comparator) while HashSet does not guarantee any order.
4. What type of collection does the java.util.Stack class represent?
Answer:
Explanation:
The Stack class extends Vector and represents a last-in-first-out (LIFO) stack of objects. Despite its name, it is a List implementation.
5. Which of these interfaces is not part of the Java Collections Framework?
Answer:
Explanation:
Sortable is not an interface in the Java Collections Framework. List, SortedMap, and SortedSet are all interfaces within the framework.
6. Which class is used to create a synchronized version of a Map?
Answer:
Explanation:
Collections.synchronizedMap() method is used to return a synchronized (thread-safe) map backed by the specified map.
7. What is the purpose of the Comparable interface in Java Collections?
Answer:
Explanation:
The Comparable interface is used to define a natural ordering for objects of each class that implements it. It has a compareTo method used for sorting objects.
8. Which of these collection classes does not allow duplicate elements?
Answer:
Explanation:
HashSet does not allow duplicate elements. It is a collection that does not store duplicate values.
9. What is the initial capacity of a HashMap in Java Collections Framework?
Answer:
Explanation:
The default initial capacity of a HashMap is 16. This is the number of buckets in the hash table initially.
10. What exception is thrown when an object is not found in a PriorityQueue?
Answer:
Explanation:
NoSuchElementException is thrown if the PriorityQueue is empty and methods like remove() or element() are called.
11. Which of these classes implements a doubly-linked list in Java Collections?
Answer:
Explanation:
LinkedList is the implementation of a doubly-linked list in the Java Collections Framework.
12. What is a major feature of the ConcurrentHashMap class?
Answer:
Explanation:
ConcurrentHashMap allows concurrent modification of the Map from several threads without the need to block them.
13. Which method in the Collections class is used to reverse the order of elements in a List?
Answer:
Explanation:
Collections.reverse() is used to reverse the order of elements in a List.
14. In which scenario would you use a WeakHashMap?
Answer:
Explanation:
WeakHashMap is a hashtable-based implementation where the keys are weak references, allowing the key-value pairs to be garbage-collected when the key is no longer in ordinary use.
15. What is the purpose of the Collections.unmodifiableCollection() method?
Answer:
Explanation:
Collections.unmodifiableCollection() method is used to return an unmodifiable view of the specified collection. This means that the collection cannot be modified through this view.
16. Which class should be used for a resizable array that provides the ability to grow or shrink its size dynamically?
Answer:
Explanation:
ArrayList is an implementation of the List interface that uses a resizable array, which is expanded or contracted as needed.
17. What is the primary difference between the offer() and add() methods in a Queue?
Answer:
Explanation:
In Queue, the offer() method is typically preferred over add() as it returns false if the element cannot be added due to capacity restrictions, while add() throws an IllegalStateException.
18. Which of these classes provides a fail-fast iterator?
Answer:
Explanation:
ArrayList provides a fail-fast iterator, which means it will throw a ConcurrentModificationException if the collection is modified after the iterator is created, except through the iterator's own remove method.
19. What is the behavior of the poll() method in a Queue when the Queue is empty?
Answer:
Explanation:
The poll() method retrieves and removes the head of the queue, or returns null if the queue is empty.
20. What is the key characteristic of a Set in Java Collections?
Answer:
Explanation:
A Set is a collection that does not allow duplicate elements. It models the mathematical set abstraction.
21. Which Java collection class is thread-safe and does not allow null elements?
Answer:
Explanation:
Hashtable is a legacy class from the original version of Java. It is synchronized (thread-safe) and does not allow null keys or null values.
22. Which method in the List interface allows to replace an element at a specific position?
Answer:
Explanation:
The set(int index, E element) method replaces the element at the specified position in the list with the specified element.
23. What type of collection is returned by Collections.synchronizedList()?
Answer:
Explanation:
Collections.synchronizedList() wraps a given List and returns a synchronized (thread-safe) version of it.
24. What is the primary difference between ConcurrentSkipListMap and TreeMap?
Answer:
Explanation:
ConcurrentSkipListMap is a concurrent, thread-safe version of a skip list, while TreeMap is a non-synchronized, sorted map implementation.
25. What does the iterator of a CopyOnWriteArrayList return when the list is modified after the iterator is created?
Answer:
Explanation:
The iterator of a CopyOnWriteArrayList returns a "snapshot" of the list at the point the iterator was created, regardless of subsequent modifications.
26. Which Java collection is best suited for LIFO (Last-In-First-Out) operations?
Answer:
Explanation:
ArrayDeque is commonly used for stack operations and is more efficient than Stack. It provides a resizable array and allows null elements, making it suitable for LIFO operations.
27. What is the primary difference between Iterator and ListIterator?
Answer:
Explanation:
ListIterator extends Iterator with additional methods that allow bidirectional traversal of a list and the modification of elements.
28. What is the significance of the BlockingQueue interface in Java Collections?
Answer:
Explanation:
BlockingQueue is designed for cases where producers and consumers are different threads. Consumers can wait for producers to insert items and vice-versa.
29. Which of these is not a feature of the LinkedHashMap class?
Answer:
Explanation:
LinkedHashMap consumes more memory than HashMap because it maintains a linked list to keep track of insertion order.
30. What is the behavior of the retainAll() method in the Collection interface?
Answer:
Explanation:
The retainAll() method is used to retain only the elements in the collection that are contained in the specified collection.
31. Which of these interfaces is typically used for a collection that does not allow duplicate elements and for which order is not an issue?
Answer:
Explanation:
The Set interface models the mathematical set abstraction and is typically used for collections that do not allow duplicate elements and do not need to maintain any order.
32. What is the difference between the add and offer methods in the Queue interface?
Answer:
Explanation:
In the Queue interface, the add method throws an IllegalStateException if the element cannot be added due to capacity restrictions, while the offer method returns false in such cases.
33. Which method would you use to obtain the first element of a Set?
Answer:
Explanation:
Sets do not have a method to directly retrieve an element by its position because Sets typically do not guarantee an order. The iterator().next() method can be used to get the first element if iterating over the Set.
34. What does the Collections.synchronizedSet() method do?
Answer:
Explanation:
Collections.synchronizedSet() method is used to return a synchronized (thread-safe) set backed by the specified set.
35. Which class in Java Collections Framework is designed for cases where high-throughput and thread-safe implementation is required?
Answer:
Explanation:
CopyOnWriteArrayList is designed for environments where high-throughput is needed, and the array rarely changes but is frequently iterated. It provides a thread-safe variant of ArrayList.
36. What is the typical use of a PriorityBlockingQueue?
Answer:
Explanation:
PriorityBlockingQueue is a thread-safe priority queue implementation. It is used in scenarios where multi-threaded tasks need to process elements based on their priorities.
37. In Java Collections, what is the main difference between a HashMap and a Hashtable?
Answer:
Explanation:
HashMap allows null keys and null values, whereas Hashtable does not allow null keys or values and is synchronized.
38. Which interface should be used when you need a map and you wish to preserve the order of elements?
Answer:
Explanation:
LinkedHashMap maintains a linked list of the entries in the map, in the order in which they were inserted. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
39. What is a key characteristic of the ConcurrentLinkedQueue class?
Answer:
Explanation:
ConcurrentLinkedQueue is a thread-safe queue based on linked nodes and supports concurrent access from multiple threads without locking.
40. What does the Collections.emptySet() method return?
Answer:
Explanation:
Collections.emptySet() returns a special immutable empty set. This set is serializable and no elements can be added to it.
41. In Java Collections, what is the difference between the poll() and remove() methods of Queue interface?
Answer:
Explanation:
In Queue interface, poll() retrieves and removes the head of the queue, returning null if the queue is empty, whereas remove() retrieves and removes the head of the queue but throws an exception if the queue is empty.
42. What will happen if you try to sort a List that contains a null element?
Answer:
Explanation:
Attempting to sort a list containing null elements will result in a NullPointerException because null cannot be compared with non-null elements.
43. Which interface extends Queue and supports operations at both ends?
Answer:
Explanation:
Deque (double-ended queue) extends Queue and allows elements to be added or removed from both ends.
44. Which class is best suited for a FIFO (First-In-First-Out) queue implementation?
Answer:
Explanation:
ArrayDeque is more efficient than LinkedList for new standard FIFO (first-in-first-out) queue operations.
45. Which method in the Map interface is used to retrieve all keys contained in the map?
Answer:
Explanation:
The keySet() method in the Map interface is used to return a Set view of the keys contained in the map.
46. What is the difference between the size and capacity of an ArrayList?
Answer:
Explanation:
In an ArrayList, the size is the number of elements currently in the list, while the capacity is the size of the array buffer inside the ArrayList, which can potentially hold more elements than the current size.
47. Which of these classes is synchronized and is designed for use in multithreaded contexts?
Answer:
Explanation:
ConcurrentHashMap is a concurrent collection class that allows multiple threads to read and write concurrently and is designed to be used in multithreaded contexts.
48. What will Collections.max() return when applied to a Collection of Strings?
Answer:
Explanation:
Collections.max() returns the maximum element of the given collection, according to the natural ordering of its elements. For a string, it would be the one that comes last in lexicographical order.
49. Which interface is the root interface in the collection hierarchy?
Answer:
Explanation:
The Collection interface is the root interface of the Java Collections Framework. Most collections in Java are derived from this interface.
50. Which method do you need to implement when creating a class that can be used as a key in a HashMap?
Answer:
Explanation:
When creating a class that can be used as a key in a HashMap, it is crucial to properly override both equals() and hashCode() methods to ensure that duplicate keys are not allowed and the hashcode contract is maintained.