Java Collections short interview FAQs with answers – Real

List of Java Collection short interview questions and answer asked in software job / technical interviews. This list is helpful for telephonic or face to face technical interviews for freshers and experience developers/professionals. These interview questions are collected from various job interviews


Q-What is difference between Add() and Offer() methods of a queue?

Answer – The difference is that offer() will return false if it fails to insert the element on a size restricted Queue, whereas add() will throw an IllegalStateException.

You should use offer() when failure to insert an element would be normal, and add() when failure would be an exceptional occurrence (that needs to be handled).


Q-How to make a Map or List as Thread-Safe or Synchronized Collection?

Answer – We can use Java Collections class method in following way

To synchronized List   : Collections.synchronizedList(list);

To synchronized Map : Collections.synchronizedMap(map);


Q-How to make a List ( ArrayList, Vector, LinkedList ) read only in java?

Answer – We can make a list read only by using Collections.unmodifiableCollection(Collection myCollection) method.

Advantage of having read only collection is that it cannot be modified. So, any method which access the collection object would not be able to modify accidentally. As soon as if function tries to modify the object, an UnsupportedOperationException java exception will be thrown.


Q-Which collection class have non synchronized methods and as well as it can access its elements with the help of index directly in java.

Answer – ArrayList.


Q-What exception you may get on using java ArrayList in multi-threaded environment?

Answer: We may get Concurrent modification exception. As In java, ArrayList is not synchronized if two or more threads try to update the ArrayList at the same time then Concurrent modification exception will occur.


Q – What is the time complexity of sorting of objects into an array in Java?

Answer: Sorting of objects in any array will take O(nlog(n)) time. As, Arrays.sort() method internally uses merge sort() and merge sort have time complexity of O(nlog(n)).


Q – What is the data structure of ArrayList in java?

Answer: Grow able array.


Q – Name some Interfaces available in collection framework?

Answer: Here are some Interfaces available in Java Collection framework.

  • Collection
  • List
  • Set
  • SortedSet
  • NavigableSet
  • Queue
  • Dequeue
  • Map
  • SortedMap
  • NavigableMap

Tips: Try to get some basic details about the interface, as in an interview, you may be dragged a little bit deeper.


Q-What data structure is used for a priority queue in java?

Answer: In Java Priority Queue has been implemented using Heap data structure.


 Q-What changes has been made related to Java HashMap from JDK 1.7 to JDK 1.8?

Answer –  In case of hashing Collision  occur, java uses Linked List for mapping the keys- value pair entry. In JDK 1.8, a tree data structure has been used to implement it for better efficiency as Linked list has linear i.e.  O(n) time complexity and tree has O (log n).


Q-What interface you can use to remove duplicate values?

Answer-  Since, Set interface does not allow duplicate values it can be used to filter the duplicate values.


 Q-What is Deque?

Answer –  A double-ended-queue is a queue that supports the addition and removal of elements from both ends i.e. from head and tail. It is also pronounced as deck.

  • This is an interface and can be implemented with the classes ArrayDeque, LinkedList, and LinkedBlockingDeque.
  • Deque can also be used as a stacks to follow FILO policy. For this we can choose any one end of the deque and perform push and pop operations using methods addLast() and removeLast()respectively or addFirst() and removeFirst() methods. 

Q-Which interface make use of key value pair in java?

Answer: Map interface.


Q-Which interface does not extends collection interface?

Answer– Map interface does not extends collection interface.


Q-What is output of the below program on iterating the list?

Linklist l = newl Linklist();

l.add(“A”);
l.add(“B”);
l.add(“C”);
l.add(0,”D”);

Answer:  D A B C.

Explanation: Statement l.add(0,”D”); will insert the element “D” at the zero position. Note that the element will not shifted in the linked list like Arraylist.


Q-Can you insert NULL as a key into Java HashMap?

Answer:  Yes, we can insert null key in a HashMap. Note that we can insert only one null key into a HashMap.

Related Posts