Interview questions from Collection bundle or framework are most mutual inward whatever Core Java Interview yet a tricky one. Together Collection together with multithreading brand whatever Java interview tough to scissure together with having a expert agreement of Collection together with threads volition aid y'all to excel inward Java interview. I idea well-nigh writing interview questions on Java collection framework together with of import classes similar ArrayList, HashMap, Hashtable, together with newly added concurrent collections e.g. ConcurrentHashMap when I starting fourth dimension wrote 10 multi-threading Interview questions simply somehow this article got delayed. Though I receive got shared several questions individually inward between.
I intend the primary argue for this delay could me spending a lot of fourth dimension for researching together with preparing the mega listing of core Java questions from in conclusion five years. In this article, nosotros volition regard a mix of to a greater extent than or less beginners together with advanced Java Collection interviews together with their answers which receive got been asked inward diverse Core Java interviews.
These Java Collection framework interview questions receive got been collected from diverse friends together with colleagues together with Answers of these interview questions tin sack too last institute yesteryear Google. BTW, if y'all are preparing for Java interview together with therefore y'all tin sack too accept aid from Java Programming Interview Exposed, an fantabulous resources to produce good inward Java interviews.
I intend the primary argue for this delay could me spending a lot of fourth dimension for researching together with preparing the mega listing of core Java questions from in conclusion five years. In this article, nosotros volition regard a mix of to a greater extent than or less beginners together with advanced Java Collection interviews together with their answers which receive got been asked inward diverse Core Java interviews.
These Java Collection framework interview questions receive got been collected from diverse friends together with colleagues together with Answers of these interview questions tin sack too last institute yesteryear Google. BTW, if y'all are preparing for Java interview together with therefore y'all tin sack too accept aid from Java Programming Interview Exposed, an fantabulous resources to produce good inward Java interviews.
Good Java Collection Interview Questions Answers
Now let's start alongside interview questions on collections. Since collection is made of diverse information structures e.g. Map, Set together with List together with in that location diverse implementation, generally interviewer checks whether interviewee is familiar alongside basics of these collections or non together with whether he knows when to utilisation Map, Set or List. Based on Role for which interview is going on questions starts alongside beginner’s flat or to a greater extent than advanced level. Normally 2 to 3 years sense counted every bit beginners piece over five years comes nether advanced category, nosotros volition regard questions from both categories.
1. How does HashMap piece of work inward Java? (answer)
This is Classical Java Collection interview questions which I receive got too discussed inward my before article how does HashMap industrial plant inward Java. This collection interview questions is generally asked during AVP Role interviews on Investment-Banks together with has a lot of follow-up questions based on the response of interviewee e.g. Why HashMap keys demand to be immutable, what is race atmospheric condition on HashMap together with how HashMap resize inward Java. For explanation together with answers of these questions Please regard before link.
2. What is the deviation betwixt poll() together with remove() method of Queue interface? (answer)
Though both poll() together with remove() method from Queue is used to take the object together with returns the caput of the queue, in that location is a subtle deviation betwixt them. If Queue is empty() together with therefore a telephone telephone to remove() method volition throw Exception, piece a telephone telephone to poll() method returns null. By the way, precisely which chemical ingredient is removed from the queue depends upon queue's ordering policy together with varies betwixt unlike implementation, for example, PriorityQueue keeps the lowest chemical ingredient every bit per Comparator or Comparable at caput position.
3. What is the deviation betwixt fail-fast together with fail-safe Iterators? (answer)
This is relatively new collection interview questions together with tin sack move push clit a fast i on if y'all hear the term fail-fast together with fail-safe starting fourth dimension time. Fail-fast Iterators throws ConcurrentModificationException when i Thread is iterating over collection object together with other thread structurally modify Collection either yesteryear adding, removing or modifying objects on underlying collection. They are called fail-fast because they endeavour to at i time throw Exception when they come across failure. On the other paw fail-safe Iterators industrial plant on re-create of collection instead of master collection
4. How produce y'all take an entry from a Collection? together with later on what is the deviation betwixt the remove() method of Collection and remove() method of Iterator, which i y'all volition utilisation piece removing elements during iteration?
Collection interface defines remove(Object obj) method to take objects from Collection. List interface adds to a greater extent than or less other method remove(int index), which is used to take object at specific index. You tin sack utilisation whatever of these method to take an entry from Collection, piece non iterating. Things change, when y'all iterate. Suppose y'all are traversing a List together with removing exclusively for certain elements based on logic, together with therefore y'all demand to utilisation Iterator's remove() method. This method removes electrical flow chemical ingredient from Iterator's perspective. If y'all utilisation Collection's or List's remove() method during iteration together with therefore your code volition throw ConcurrentModificationException. That's why it's advised to utilisation Iterator remove() method to take objects from Collection.
5. What is the deviation betwixt Synchronized Collection together with Concurrent Collection? (answer)
Java five has added several novel Concurrent Collection classes e.g. ConcurrentHashMap, CopyOnWriteArrayList, BlockingQueue etc, which has made Interview questions on Java Collection fifty-fifty trickier. Java Also provided a means to larn Synchronized re-create of collection e.g. ArrayList, HashMap yesteryear using Collections.synchronizedMap() Utility function.One Significant deviation is that Concurrent Collections has improve performance than synchronized Collection because they lock exclusively a portion of Map to accomplish concurrency together with Synchronization. See the deviation betwixt Synchronized Collection together with Concurrent Collection inward Java for to a greater extent than details.
6. What is the deviation betwixt Iterator together with Enumeration? (answer)
This is a beginner flat collection interview questions together with generally asked during interviews of Junior Java developer upwardly to sense of 2 to 3 years Iterator duplicate functionality of Enumeration with i add-on of remove() method together with both furnish navigation functionally on objects of Collection.Another deviation is that Iterator is to a greater extent than rubber than Enumeration and doesn't allow to a greater extent than or less other thread to modify collection object during iteration except remove() method together with throws ConcurrentModificaitonException. See Iterator vs Enumeration inward Java for to a greater extent than differences.
7. How does HashSet is implemented inward Java, How does it utilisation Hashing? (answer)
This is a tricky query inward Java because for hashing y'all demand both telephone substitution together with value and there is no telephone substitution for the shop it inward a bucket, together with therefore how exactly HashSet store element internally. Well, HashSet is built on transcend of HashMap. If y'all expect at source code of java.util.HashSet class, y'all volition discover that that it uses a HashMap alongside same values for all keys, every bit shown below:
private transient HashMap map;
// Dummy value to associate alongside an Object inward the backing Map
private static terminal Object PRESENT = novel Object();
When y'all telephone telephone add() method of HashSet, it set entry inward HashMap :
public boolean add(E e) {
render map.put(e, PRESENT)==null;
}
Since keys are unique inward a HashMap, it provides uniqueness guarantee of Set interface.
8. What produce y'all demand to produce to utilisation a custom object every bit a telephone substitution inward Collection classes similar Map or Set? (answer)
The reply is: If y'all are using whatever custom object inward Map every bit key, y'all demand to override equals() together with hashCode() method, together with brand for certain they follow their contract. On the other paw if y'all are storing a custom object inward Sorted Collection e.g. SortedSet or SortedMap, y'all too demand to brand for certain that your equals() method is consistent to compareTo() method, otherwise that collection volition non follow in that location contacts e.g. Set may allow duplicates.
9. The deviation betwixt HashMap together with Hashtable? (answer)
This is to a greater extent than or less other Classical Java Collection interview asked on beginner’s flat together with most of Java developer has a predefined reply for this interview questions e.g. HashMap is non synchronized piece Hashtable is non or hashmap is faster than hash tabular array etc. What could move incorrect is that if he placed another follow-up question similar how hashMap industrial plant inward Java or tin sack y'all supersede Hashtable with ConcurrentHashMap etc. See Hashtable vs HashMap inward Java for detailed reply of this interview question.
10. When produce y'all utilisation ConcurrentHashMap inward Java? (answer)
This is to a greater extent than or less other advanced flat collection interview questions inward Java which usually asked to cheque whether the interviewer is familiar alongside optimization done on ConcurrentHashMap or not. ConcurrentHashMap is improve suited for province of affairs where y'all receive got multiple readers together with one
Writer or fewer writers since Map gets locked exclusively during the write operation. If y'all receive got an equal expose of reader together with author than ConcurrentHashMap volition perform inward the occupation of Hashtable or synchronized HashMap.
Another classical Java Collection interviews pop on telephonic circular or the starting fourth dimension circular of interview. Most of Java programmer knows that Set doesn't allowed duplicate piece List does together with List maintains insertion gild piece Set doesn't. What is telephone substitution hither is to exhibit the interviewer that y'all tin sack create upwardly one's hear which collection is to a greater extent than suited based on requirements.
This Collection interview query serves 2 purpose it non exclusively examine an of import programming concept Sorting simply also utility class similar Collections which furnish several methods for creating synchronized collection together with sorting. Sorting is implemented using Comparable together with Comparator inward Java together with when y'all telephone telephone Collections.sort() it gets sorted based on the natural gild specified inward compareTo() method piece Collections.sort(Comparator) volition form objects based on compare() method of Comparator.
13. What is the deviation betwixt Vector together with ArrayList? (answer)
One to a greater extent than beginner flat collection interview questions, this is yet real pop together with generally asked inward the telephonic round. ArrayList inward Java is i of the most used Collection degree together with the most interviewers asked questions on ArrayList. See Difference betwixt Vector together with ArrayList for the reply to this interview question.
14. What is the deviation betwixt HashMap together with HashSet? (answer)
This collection interview questions is asked inward conjunction alongside HashMap vs Hashtable. HashSet implements java.util.Set interface together with that's why exclusively contains unique elements, piece HashMap allows duplicate values. In fact, HashSet is genuinely implemented on transcend of java.util.HashMap. If y'all expect internal implementation of java.util.HashSet, y'all volition discover that it adds chemical ingredient every bit telephone substitution on internal map alongside same values. For a to a greater extent than detailed answer, regard HashMap vs HashSet.
15) What is NavigableMap inward Java? What is a produce goodness over Map? (answer)
NavigableMap Map was added inward Java 1.6, it adds navigation capability to Map data structure. It provides methods similar lowerKey() to larn keys which is less than specified key, floorKey() to render keys which is less than or equal to specified key, ceilingKey() to larn keys which is greater than or equal to specified telephone substitution together with higherKey() to render keys which is greater specified telephone substitution from a Map. It too furnish similar methods to larn entries e.g. lowerEntry(), floorEntry(), ceilingEntry() together with higherEntry(). Apart from navigation methods, it too provides utilities to create sub-Map e.g. creating a Map from entries of an exsiting Map similar tailMap, headMap together with subMap. headMap() method returns a NavigableMap whose keys are less than specified, tailMap() returns a NavigableMap whose keys are greater than the specified together with subMap() gives a NavigableMap betwixt a range, specified yesteryear toKey to fromKey.
16) Which i y'all volition prefer betwixt Array together with ArrayList for Storing object together with why? (answer)
Though ArrayList is too backed upwardly yesteryear array, it offers to a greater extent than or less usability wages over array inward Java. Array is fixed length information structure, i time created y'all tin sack non alter it's length. On the other hand, ArrayList is dynamic, it automatically allocate a novel array together with copies content of quondam array, when it resize. Another argue of using ArrayList over Array is back upwardly of Generics. Array doesn't back upwardly Generics, together with if y'all shop an Integer object on a String array, y'all volition exclusively going to know well-nigh it at runtime, when it throws ArrayStoreException. On the other hand, if y'all utilisation ArrayList, compiler together with IDE volition pick out handgrip of those mistake on the spot. So if y'all know size inward advance together with y'all don't demand re-sizing than utilisation array, otherwise utilisation ArrayList.
Further Learning
Java In-Depth: Become a Complete Java Engineer
read)
50+ Java Multithreading Questions from Last 3 years (the list)
50+ Programmer Phone Interview Questions alongside answers (read)
Recommended Books
Java Programming Interview Exposed
Cracking the Coding Interview: 150 Programming Questions
15) What is NavigableMap inward Java? What is a produce goodness over Map? (answer)
NavigableMap Map was added inward Java 1.6, it adds navigation capability to Map data structure. It provides methods similar lowerKey() to larn keys which is less than specified key, floorKey() to render keys which is less than or equal to specified key, ceilingKey() to larn keys which is greater than or equal to specified telephone substitution together with higherKey() to render keys which is greater specified telephone substitution from a Map. It too furnish similar methods to larn entries e.g. lowerEntry(), floorEntry(), ceilingEntry() together with higherEntry(). Apart from navigation methods, it too provides utilities to create sub-Map e.g. creating a Map from entries of an exsiting Map similar tailMap, headMap together with subMap. headMap() method returns a NavigableMap whose keys are less than specified, tailMap() returns a NavigableMap whose keys are greater than the specified together with subMap() gives a NavigableMap betwixt a range, specified yesteryear toKey to fromKey.
16) Which i y'all volition prefer betwixt Array together with ArrayList for Storing object together with why? (answer)
Though ArrayList is too backed upwardly yesteryear array, it offers to a greater extent than or less usability wages over array inward Java. Array is fixed length information structure, i time created y'all tin sack non alter it's length. On the other hand, ArrayList is dynamic, it automatically allocate a novel array together with copies content of quondam array, when it resize. Another argue of using ArrayList over Array is back upwardly of Generics. Array doesn't back upwardly Generics, together with if y'all shop an Integer object on a String array, y'all volition exclusively going to know well-nigh it at runtime, when it throws ArrayStoreException. On the other hand, if y'all utilisation ArrayList, compiler together with IDE volition pick out handgrip of those mistake on the spot. So if y'all know size inward advance together with y'all don't demand re-sizing than utilisation array, otherwise utilisation ArrayList.
17) Can nosotros supersede Hashtable alongside ConcurrentHashMap? (answer)
Answer 3: Yes nosotros tin sack supersede Hashtable alongside ConcurrentHashMap together with that's what suggested inward Java documentation of ConcurrentHashMap. simply y'all demand to last careful alongside code which relies on locking conduct of Hashtable. Since Hashtable locks whole Map instead of a portion of Map, chemical compound operations similar if(Hashtable.get(key) == null) put(key, value) industrial plant inward Hashtable simply non inward concurrentHashMap. instead of this utilisation putIfAbsent() method of ConcurrentHashMap
18) What is CopyOnWriteArrayList, how it is unlike than ArrayList together with Vector? (answer)
Answer: CopyOnWriteArrayList is novel List implementation introduced inward Java 1.5 which provides improve concurrent access than Synchronized List. improve concurrency is achieved yesteryear Copying ArrayList over each write together with supersede alongside master instead of locking. Also CopyOnWriteArrayList doesn't throw whatever ConcurrentModification Exception. Its unlike than ArrayList because its thread-safe together with ArrayList is non thread-safe together with it's unlike than Vector inward price of Concurrency. CopyOnWriteArrayList provides improve Concurrency yesteryear reducing tilt amidst readers together with writers. Here is a prissy tabular array which compares performance of iii of pop List implementation ArrayList, LinkedList together with CopyOnWriteArrayList inward Java:
19) Why ListIterator has added() method simply Iterator doesn't or Why to add() method is declared inward ListIterator together with non on Iterator. (answer)
Answer: ListIterator has added() method because of its mightiness to traverse or iterate inward both direction of the collection. it maintains 2 pointers inward price of previous together with side yesteryear side telephone telephone together with inward a seat to add together a novel chemical ingredient without affecting electrical flow iteration.
20) When does ConcurrentModificationException occur on iteration? (answer)
When y'all take object using Collection's or List's take method e.g. remove(Object element) or remove(int index), instead of Iterator's remove() method than ConcurrentModificationException occurs. As per Iterator's contract, if it honour whatever structural alter inward Collection e.g. adding or removing of the element, i time Iterator begins, it tin sack throw ConcurrentModificationException. Here are to a greater extent than or less tips to avoid ConcurrentModification inward Java.
21) Difference betwixt Set, List together with Map Collection classes? (answer)
java.util.Set, java.util.List together with java.util.Map defines iii of most pop information construction back upwardly inward Java. Set provides uniqueness guarantee i.e.g y'all tin sack non shop duplicate elements on it, simply it's non ordered. On the other paw List is an ordered Collection together with too allowes duplicates. Map is based on hashing together with stores telephone substitution together with value inward an Object called entry. It provides O(1) performance to larn object, if y'all know keys, if in that location is no collision. Popular impelmentation of Set is HashSet, of List is ArrayList together with LinkedList, together with of Map are HashMap, Hashtable together with ConcurrentHashMap. Another telephone substitution deviation betwixt Set, List together with Map are that Map doesn't implement Collection interface, piece other 2 does. For a to a greater extent than detailed answer, regard Set vs List vs Map inward Java
22) What is BlockingQueue, how it is unlike than other collection classes? (answer)
BlockingQueue is a Queue implementation available in java.util.concurrent package. It's i of the concurrent Collection degree added on Java 1.5, primary deviation between BlockingQueue and other collection classes is that apart from storage, it too provides menstruum control. It tin sack last used inward inter-thread communication together with too provides built-in thread-safety yesteryear using happens-before guarantee. You tin sack utilisation BlockingQueue to solve Producer Consumer problem, which is what is needed inward most of concurrent applications.
Further Learning
Java In-Depth: Become a Complete Java Engineer
read)
50+ Java Multithreading Questions from Last 3 years (the list)
50+ Programmer Phone Interview Questions alongside answers (read)
Recommended Books
Java Programming Interview Exposed
Cracking the Coding Interview: 150 Programming Questions