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.