Monday, March 31, 2025

 ❇️Core Java Collection Interview Questions

1️⃣ Arrays vs. Collections Q1: What are the main limitations of Object arrays in Java? Q2: How do arrays differ from collections in Java? Q3: What are the key differences between an Array and an ArrayList? Q4: What are the key differences between an Array and a Vector? 2️⃣ Collection API and Framework Q5: What is the Collection API in Java? Q6: What is the Collection framework, and how does it differ from the Collection API? Q7: What is the difference between Collection and Collections? Q8: Can you explain the purpose of the Collection interface in Java? 3️⃣ List and Set Interfaces Q9: Explain the characteristics of the List interface. How does it differ from Set? Q10: What are the primary characteristics of the Set interface? Q11: Explain the difference between SortedSet and NavigableSet interfaces. 4️⃣ Queue and Map Interfaces Q12: What is the Queue interface and when would you use it? Q13: How does the Map interface differ from the Collection interface? Q14: What are the characteristics of the SortedMap interface, and how is it different from NavigableMap? 5️⃣ Collection Classes: ArrayList, LinkedList, Vector, and HashSet Q15: Describe the key features of the ArrayList class. Q16: What is the RandomAccess interface, and which classes implement it? Q17: Explain the difference between ArrayList and LinkedList. In what scenarios would you prefer one over the other? Q18: Describe the key features of the Vector class. How does it differ from ArrayList? Q19: What are the key differences between HashSet and LinkedHashSet? 6️⃣ Thread-Safety and Synchronization Q20: How would you make an ArrayList thread-safe in Java? Q21: What is the difference between an ArrayList and a Vector in terms of synchronization and performance? 7️⃣ Legacy Classes and Interfaces Q22: What are the legacy classes and interfaces in the Collections framework? Q23: What are the key differences between Enumeration and Iterator? 8️⃣ Iterators and Cursors Q24: What are the limitations of the Enumeration interface? Q25: How does Iterator differ from ListIterator in terms of functionality and usage? Q26: What is the relationship between ListIterator and Iterator? 9️⃣ Map Implementations Q27: What are the key characteristics of the HashMap class? Q28: How does HashMap differ from LinkedHashMap? Q29: What are the differences between HashMap and Hashtable? Q30: What is an IdentityHashMap, and how does it differ from HashMap? Q31: Explain the purpose of WeakHashMap in Java. 🔟 Comparison and Sorting Q32: What is the Comparable interface, and how does it differ from the Comparator interface? Q33: What is the main difference between using a HashSet and a TreeSet? Q34: How do you get a synchronized version of a Set or Map in Java? Q35: What is the difference between the size and capacity of a collection object in Java?


1️⃣ Arrays vs. Collections

Q1: Limitations of Object arrays?

  • Fixed size (cannot grow dynamically).

  • No built-in methods for searching, sorting, etc.

  • Only supports homogeneous data (unless declared as Object[]).

Q2: Arrays vs. Collections

  • Arrays: Fixed size, primitive/object support, faster for random access.

  • Collections: Dynamic size, only objects, rich API (e.g., add()remove()).

Q3: Array vs. ArrayList

  • Array: Fixed size, primitive support, no methods like add().

  • ArrayList: Resizable, only objects, implements List with methods like get().

Q4: Array vs. Vector

  • Array: Fixed size, not synchronized.

  • Vector: Dynamic, synchronized (thread-safe), legacy class.



2️⃣ Collection API and Framework

Q5: What is the Collection API?

  • A unified architecture (interfaces like ListSetQueue) to store/manipulate groups of objects.

Q6: Collection Framework vs. API?

  • Framework: Implementation classes (e.g., ArrayListHashSet).

  • API: Interfaces (e.g., CollectionList).

Q7: Collection vs. Collections

  • Collection: Root interface (ListSetQueue).

  • Collections: Utility class with static methods (sort()synchronizedList()).

Q8: Purpose of Collection Interface?

  • Defines core operations (add()remove()size()) for all collections.




3️⃣ List and Set Interfaces

Q9: List vs. Set

  • List: Ordered, duplicates allowed, index-based access.

  • Set: Unordered (except LinkedHashSet), no duplicates.

Q10: Set Characteristics

  • Unique elements, uses equals()/hashCode(). Implementations: HashSetTreeSet.

Q11: SortedSet vs. NavigableSet

  • SortedSet: Elements sorted in natural order.

  • NavigableSet: Extends SortedSet with methods like ceiling()floor().




4️⃣ Queue and Map Interfaces

Q12: Queue Interface

  • FIFO order. Used in producer-consumer scenarios. Implementations: LinkedListPriorityQueue.

Q13: Map vs. Collection

  • Map: Key-value pairs (HashMapTreeMap).

  • Collection: Single elements (ListSet).

Q14: SortedMap vs. NavigableMap

  • SortedMap: Keys sorted in natural order.

  • NavigableMap: Extends SortedMap with methods like lowerKey().




5️⃣ Collection Classes

Q15: ArrayList Features

  • Resizable array, fast random access, non-synchronized.

Q16: RandomAccess Interface

  • Marker interface for fast random access (e.g., ArrayListVector).

Q17: ArrayList vs. LinkedList

  • ArrayList: Faster for get(), slower for insertions.

  • LinkedList: Faster for add()/remove(), implements Queue.

Q18: Vector vs. ArrayList

  • Vector: Synchronized, thread-safe, slower.

  • ArrayList: Non-synchronized, faster.

Q19: HashSet vs. LinkedHashSet

  • HashSet: Unordered, faster.

  • LinkedHashSet: Maintains insertion order.



6️⃣ Thread-Safety

Q20: Thread-safe ArrayList

  • Use Collections.synchronizedList(new ArrayList()) or CopyOnWriteArrayList.

Q21: ArrayList vs. Vector Performance

  • Vector: Synchronization overhead → slower.

  • ArrayList: No synchronization → faster.




7️⃣ Legacy Classes

Q22: Legacy Classes

  • VectorStackHashtableEnumeration.

Q23: Enumeration vs. Iterator

  • Enumeration: Legacy, read-only.

  • Iterator: Modern, supports remove().




8️⃣ Iterators

Q24: Enumeration Limitations

  • No remove() method, not fail-fast.

Q25: Iterator vs. ListIterator

  • Iterator: Unidirectional (forward-only).

  • ListIterator: Bidirectional, supports add()set().




9️⃣ Map Implementations

Q27: HashMap

  • Key-value pairs, allows null keys/values, O(1) average time complexity.

Q28: HashMap vs. LinkedHashMap

  • LinkedHashMap: Maintains insertion order.

Q29: HashMap vs. Hashtable

  • HashMap: Non-synchronized, allows null.

  • Hashtable: Synchronized, no null keys/values.

Q30: IdentityHashMap

  • Uses == for key comparison (not equals()).

Q31: WeakHashMap

  • Keys are weak references (garbage-collected if not referenced).




🔟 Comparison & Sorting

Q32: Comparable vs. Comparator

  • Comparable: Natural ordering (compareTo()).

  • Comparator: Custom ordering (compare()).

Q33: HashSet vs. TreeSet

  • HashSet: O(1) operations, unordered.

  • TreeSet: O(log n), sorted (natural/comparator order).

Q34: Synchronized Set/Map

  • Collections.synchronizedSet(new HashSet()) or ConcurrentHashMap.

Q35: Size vs. Capacity

  • Size: Current number of elements.

  • Capacity: Underlying array length (ArrayList initial capacity=10).




Related Posts:


0 comments:

Post a Comment

Translate

Popular Posts

Total Pageviews

150,724