1.What Is The Java Collections Framework (JCF)?

Interfaces and classes for storing data in ways compatible with data structures (that is, memory-efficient).

2.What Are The Primary Interfaces In The Java Collections Framework?

Collection, List, Set Queue Map and all of their subinterfaces.

3.What is the difference between Collection and Collections in Java?

Collection: the root interface for data structures.

Collections: a utility class that has static methods.

4.Is There A difference among List, Set, and Map*

List: Sequential order, no duplication.

Set: No order, no duplication.

Map: Key value pairs--like a dictionary house keys in general practice--with unique keys.

5.What are the key differences between ArrayList and LinkedList?

ArrayList: Faster access to random locations but slow in the middle of things.

LinkedList: Quick updates anywhere on list, slower access than with less predictable block sizes.

6.What is the difference between HashSet, LinkedHashSet, and TreeSet?

HashSet: Unordered, fast.

LinkedHashSet: Maintains order of insertion (or addition)

TreeSet: A set in alphabetical or other order, depending on the key.

7.How Does HashMap Work in Java?

The key-value storage system uses buckets and hashing to handle collisions. With Java 8, it has been modified to handle collisions (e.g. using red-black trees).

8.What is the default capacity of an ArrayList?

10 (increases by 50% when full).

9.What are fail-fast and fail-safe iterators and how do they differ?

Fail-fast: throws a ConcurrentModificationException if modified during iteration

Fail-safe: permits modification (CopyOnWriteArrayList, ConcurrentHashMap).

10.Why is HashSet faster than TreeSet?

HashSet uses hashing (O(1)), while TreeSet uses a Red-Black tree (O(log n)).

11.What is the internal implementation of HashMap?

Behind the scenes: It's an array of buckets with linked lists for collision handling.

12.Why is the load factor in HashMap set to 0.75?

It's a trade-off between memory efficiency and speed (reduces rehashing costs).

13.What is the difference between Comparable and Comparator?

Comparable is for natural ordering (compareTo).

Comparator only does sorting (compare).

14.How Does ConcurrentHashMap Work Internally?

SEGMENT LOCKING. (Java 7), BUCKET LEVEL LOCKING. (Java 8) allow instantaneous access

15.What is the difference between synchronized and concurrent collections?

Synchronized: Slower, blocks threads (eg.Collections.synchronizedList). Concurrent: optimized for chucks of work (ConcurrentHashMap).

16.How does CopyOnWriteArrayList work?

When the list is modified, it creates a new copy for every call (which makes the program thread-safe). This causes a great shortfall in memory usage efficiency.

17.What is the difference between Hashtable and HashMap?

Hashtable: Hashtable is synchronized (slow).

HashMap: HashMap is not synchronized (faster).

18.Why should you override hashCode() and equals() in HashMap keys?

So that you act upon both the correct key and never retrieve any duplicates

19.How to make an ArrayList thread-safe?

Use Collections.synchronizedList() or CopyOnWriteArrayList.

20.What are weak keys in WeakHashMap?

If there are no strong references left, the keys are eligible for garbage collection. This is true of all of them in a WeakHashMap.

Advanced Level Questions

21.What are the differences between ConcurrentHashMap and SynchronizedMap?

ConcurrentHashMap: More efficient, with bucket-level locking. SynchronizedMap: locks entire collection.

22.How Does TreeMap Maintain Ordering?

It uses a Red-Black Tree, which orders elements in natural/comparator order.

23.How Does LinkedHashMap Maintain Insertion Order?

