1. Which of the following is not a part of the Java Collections Framework?
Answer:
Explanation:
HashTable is part of the Java Collections API but not a part of the Collections Framework introduced in Java 2. It is considered a legacy class.
2. What does the 'Set' interface primarily ensure?
Answer:
Explanation:
The Set interface models the mathematical set abstraction and ensures that no duplicate elements are stored.
3. Which class is used for resizable-array implementation of the List interface?
Answer:
Explanation:
ArrayList is used as it provides a resizable-array, which means that the size of the list can be increased or decreased dynamically.
4. Which of the following is true about the PriorityQueue class?
Answer:
Explanation:
PriorityQueue is a queue implementation that orders elements based on their natural ordering or by a Comparator provided at queue construction time.
5. What is the primary difference between a HashSet and a LinkedHashSet?
Answer:
Explanation:
LinkedHashSet extends HashSet, but it also maintains a doubly-linked list across all elements, ensuring that the order of insertion is maintained.
6. What interface represents a last-in-first-out (LIFO) stack of objects?
Answer:
Explanation:
Deque interface extends Queue and allows insertion and removal of elements at both ends, thus functioning as a LIFO stack.
7. What does the 'Map' interface in Java not allow?
Answer:
Explanation:
A Map cannot contain duplicate keys; each key can map to at most one value.
8. In a TreeMap, which of the following is true about the ordering of the elements?
Answer:
Explanation:
TreeMap stores its elements in a red-black tree, which orders them based on their natural ordering or by a comparator provided at map creation time.
9. Which of these methods deletes all the elements from a collection?
Answer:
Explanation:
The clear() method is used to delete all the elements of a collection, resetting its size to zero.
10. Which interface forms the root of the collection hierarchy in Java?
Answer:
Explanation:
The Collection interface is the root of the collection hierarchy. List, Set, and Queue interfaces extend Collection.
11. What will Collections.emptySet() return?
Answer:
Explanation:
Collections.emptySet() returns a special immutable empty set. No elements can be added to this set.
12. Which class provides a resizable array and implements the List interface?
Answer:
Explanation:
ArrayList provides a resizable-array, which can be expanded or contracted dynamically.
13. Which of the following is true about the hashCode() method?
Answer:
Explanation:
hashCode() is used by hash-based collections like HashSet, HashMap, etc., for efficiently locating objects.
14. Which of these is a FIFO (first-in-first-out) data structure?
Answer:
Explanation:
LinkedList can be used as a queue which follows FIFO order.
15. What is the initial capacity of a Vector class in Java Collections Framework?
Answer:
Explanation:
The default initial capacity of a Vector is 10. If no capacity is specified, it uses this default value.
16. Which of these is synchronized?
Answer:
Explanation:
Vector is synchronized, meaning it is thread-safe. The others are not synchronized.
17. Which method is used to retrieve but not remove the head of a Queue?
Answer:
Explanation:
peek() retrieves the head of the queue without removing it. If the queue is empty, it returns null.
18. Which of the following collections classes allows the storage of many null values?
Answer:
Explanation:
HashSet allows the storage of many null values. TreeSet and TreeMap do not allow null keys (and in the case of TreeSet, not even null values).
19. Which collection class is typically used to represent a 'LIFO' stack?
Answer:
Explanation:
ArrayDeque can be used as a stack and is more efficient than Stack class.
20. In a HashMap, when a key-value pair is stored, the key object is used as a ________.
Answer:
Explanation:
In HashMap, the key object's hashcode is used to find the bucket where the Entry object (which contains the key-value pair) will be stored.
21. The Collections Framework was introduced in which version of Java?
Answer:
Explanation:
The Collections Framework was a major addition in JDK 1.2.
22. What will be the result of invoking remove(0) on an empty LinkedList object?
Answer:
Explanation:
Attempting to remove an element from an empty list at any index will result in IndexOutOfBoundsException.
23. Which of these interfaces extends the Collection interface?
Answer:
Explanation:
The List interface extends Collection. Map is not a true Collection, and it does not extend Collection interface.
24. Which class implements a bit vector that allows users to set or clear individual bits?
Answer:
Explanation:
BitSet creates a special type of array that holds bit values and can increase in size as needed.
25. Which method in the Collections class is synchronized?
Answer:
Explanation:
synchronizedCollection() is used to return a synchronized (thread-safe) collection backed by the specified collection.
26. What is the behavior of the add() method in a Set collection?
Answer:
Explanation:
In a Set, the add() method will add the element only if it is not already present in the set, ensuring no duplicates.
27. What will happen if you try to sort a list that contains null elements?
Answer:
Explanation:
Attempting to sort a list that contains null elements will result in a NullPointerException, as null cannot be compared with non-null elements.
28. Which of these is not a part of the Collections Framework?
Answer:
Explanation:
Series is not a part of the Java Collections Framework. Iterator, Enumeration, and ListIterator are all interfaces provided in the framework.
29. What does the Iterator interface provide?
Answer:
Explanation:
The Iterator interface provides methods to iterate over the elements of a collection one at a time.
30. Which interface is used to represent a sequence where duplicates are allowed?
Answer:
Explanation:
The List interface allows a sequence of elements where duplicates are allowed.
31. What distinguishes a HashMap from a Hashtable in Java Collections?
Answer:
Explanation:
Hashtable is a legacy class and is synchronized; it does not allow any null key or null value. HashMap, part of the Java Collections Framework, allows one null key and multiple null values.
32. Which method in the List interface inserts the specified element at the specified position?
Answer:
Explanation:
The add(int index, E element) method inserts the element at the specified position in the list.
33. How does a ConcurrentHashMap differ from a HashMap?
Answer:
Explanation:
ConcurrentHashMap class does not allow null keys or values.
34. What is the default load factor of a HashMap in Java?
Answer:
Explanation:
The default load factor for a HashMap is 0.75, which offers a good trade-off between time and space costs.
35. Which of the following methods is defined in the Collection interface?
Answer:
Explanation:
add(E e) is a method defined in the Collection interface, used to add an element to the collection.
36. What will happen if two different HashMap keys have the same hashcode?
Answer:
Explanation:
If two keys have the same hashcode, they will be stored in the same bucket but as separate entries (this is called a collision).
37. Which of these is not a feature of the Java Collections Framework?
Answer:
Explanation:
The Java Collections Framework fully supports generics, which enhances type safety and reduces the need for type casting.
38. What is the primary difference between Comparable and Comparator interfaces?
Answer:
Explanation:
Comparable is implemented by a class to compare its instances, while Comparator is a separate class used to compare instances of different classes.
39. Which collection class provides constant-time performance for basic operations?
Answer:
Explanation:
HashSet offers constant time performance for basic operations like add, remove, contains, and size, assuming the hash function disperses elements properly.
40. What is the advantage of using a LinkedHashMap over a HashMap?
Answer:
Explanation:
LinkedHashMap maintains a doubly-linked list running through all its entries, thus maintaining the order of insertion.
41. What exception is thrown when an object is not found in a collection?
Answer:
Explanation:
NoSuchElementException is thrown by methods like nextElement() of Enumeration when no more elements exist.
42. Which of these methods is not part of the Map interface?
Answer:
Explanation:
The Map interface does not have an add() method; it uses put(Object key, Object value) to insert elements.
43. What does Collections.synchronizedList(list) return?
Answer:
Explanation:
Collections.synchronizedList(list) returns a synchronized (thread-safe) list backed by the specified list.
44. What is the primary purpose of the WeakHashMap class?
Answer:
Explanation:
WeakHashMap is a hashtable-based implementation that stores only weak references to its keys, allowing keys to be garbage-collected if they are no longer referenced elsewhere.
45. Which interface represents a queue that supports operations at both ends?
Answer:
Explanation:
Deque (double-ended queue) allows us to add or remove elements from both ends.
46. How does a CopyOnWriteArrayList differ from a regular ArrayList?
Answer:
Explanation:
CopyOnWriteArrayList creates a new array upon modification, thus allowing concurrent iterations without throwing ConcurrentModificationException.
47. What will Collections.unmodifiableList(list) return?
Answer:
Explanation:
Collections.unmodifiableList(list) returns an unmodifiable view of the specified list, which means no changes can be made to the List through this view.
48. Which of the following best describes the properties of a TreeMap?
Answer:
Explanation:
TreeMap does not allow null keys and maintains the keys in sorted order (natural order or using a comparator).
49. Which method would you use to obtain an array from a Collection?
Answer:
Explanation:
The toArray() method is used to obtain an array containing all of the elements in the collection.
50. 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.
Comments
Post a Comment
Leave Comment