Top 25 Coffee Collection Framework Interview Questions Answers For Freshers Too Experienced Programmers
Interview questions from Collection bundle or framework are most mutual inward whatsoever Core Java Interview yet a tricky one. Together Collection together with multithreading brand whatsoever Java interview tough to fissure together with having a proficient agreement of Collection together with threads volition aid y'all to excel inward Java interview. I idea most 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 kickoff wrote 10 multi-threading Interview questions simply somehow this article got delayed. Though I receive got shared several questions individually inward between.
I holler back the principal 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 lastly five years. In this article, nosotros volition catch a mix of unopen to 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 give the axe likewise live works life past times Google. BTW, if y'all are preparing for Java interview together with thus y'all tin give the axe likewise accept aid from Java Programming Interview Exposed, an splendid resources to produce good inward Java interviews.
I holler back the principal 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 lastly five years. In this article, nosotros volition catch a mix of unopen to 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 give the axe likewise live works life past times Google. BTW, if y'all are preparing for Java interview together with thus y'all tin give the axe likewise accept aid from Java Programming Interview Exposed, an splendid resources to produce good inward Java interviews.
Good Java Collection Interview Questions Answers
Now let's start amongst interview questions on collections. Since collection is made of diverse information structures e.g. Map, Set together with List together with at that spot diverse implementation, generally interviewer checks whether interviewee is familiar amongst basics of these collections or non together with whether he knows when to usage Map, Set or List. Based on Role for which interview is going on questions starts amongst beginner’s marker or to a greater extent than advanced level. Normally 2 to 3 years sense counted every bit beginners spell over five years comes nether advanced category, nosotros volition catch questions from both categories. 1. How does HashMap piece of occupation inward Java? (answer)
This is Classical Java Collection interview questions which I receive got likewise discussed inward my before article how does HashMap works 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 catch before link.
2. What is the divergence betwixt poll() together with remove() method of Queue interface? (answer)
Though both poll() together with remove() method from Queue is used to withdraw the object together with returns the caput of the queue, at that spot is a subtle divergence betwixt them. If Queue is empty() together with thus a telephone weep upward to remove() method volition throw Exception, spell a telephone weep upward to poll() method returns null. By the way, precisely which chemical factor is removed from the queue depends upon queue's ordering policy together with varies betwixt dissimilar implementation, for example, PriorityQueue keeps the lowest chemical factor every bit per Comparator or Comparable at caput position.
3. What is the divergence betwixt fail-fast together with fail-safe Iterators? (answer)
This is relatively new collection interview questions together with tin give the axe larn play a joke on if y'all listen the term fail-fast together with fail-safe kickoff time. Fail-fast Iterators throws ConcurrentModificationException when i Thread is iterating over collection object together with other thread structurally modify Collection either past times adding, removing or modifying objects on underlying collection. They are called fail-fast because they endeavour to straightaway throw Exception when they meet failure. On the other paw fail-safe Iterators works on re-create of collection instead of master copy collection
4. How produce y'all withdraw an entry from a Collection? together with afterward what is the divergence betwixt the remove() method of Collection and remove() method of Iterator, which i y'all volition usage spell removing elements during iteration?
Collection interface defines remove(Object obj) method to withdraw objects from Collection. List interface adds unopen to other method remove(int index), which is used to withdraw object at specific index. You tin give the axe usage whatsoever of these method to withdraw an entry from Collection, spell non iterating. Things change, when y'all iterate. Suppose y'all are traversing a List together with removing entirely surely elements based on logic, together with thus y'all demand to usage Iterator's remove() method. This method removes electrical current chemical factor from Iterator's perspective. If y'all usage Collection's or List's remove() method during iteration together with thus your code volition throw ConcurrentModificationException. That's why it's advised to usage Iterator remove() method to withdraw objects from Collection.
5. What is the divergence 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 agency to larn Synchronized re-create of collection e.g. ArrayList, HashMap past times using Collections.synchronizedMap() Utility function.One Significant divergence is that Concurrent Collections has ameliorate performance than synchronized Collection because they lock entirely a portion of Map to accomplish concurrency together with Synchronization. See the divergence betwixt Synchronized Collection together with Concurrent Collection inward Java for to a greater extent than details.
6. What is the divergence betwixt Iterator together with Enumeration? (answer)
This is a beginner marker collection interview questions together with generally asked during interviews of Junior Java developer upward 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 divergence is that Iterator is to a greater extent than prophylactic than Enumeration and doesn't allow unopen to 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 usage Hashing? (answer)
This is a tricky query inward Java because for hashing y'all demand both fundamental together with value and there is no fundamental for the shop it inward a bucket, together with thus how exactly HashSet store element internally. Well, HashSet is built on top of HashMap. If y'all await at source code of java.util.HashSet class, y'all volition reveal that that it uses a HashMap amongst same values for all keys, every bit shown below:
private transient HashMap map;
// Dummy value to associate amongst an Object inward the backing Map
private static lastly Object PRESENT = novel Object();
When y'all telephone weep upward add() method of HashSet, it pose 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 usage a custom object every bit a fundamental inward Collection classes similar Map or Set? (answer)
The respond is: If y'all are using whatsoever custom object inward Map every bit key, y'all demand to override equals() together with hashCode() method, together with brand surely 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 likewise demand to brand surely that your equals() method is consistent to compareTo() method, otherwise that collection volition non follow at that spot contacts e.g. Set may allow duplicates.
9. The divergence betwixt HashMap together with Hashtable? (answer)
This is unopen to other Classical Java Collection interview asked on beginner’s marker together with most of Java developer has a predefined respond for this interview questions e.g. HashMap is non synchronized spell Hashtable is non or hashmap is faster than hash tabular array etc. What could move past times incorrect is that if he placed another follow-up question similar how hashMap works inward Java or tin give the axe y'all supervene upon Hashtable with ConcurrentHashMap etc. See Hashtable vs HashMap inward Java for detailed respond of this interview question.
10. When produce y'all usage ConcurrentHashMap inward Java? (answer)
This is unopen to other advanced marker collection interview questions inward Java which usually asked to cheque whether the interviewer is familiar amongst optimization done on ConcurrentHashMap or not. ConcurrentHashMap is ameliorate suited for province of affairs where y'all receive got multiple readers together with one
Writer or fewer writers since Map gets locked entirely during the write operation. If y'all receive got an equal release of reader together with author than ConcurrentHashMap volition perform inward the line of piece of occupation of Hashtable or synchronized HashMap.
Another classical Java Collection interviews pop on telephonic circular or the kickoff circular of interview. Most of Java programmer knows that Set doesn't allowed duplicate spell List does together with List maintains insertion companionship spell Set doesn't. What is fundamental hither is to present the interviewer that y'all tin give the axe create upward one's heed which collection is to a greater extent than suited based on requirements.
This Collection interview query serves ii purpose it non entirely evidence 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 weep upward Collections.sort() it gets sorted based on the natural companionship specified inward compareTo() method spell Collections.sort(Comparator) volition form objects based on compare() method of Comparator.
13. What is the divergence betwixt Vector together with ArrayList? (answer)
One to a greater extent than beginner marker collection interview questions, this is yet rattling pop together with generally asked inward the telephonic round. ArrayList inward Java is i of the most used Collection shape together with the most interviewers asked questions on ArrayList. See Difference betwixt Vector together with ArrayList for the respond to this interview question.
14. What is the divergence betwixt HashMap together with HashSet? (answer)
This collection interview questions is asked inward conjunction amongst HashMap vs Hashtable. HashSet implements java.util.Set interface together with that's why entirely contains unique elements, spell HashMap allows duplicate values. In fact, HashSet is genuinely implemented on top of java.util.HashMap. If y'all await internal implementation of java.util.HashSet, y'all volition reveal that it adds chemical factor every bit fundamental on internal map amongst same values. For a to a greater extent than detailed answer, catch 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 fundamental together with higherKey() to render keys which is greater specified fundamental from a Map. It likewise furnish similar methods to larn entries e.g. lowerEntry(), floorEntry(), ceilingEntry() together with higherEntry(). Apart from navigation methods, it likewise 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 past times 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 likewise backed upward past times array, it offers unopen to usability payoff over array inward Java. Array is fixed length information structure, i time created y'all tin give the axe non modify it's length. On the other hand, ArrayList is dynamic, it automatically allocate a novel array together with copies content of former array, when it resize. Another argue of using ArrayList over Array is back upward of Generics. Array doesn't back upward Generics, together with if y'all shop an Integer object on a String array, y'all volition entirely going to know most it at runtime, when it throws ArrayStoreException. On the other hand, if y'all usage ArrayList, compiler together with IDE volition grab those fault on the spot. So if y'all know size inward advance together with y'all don't demand re-sizing than usage array, otherwise usage 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 amongst 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 fundamental together with higherKey() to render keys which is greater specified fundamental from a Map. It likewise furnish similar methods to larn entries e.g. lowerEntry(), floorEntry(), ceilingEntry() together with higherEntry(). Apart from navigation methods, it likewise 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 past times 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 likewise backed upward past times array, it offers unopen to usability payoff over array inward Java. Array is fixed length information structure, i time created y'all tin give the axe non modify it's length. On the other hand, ArrayList is dynamic, it automatically allocate a novel array together with copies content of former array, when it resize. Another argue of using ArrayList over Array is back upward of Generics. Array doesn't back upward Generics, together with if y'all shop an Integer object on a String array, y'all volition entirely going to know most it at runtime, when it throws ArrayStoreException. On the other hand, if y'all usage ArrayList, compiler together with IDE volition grab those fault on the spot. So if y'all know size inward advance together with y'all don't demand re-sizing than usage array, otherwise usage ArrayList.
17) Can nosotros supervene upon Hashtable amongst ConcurrentHashMap? (answer)
Answer 3: Yes nosotros tin give the axe supervene upon Hashtable amongst ConcurrentHashMap together with that's what suggested inward Java documentation of ConcurrentHashMap. simply y'all demand to live careful amongst code which relies on locking demeanour 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) works inward Hashtable simply non inward concurrentHashMap. instead of this usage putIfAbsent() method of ConcurrentHashMap
18) What is CopyOnWriteArrayList, how it is dissimilar than ArrayList together with Vector? (answer)
Answer: CopyOnWriteArrayList is novel List implementation introduced inward Java 1.5 which provides ameliorate concurrent access than Synchronized List. ameliorate concurrency is achieved past times Copying ArrayList over each write together with supervene upon amongst master copy instead of locking. Also CopyOnWriteArrayList doesn't throw whatsoever ConcurrentModification Exception. Its dissimilar than ArrayList because its thread-safe together with ArrayList is non thread-safe together with it's dissimilar than Vector inward damage of Concurrency. CopyOnWriteArrayList provides ameliorate Concurrency past times reducing tilt amid readers together with writers. Here is a prissy tabular array which compares performance of 3 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 ii pointers inward damage of previous together with adjacent telephone weep upward together with inward a seat to add together a novel chemical factor without affecting electrical current iteration.
20) When does ConcurrentModificationException occur on iteration? (answer)
When y'all withdraw object using Collection's or List's withdraw 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 whatsoever structural modify inward Collection e.g. adding or removing of the element, i time Iterator begins, it tin give the axe throw ConcurrentModificationException. Here are unopen to 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 3 of most pop information construction back upward inward Java. Set provides uniqueness guarantee i.e.g y'all tin give the axe non shop duplicate elements on it, simply it's non ordered. On the other paw List is an ordered Collection together with likewise allowes duplicates. Map is based on hashing together with stores fundamental together with value inward an Object called entry. It provides O(1) performance to larn object, if y'all know keys, if at that spot 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 fundamental divergence betwixt Set, List together with Map are that Map doesn't implement Collection interface, spell other ii does. For a to a greater extent than detailed answer, catch Set vs List vs Map inward Java
22) What is BlockingQueue, how it is dissimilar than other collection classes? (answer)
BlockingQueue is a Queue implementation available in java.util.concurrent package. It's i of the concurrent Collection shape added on Java 1.5, principal divergence between BlockingQueue and other collection classes is that apart from storage, it likewise provides menses control. It tin give the axe live used inward inter-thread communication together with likewise provides built-in thread-safety past times using happens-before guarantee. You tin give the axe usage 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 amongst answers (read)
Recommended Books
Java Programming Interview Exposed
Cracking the Coding Interview: 150 Programming Questions




Komentar
Posting Komentar