LinkedHashMap: Hash table and double chain.("

24.What Does remove() do in Iterator?

It Removes the last element returned by Iterator and Disallows exceptions when something goes wrong. It just bites! (So to speak.)

25.What is PriorityQueue, and how does it work?

A queque based on a minimum value of min-heap orders elements naturally in the queue or by comparing at run-time

26.What is BlockingQueue, and how does it help in multithreading?

It's one of the series of Implementations BlockingQueues: Full.put() will block until space is available, and (take() Empty BC) ditto for take. only blocking queue

27.What are IdentityHashMap and EnumMap?

IdentityHashMap: uses object identity values for keys. EnumMap: optimized to work with enum keys.

28.How Does The computeIfAbsent() Method Work In Java 8?

Adds value to a map only if key is missing.

29.How Does The Spliterator Work?

In parallel processing mode of a series of chunks

30.What are the performance considerations when choosing between different collection types?

Choose ArrayList for fast read, LinkedList for frequent inserts/deletes, Hash stark areas, Terence <>TreeSet for sorted data.

31. What is the difference between Collection and Collections in Java?

  • Collection: The collection framework is the root of the List, Set, and Queue classes.
  • Collections: A utility class, such as sort() or reverse(), that holds only static methods.

32. How is Iterator different from ListIterator?

  • Iterator: Only supports forward traversal.
  • ListIterator: You can go forwards and backwards. You can change the elements on the list.

33. What are Java′s different kinds of queues?

  • PriorityQueue: Sorts by priority.
  • Deque: A double-ended queue.
  • BlockingQueue: Supports thread-safe operations. Examples include: ArrayBlockingQueue, LinkedBlockingQueue.

34. What’s the difference between a Stack and a Queue?

  • Stack: Last In, First Out.
  • Queue: First In, First Out.

35. What is an Enumeration in Java?

  • A way to iterate over collections. It is used for iterating through collections (e.g., Vector, Hashtable).
  • A substitute for Iterator and ListIterator used in modern Java.

36. What is a LinkedHashMap in Java?

  • A HashMap with insertion order of elements on a doubly linked list.

37. What happens when a HashMap's capacity is exceeded?

  • It makes new changes (doubling capacity), rehashes elements, and puts them into new buckets.

38. How do you synchronize a List or a Set?

  • Use Collections.synchronizedList(myList or Collections.synchronizedSet(mySet).

39. What are the differences between a HashMap and a TreeMap?

  • HashMap: Not in order. O(1) lookup time.
  • TreeMap: The order of elements is O(log n) lookup time (Red-Black Tree).

40. What are the differences between Arrays and ArrayLists? - Array: Fixed size, faster to access. - ArrayList: Dynamic resizing, slower than the array but flexible.

41. How does the remove(Object o) method work in a Collection? - Removes the first occurrence.

42. What happens when two objects have the same hashCode() but are not equal? - One bucket of the same number holds the elements as a tree or a linked list.

43. What is a NavigableSet in Java? - It is a SortedSet with methods like lower(), higher(), ceiling(), and floor().

44. What’s the difference between poll() and remove() in a Queue? - poll(): This removes and returns the header element. When the queue is empty, it returns null. - remove(): Same as poll() but throws a NoSuchElementException if the queue is empty.

45. What's the point of the containsKey() and containsValue() methods in Map? - containsKey(): Makes certain that a given key exists. - containsValue(): Checks to see if a value exists (slower because it must search through all values).

46. What’s the difference between peek(), poll(), and remove() in a Queue? - peek(): Returns the head without removing it. - poll(): Returns the head, removes it, and returns null if empty. - remove(): Similar to poll() except if empty, it throws a NoSuchElementException.

47. What's the difference between IdentityHashMap and HashMap? - IdentityHashMap uses reference equality (==), while HashMap uses .equals().

48. How does TreeMap sort its elements? - It uses natural ordering (Comparable) or a custom comparator (Comparator).

49. What is a WeakHashMap in Java? - A HashMap where the keys are weak references and are removed from the map when not referenced any further.

50. What exactly is a ConcurrentLinkedQueue? - It's a thread-safe, non-blocking queue that relies on lock-free linked nodes to operate.

51. What are the advantages of ConcurrentHashMap over HashMap? - It uses segment-level locking in Java 7 and bucket-level locking in Java 8. - Multiple threads can make changes to different segments simultaneously.

52. What does Java 8 do to improve the performance of HashMap? - It uses balanced trees (Red-Black trees) instead of linked lists if the bucket size is > 8.

53. What are ConcurrentSkipListMap and TreeMap’s differences? - ConcurrentSkipListMap: A thread-safe sorted map that relies on a Skip List. - TreeMap: A Red-Black Tree that is not thread-safe.

54. What is BlockingQueue and its implementations? - A thread-blocking queue that fills the threads to keep the water level constant or empty. Examples include: LinkedBlockingQueue, ArrayBlockingQueue, PriorityBlockingQueue.

55. What is the difference between SoftReference, WeakReference, and PhantomReference? - SoftReference: Collected only when memory is low. - WeakReference: Collected at the next garbage collection if it has no strong references. - PhantomReference: Used for finalization and releasing resources.

56. How and when does CopyOnWriteArrayList give you thread safety? - After a list is changed, it copies the old list so that no thread can modify it while working with the new list.

57. What is the difference between TreeSet and HashSet? - TreeSet: Ordered, implemented using Red-Black Tree (O(log n) time). - HashSet: Unordered, using Hashing (O(1) time).

58. What is a BitSet? - An efficient bit-sponging tool.

59. What is the forEach() method in Java 8 collections? - It uses lambda expressions to iterate over a collection (e.g., list.forEach(System.out::println)).

60. What's the difference between unmodifiable and immutable collections? - Unmodifiable: A wrapper that prevents any changes (e.g., Collections.unmodifiableList()). - Immutable: Can't be changed in any way (e.g.,List.of() ,Set.of(0).

61. Why are HashMap keys immutable in Java? 
- To prevent inconsistent behavior from 'hashCode' changes.

62. What is a ConcurrentHashMap compute() method? - It allows atomic updates of the value associated with a key. (e.g., map.compute(key, (k, v) -> v + 1)).

63. How does the parallelStream() method in Java 8 work? - Splits the collection into segments and processes them in parallel using the Fork/Join system.

64. Why is EnumMap faster than HashMap for Enums? - It uses an array-based internal storage system, avoiding the overhead of computing hashes.

65. What’s the difference between TreeSet and LinkedHashSet? -

  TreeSet: Ordered set (O(log n)). 

- LinkedHashSet:Maintains insertion order (O(1)).


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu