Top 50 collection interview questions along with brief answers and examples:
1. What is a Collection in Java?
- *Answer:* A Collection is an object that represents a group of objects, known as elements.
2. Explain the difference between List and Set.
- *Answer:* List allows duplicate elements and maintains order, while Set doesn't allow duplicates and doesn't guarantee order.
3. What is an ArrayList?
- *Answer:* ArrayList is a dynamic array that can grow or shrink in size, and it's part of the Java Collections Framework.
4. How does a HashSet handle duplicates?
- *Answer:* HashSet doesn't allow duplicate elements; it uses the equals() and hashCode() methods to check for duplicates.
5. Can you give an example of using a HashMap?
- *Answer:* HashMap<String, Integer> map = new HashMap<>(); map.put("One", 1);
--------------------------------------------------6. What is the difference between ArrayList and LinkedList?
- *Answer:* ArrayList uses a dynamic array, while LinkedList uses a doubly-linked list. ArrayList is better for random access, while LinkedList is efficient for insertions and deletions.
7. How does the TreeMap store elements?
- *Answer:* TreeMap stores elements in a sorted order based on their natural order or a specified comparator.
8. What is the purpose of the Comparable interface?
- *Answer:* The Comparable interface is used to define the natural order of elements. Objects that implement this interface can be compared and sorted.
9. Explain the concept of Iterator.
- *Answer:* Iterator is an interface in Java used to iterate over a collection. It provides methods like hasNext() and next().
10. What is the difference between fail-fast and fail-safe iterators?
- *Answer:* Fail-fast iterators throw ConcurrentModificationException if the collection is modified during iteration, while fail-safe iterators allow modifications without throwing exceptions.
--------------------------------------------------11. What is the difference between Iterator and ListIterator?
- *Answer:* Iterator can traverse elements in one direction, while ListIterator can traverse in both directions and allows modifications during iteration.
12. Explain the purpose of the Collections class in Java.
- *Answer:* The Collections class provides utility methods to operate on collections, such as sorting, shuffling, and searching.
13. How does the Arrays.asList() method work?
- *Answer:* Arrays.asList() converts an array into a fixed-size list backed by the original array.
14. What is the compareTo() method used for in the Comparable interface?
- *Answer:* compareTo() is used to compare the current object with another object, returning a negative, zero, or positive value based on the comparison.
15. Can you explain the concept of the equals()
method in Java?
- *Answer:* The equals() method is used to compare the content of objects for equality. It should be overridden in custom classes for meaningful comparison.
--------------------------------------------------16. What is the poll() method in the Queue interface?
- *Answer:* The poll() method retrieves and removes the head of the queue, returning null if the queue is empty.
17. How does the retainAll() method work in the Collection interface?
- *Answer:* retainAll() retains only the elements in the collection that are also present in the specified collection.
18. What is the purpose of the subList() method in the List interface?
- *Answer:* subList() returns a view of a portion of the list, allowing operations on the sublist to affect the original list.
19. How does the computeIfAbsent() method in the Map interface work?
- *Answer:* computeIfAbsent() computes the value for the specified key if it's not present or is null, using the provided mapping function.
20. Can you explain the difference between HashMap and HashTable ?
- *Answer:* HashMap is not synchronized and allows null values, while HashTable is synchronized but doesn't allow null values.
--------------------------------------------------21. What is the purpose of the Comparator interface in Java?
- *Answer:* The Comparator interface is used for custom sorting of objects. It provides a method, compare() , to define the sorting logic.
22. Explain the TreeSet class and how elements are stored in it.
- *Answer:* TreeSet is a NavigableSet implementation backed by a TreeMap. Elements are stored in sorted order based on their natural order or a specified comparator.
23. How does the copyOf() method in List interface work?
- *Answer:* List.copyOf() creates an immutable copy of the specified list. It helps prevent unintended modifications to the original list.
24. What is the purpose of the Deque interface, and how is it different from Queue?
- *Answer:* Deque is a double-ended queue that allows insertion and removal from both ends. It extends the Queue interface by providing more operations.
25. How does the ConcurrentHashMap differ from
HashMap in terms of concurrency?
- *Answer:* ConcurrentHashMap allows multiple threads to read without blocking but uses locks for updates, ensuring thread-safety during modifications.
--------------------------------------------------26. What is the purpose of the stream() method in the
Collection interface?
- *Answer:* The stream() method allows for processing elements in a collection in a functional style, facilitating operations like filtering, mapping, and reducing.
27. Explain the concept of a weak reference in Java.
- *Answer:* A weak reference is a reference that allows the associated object to be garbage-collected when there are no strong references to it.
28. How does the Arrays.copyOfRange() method work?
- *Answer:* Arrays.copyOfRange() copies the specified range of the original array into a new array. It provides a convenient way to create a subarray.
29. What is the purpose of the ListIterator interface in Java?
- *Answer:* ListIterator extends Iterator and allows bidirectional traversal of a list, along with the ability to modify elements during iteration.
30. How does the addAll() method in the Collection
interface differ from add() ?
- *Answer:* addAll() is used to add all elements from a specified collection to the invoking collection, whereas
add() adds a single element.
--------------------------------------------------31. What is the IdentityHashMap class, and how does it differ from HashMap ?
- *Answer:* IdentityHashMap uses reference equality (==) for key comparison, unlike HashMap which uses
equals() method.
32. Explain the purpose of the removeIf() method in the
Collection interface.
- *Answer:* removeIf() removes elements from a collection that satisfy a given condition, based on a provided predicate.
33. How does the LinkedHashSet differ from HashSet ?
- *Answer:* LinkedHashSet maintains the insertion order of elements, while HashSet does not guarantee any specific order.
34. What is the difference between a shallow copy and a deep copy of a collection?
- *Answer:* A shallow copy duplicates the collection structure but not the elements, while a deep copy duplicates both the structure and the elements.
35. Explain the concept of a fail-fast iterator.
- *Answer:* A fail-fast iterator detects concurrent modification during iteration and throws a ConcurrentModificationException to avoid inconsistent behavior.
--------------------------------------------------36. How does the toArray() method work in the Collection interface?
- *Answer:* toArray() converts a collection to an array. It can be called with no arguments or with an array of a specified type.
37. What is the purpose of the Arrays.fill() method in Java?
- *Answer:* Arrays.fill() sets every element of an array to a specified value, providing a quick way to initialize or reset array elements.
38. Explain the concept of the CopyOnWriteArrayList
class.
- *Answer:* CopyOnWriteArrayList is a thread-safe variant of ArrayList where every write operation creates a new copy of the underlying array.
39. How does the computeIfPresent() method in the Map interface work?
- *Answer:* computeIfPresent() computes the value for the specified key if it is present, using the provided mapping function.
40. What is the purpose of the Enumeration
interface in Java?
- *Answer:* Enumeration is an older interface used to iterate through elements in legacy collections, like Vector and Hashtable.
0 Comments
Thank You for comment
if you have any queries then Contact us k2aindiajob@gmail.com