Answers with Complete Solutions
What is the main purpose of the Java Collections Framework?
✔✔ To provide a set of reusable data structures and algorithms for managing collections of
objects.
How does a HashMap store its elements?
✔✔ It uses a combination of an array and linked list (or tree in case of high collisions) to store
key-value pairs.
What is the difference between an ArrayList and a LinkedList?
✔✔ An ArrayList provides fast random access but slow insertions/removals in the middle, while
a LinkedList allows fast insertions/removals but slower random access.
Which collection class does not allow duplicate elements?
✔✔ HashSet
How do you iterate over a HashMap?
1
,✔✔ By using an Iterator, forEach loop, or entrySet().
What happens when you insert a duplicate key in a HashMap?
✔✔ The old value associated with the key gets replaced by the new value.
Which Java collection maintains the order of insertion?
✔✔ LinkedHashMap
How does a TreeSet maintain order?
✔✔ It sorts elements in their natural ordering or according to a specified Comparator.
What is the load factor in a HashMap?
✔✔ It determines when the capacity of the HashMap should be increased to maintain
performance.
How does a ConcurrentHashMap differ from a HashMap?
✔✔ ConcurrentHashMap is thread-safe and allows concurrent access, while HashMap is not
synchronized.
2
, What is the default initial capacity of an ArrayList?
✔✔ 10
Which collection class provides a resizable array implementation?
✔✔ ArrayList
What is the difference between a HashSet and a TreeSet?
✔✔ HashSet does not maintain order, while TreeSet sorts elements in natural order or as defined
by a Comparator.
Which collection is best for LIFO (Last In, First Out) operations?
✔✔ Stack
How do you convert an array to a List in Java?
✔✔ Using `Arrays.asList(array)`.
What happens when a HashSet encounters duplicate elements?
3