Top 25 Coffee Collection Framework Interview Questions Answers For Freshers As Well As Experienced Programmers
Sunday, July 1, 2018
Add Comment
Interview questions from Collection parcel or framework are most mutual inwards whatsoever Core Java Interview yet a tricky one. Together Collection as well as multithreading brand whatsoever Java interview tough to scissure as well as having a practiced agreement of Collection as well as threads volition aid you lot to excel inwards Java interview. I idea close writing interview questions on Java collection framework as well as of import classes similar ArrayList, HashMap, Hashtable, as well as newly added concurrent collections e.g. ConcurrentHashMap when I offset wrote 10 multi-threading Interview questions just somehow this article got delayed. Though I conduct hold shared several questions individually inwards between.
I mean value the chief argue for this delay could me spending a lot of fourth dimension for researching as well as preparing the mega listing of core Java questions from in conclusion v years. In this article, nosotros volition regard a mix of unopen to beginners as well as advanced Java Collection interviews as well as their answers which conduct hold been asked inwards diverse Core Java interviews.
These Java Collection framework interview questions conduct hold been collected from diverse friends as well as colleagues as well as Answers of these interview questions tin likewise live on institute yesteryear Google. BTW, if you lot are preparing for Java interview therefore you lot tin likewise conduct hold aid from Java Programming Interview Exposed, an fantabulous resources to do good inwards Java interviews.
I mean value the chief argue for this delay could me spending a lot of fourth dimension for researching as well as preparing the mega listing of core Java questions from in conclusion v years. In this article, nosotros volition regard a mix of unopen to beginners as well as advanced Java Collection interviews as well as their answers which conduct hold been asked inwards diverse Core Java interviews.
These Java Collection framework interview questions conduct hold been collected from diverse friends as well as colleagues as well as Answers of these interview questions tin likewise live on institute yesteryear Google. BTW, if you lot are preparing for Java interview therefore you lot tin likewise conduct hold aid from Java Programming Interview Exposed, an fantabulous resources to do good inwards 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 as well as List as well as at that topographic point diverse implementation, by as well as large interviewer checks whether interviewee is familiar amongst basics of these collections or non as well as whether he knows when to work Map, Set or List. Based on Role for which interview is going on questions starts amongst beginner’s flat or to a greater extent than advanced level. Normally 2 to 3 years sense counted equally beginners spell over v years comes nether advanced category, nosotros volition regard questions from both categories.
1. How does HashMap piece of work inwards Java? (answer)
This is Classical Java Collection interview questions which I conduct hold likewise discussed inwards my before article how does HashMap industrial plant inwards Java. This collection interview questions is by as well as large asked during AVP Role interviews on Investment-Banks as well as 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 weather on HashMap as well as how HashMap resize inwards Java. For explanation as well as answers of these questions Please regard before link.
2. What is the divergence betwixt poll() as well as remove() method of Queue interface? (answer)
Though both poll() as well as remove() method from Queue is used to take the object as well as returns the caput of the queue, at that topographic point is a subtle divergence betwixt them. If Queue is empty() therefore a telephone weep upwards to remove() method volition throw Exception, spell a telephone weep upwards to poll() method returns null. By the way, just which chemical ingredient is removed from the queue depends upon queue's ordering policy as well as varies betwixt dissimilar implementation, for example, PriorityQueue keeps the lowest chemical ingredient equally per Comparator or Comparable at caput position.
3. What is the divergence betwixt fail-fast as well as fail-safe Iterators? (answer)
This is relatively new collection interview questions as well as tin larn play a joke on if you lot take heed the term fail-fast as well as fail-safe offset time. Fail-fast Iterators throws ConcurrentModificationException when 1 Thread is iterating over collection object as well as other thread structurally modify Collection either yesteryear adding, removing or modifying objects on underlying collection. They are called fail-fast because they endeavor to right away throw Exception when they regard failure. On the other paw fail-safe Iterators industrial plant on re-create of collection instead of master copy collection
4. How do you lot take an entry from a Collection? as well as afterwards what is the divergence betwixt the remove() method of Collection and remove() method of Iterator, which 1 you lot volition work spell removing elements during iteration?
Collection interface defines remove(Object obj) method to take objects from Collection. List interface adds unopen to other method remove(int index), which is used to take object at specific index. You tin work whatsoever of these method to take an entry from Collection, spell non iterating. Things change, when you lot iterate. Suppose you lot are traversing a List as well as removing alone for certain elements based on logic, therefore you lot demand to work Iterator's remove() method. This method removes electrical flow chemical ingredient from Iterator's perspective. If you lot work Collection's or List's remove() method during iteration therefore your code volition throw ConcurrentModificationException. That's why it's advised to work Iterator remove() method to take objects from Collection.
5. What is the divergence betwixt Synchronized Collection as well as Concurrent Collection? (answer)
Java v 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 yesteryear using Collections.synchronizedMap() Utility function.One Significant divergence is that Concurrent Collections has amend performance than synchronized Collection because they lock alone a portion of Map to accomplish concurrency as well as Synchronization. See the divergence betwixt Synchronized Collection as well as Concurrent Collection inwards Java for to a greater extent than details.
6. What is the divergence betwixt Iterator as well as Enumeration? (answer)
This is a beginner flat collection interview questions as well as by as well as large asked during interviews of Junior Java developer upwards to sense of 2 to 3 years Iterator duplicate functionality of Enumeration with 1 improver of remove() method as well as both supply navigation functionally on objects of Collection.Another divergence is that Iterator is to a greater extent than condom than Enumeration and doesn't allow unopen to other thread to modify collection object during iteration except remove() method as well as throws ConcurrentModificaitonException. See Iterator vs Enumeration inwards Java for to a greater extent than differences.
7. How does HashSet is implemented inwards Java, How does it work Hashing? (answer)
This is a tricky query inwards Java because for hashing you lot demand both primal as well as value and there is no primal for the shop it inwards a bucket, therefore how exactly HashSet store element internally. Well, HashSet is built on hit of HashMap. If you lot await at source code of java.util.HashSet class, you lot volition discovery that that it uses a HashMap amongst same values for all keys, equally shown below:
private transient HashMap map;
// Dummy value to associate amongst an Object inwards the backing Map
private static in conclusion Object PRESENT = novel Object();
When you lot telephone weep upwards add() method of HashSet, it seat entry inwards HashMap :
public boolean add(E e) {
render map.put(e, PRESENT)==null;
}
Since keys are unique inwards a HashMap, it provides uniqueness guarantee of Set interface.
8. What do you lot demand to do to work a custom object equally a primal inwards Collection classes similar Map or Set? (answer)
The reply is: If you lot are using whatsoever custom object inwards Map equally key, you lot demand to override equals() as well as hashCode() method, as well as brand for certain they follow their contract. On the other paw if you lot are storing a custom object inwards Sorted Collection e.g. SortedSet or SortedMap, you lot likewise demand to brand for certain that your equals() method is consistent to compareTo() method, otherwise that collection volition non follow at that topographic point contacts e.g. Set may allow duplicates.
9. The divergence betwixt HashMap as well as Hashtable? (answer)
This is unopen to other Classical Java Collection interview asked on beginner’s flat as well as most of Java developer has a predefined reply 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 travel incorrect is that if he placed another follow-up question similar how hashMap industrial plant inwards Java or tin you lot supervene upon Hashtable with ConcurrentHashMap etc. See Hashtable vs HashMap inwards Java for detailed reply of this interview question.
10. When do you lot work ConcurrentHashMap inwards Java? (answer)
This is unopen to other advanced flat collection interview questions inwards Java which unremarkably asked to banking concern agree whether the interviewer is familiar amongst optimization done on ConcurrentHashMap or not. ConcurrentHashMap is amend suited for province of affairs where you lot conduct hold multiple readers as well as one
Writer or fewer writers since Map gets locked alone during the write operation. If you lot conduct hold an equal let out of reader as well as author than ConcurrentHashMap volition perform inwards the business of Hashtable or synchronized HashMap.
Another classical Java Collection interviews pop on telephonic circular or the offset circular of interview. Most of Java programmer knows that Set doesn't allowed duplicate spell List does as well as List maintains insertion corporation spell Set doesn't. What is primal hither is to demo the interviewer that you lot tin determine which collection is to a greater extent than suited based on requirements.
This Collection interview query serves 2 purpose it non alone essay an of import programming concept Sorting just also utility class similar Collections which supply several methods for creating synchronized collection as well as sorting. Sorting is implemented using Comparable as well as Comparator inwards Java as well as when you lot telephone weep upwards Collections.sort() it gets sorted based on the natural corporation specified inwards compareTo() method spell Collections.sort(Comparator) volition form objects based on compare() method of Comparator.
13. What is the divergence betwixt Vector as well as ArrayList? (answer)
One to a greater extent than beginner flat collection interview questions, this is nonetheless real pop as well as by as well as large asked inwards the telephonic round. ArrayList inwards Java is 1 of the most used Collection degree as well as the most interviewers asked questions on ArrayList. See Difference betwixt Vector as well as ArrayList for the reply to this interview question.
14. What is the divergence betwixt HashMap as well as HashSet? (answer)
This collection interview questions is asked inwards conjunction amongst HashMap vs Hashtable. HashSet implements java.util.Set interface as well as that's why alone contains unique elements, spell HashMap allows duplicate values. In fact, HashSet is genuinely implemented on hit of java.util.HashMap. If you lot await internal implementation of java.util.HashSet, you lot volition discovery that it adds chemical ingredient equally primal on internal map amongst same values. For a to a greater extent than detailed answer, regard HashMap vs HashSet.
15) What is NavigableMap inwards Java? What is a do goodness over Map? (answer)
NavigableMap Map was added inwards 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 primal as well as higherKey() to render keys which is greater specified primal from a Map. It likewise supply similar methods to larn entries e.g. lowerEntry(), floorEntry(), ceilingEntry() as well as higherEntry(). Apart from navigation methods, it likewise provides utilities to do sub-Map e.g. creating a Map from entries of an exsiting Map similar tailMap, headMap as well as subMap. headMap() method returns a NavigableMap whose keys are less than specified, tailMap() returns a NavigableMap whose keys are greater than the specified as well as subMap() gives a NavigableMap betwixt a range, specified yesteryear toKey to fromKey.
16) Which 1 you lot volition prefer betwixt Array as well as ArrayList for Storing object as well as why? (answer)
Though ArrayList is likewise backed upwards yesteryear array, it offers unopen to usability wages over array inwards Java. Array is fixed length information structure, 1 time created you lot tin non alter it's length. On the other hand, ArrayList is dynamic, it automatically allocate a novel array as well as copies content of erstwhile array, when it resize. Another argue of using ArrayList over Array is back upwards of Generics. Array doesn't back upwards Generics, as well as if you lot shop an Integer object on a String array, you lot volition alone going to know close it at runtime, when it throws ArrayStoreException. On the other hand, if you lot work ArrayList, compiler as well as IDE volition grab those fault on the spot. So if you lot know size inwards advance as well as you lot don't demand re-sizing than work array, otherwise work 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 inwards Java? What is a do goodness over Map? (answer)
NavigableMap Map was added inwards 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 primal as well as higherKey() to render keys which is greater specified primal from a Map. It likewise supply similar methods to larn entries e.g. lowerEntry(), floorEntry(), ceilingEntry() as well as higherEntry(). Apart from navigation methods, it likewise provides utilities to do sub-Map e.g. creating a Map from entries of an exsiting Map similar tailMap, headMap as well as subMap. headMap() method returns a NavigableMap whose keys are less than specified, tailMap() returns a NavigableMap whose keys are greater than the specified as well as subMap() gives a NavigableMap betwixt a range, specified yesteryear toKey to fromKey.
16) Which 1 you lot volition prefer betwixt Array as well as ArrayList for Storing object as well as why? (answer)
Though ArrayList is likewise backed upwards yesteryear array, it offers unopen to usability wages over array inwards Java. Array is fixed length information structure, 1 time created you lot tin non alter it's length. On the other hand, ArrayList is dynamic, it automatically allocate a novel array as well as copies content of erstwhile array, when it resize. Another argue of using ArrayList over Array is back upwards of Generics. Array doesn't back upwards Generics, as well as if you lot shop an Integer object on a String array, you lot volition alone going to know close it at runtime, when it throws ArrayStoreException. On the other hand, if you lot work ArrayList, compiler as well as IDE volition grab those fault on the spot. So if you lot know size inwards advance as well as you lot don't demand re-sizing than work array, otherwise work ArrayList.
17) Can nosotros supervene upon Hashtable amongst ConcurrentHashMap? (answer)
Answer 3: Yes nosotros tin supervene upon Hashtable amongst ConcurrentHashMap as well as that's what suggested inwards Java documentation of ConcurrentHashMap. just you lot demand to live on careful amongst 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 inwards Hashtable just non inwards concurrentHashMap. instead of this work putIfAbsent() method of ConcurrentHashMap
18) What is CopyOnWriteArrayList, how it is dissimilar than ArrayList as well as Vector? (answer)
Answer: CopyOnWriteArrayList is novel List implementation introduced inwards Java 1.5 which provides amend concurrent access than Synchronized List. amend concurrency is achieved yesteryear Copying ArrayList over each write as well as supervene upon amongst master copy instead of locking. Also CopyOnWriteArrayList doesn't throw whatsoever ConcurrentModification Exception. Its dissimilar than ArrayList because its thread-safe as well as ArrayList is non thread-safe as well as it's dissimilar than Vector inwards price of Concurrency. CopyOnWriteArrayList provides amend Concurrency yesteryear reducing arguing amid readers as well as writers. Here is a overnice tabular array which compares performance of 3 of pop List implementation ArrayList, LinkedList as well as CopyOnWriteArrayList inwards Java:
19) Why ListIterator has added() method just Iterator doesn't or Why to add() method is declared inwards ListIterator as well as non on Iterator. (answer)
Answer: ListIterator has added() method because of its mightiness to traverse or iterate inwards both direction of the collection. it maintains 2 pointers inwards price of previous as well as adjacent telephone weep upwards as well as inwards a seat to add together a novel chemical ingredient without affecting electrical flow iteration.
20) When does ConcurrentModificationException occur on iteration? (answer)
When you lot 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 discovery whatsoever structural alter inwards Collection e.g. adding or removing of the element, 1 time Iterator begins, it tin throw ConcurrentModificationException. Here are unopen to tips to avoid ConcurrentModification inwards Java.
21) Difference betwixt Set, List as well as Map Collection classes? (answer)
java.util.Set, java.util.List as well as java.util.Map defines 3 of most pop information construction back upwards inwards Java. Set provides uniqueness guarantee i.e.g you lot tin non shop duplicate elements on it, just it's non ordered. On the other paw List is an ordered Collection as well as likewise allowes duplicates. Map is based on hashing as well as stores primal as well as value inwards an Object called entry. It provides O(1) performance to larn object, if you lot know keys, if at that topographic point is no collision. Popular impelmentation of Set is HashSet, of List is ArrayList as well as LinkedList, as well as of Map are HashMap, Hashtable as well as ConcurrentHashMap. Another primal divergence betwixt Set, List as well as Map are that Map doesn't implement Collection interface, spell other 2 does. For a to a greater extent than detailed answer, regard Set vs List vs Map inwards 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 1 of the concurrent Collection degree added on Java 1.5, chief divergence between BlockingQueue and other collection classes is that apart from storage, it likewise provides menstruation control. It tin live on used inwards inter-thread communication as well as likewise provides built-in thread-safety yesteryear using happens-before guarantee. You tin work BlockingQueue to solve Producer Consumer problem, which is what is needed inwards 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
0 Response to "Top 25 Coffee Collection Framework Interview Questions Answers For Freshers As Well As Experienced Programmers"
Post a Comment