Top 25 Coffee Collection Framework Interview Questions Answers For Freshers As Well As Experienced Programmers

Interview questions from Collection packet or framework are most mutual inward whatsoever Core Java Interview yet a tricky one. Together Collection as well as multithreading brand whatsoever Java interview tough to fissure as well as having a proficient agreement of Collection as well as threads volition aid yous to excel inward Java interview. I idea almost 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 get-go wrote 10 multi-threading Interview questions  but somehow this article got delayed. Though I conduct hold shared several questions individually inward between.


I call upwards the principal 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 lastly five years.  In this article, nosotros volition watch a mix of simply about beginners as well as advanced Java Collection interviews as well as their answers which conduct hold been asked inward 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 institute past times Google. BTW, if yous are preparing for Java interview hence yous tin likewise accept aid from Java Programming Interview Exposed, an first-class resources to create good inward Java interviews.


 packet or framework are most mutual inward whatsoever Core Java Interview yet a tricky 1 Top 25 Java Collection Framework Interview Questions Answers for Freshers as well as Experienced Programmers

    


Good Java Collection Interview Questions Answers


 packet or framework are most mutual inward whatsoever Core Java Interview yet a tricky 1 Top 25 Java Collection Framework Interview Questions Answers for Freshers as well as Experienced ProgrammersNow 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 in that location diverse implementation, to a greater extent than oft than non 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 score or to a greater extent than advanced level. Normally 2 to 3 years sense counted equally beginners piece over five years comes nether advanced category, nosotros volition watch questions from both categories.


1. How does HashMap piece of work inward Java? (answer)
This is Classical Java Collection interview questions which I conduct hold likewise discussed inward my before article how does HashMap industrial plant inward Java. This collection interview questions is to a greater extent than oft than non 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 inward Java. For explanation as well as answers of these questions Please watch before link.



2. What is the departure 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, in that location is a subtle departure betwixt them. If Queue is empty() hence a telephone scream upwards to remove() method volition throw Exception, piece a telephone scream upwards to poll() method returns null. By the way, precisely which chemical component 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 component equally per Comparator or Comparable at caput position. 




3. What is the departure betwixt fail-fast as well as fail-safe Iterators? (answer)
This is relatively new collection interview questions as well as tin larn fox if yous take heed the term fail-fast as well as fail-safe get-go time. Fail-fast Iterators throws ConcurrentModificationException when 1 Thread is iterating over collection object as well as other thread structurally modify Collection either past times adding, removing or modifying objects on underlying collection. They are called fail-fast because they endeavor to forthwith throw Exception when they meet failure. On the other mitt fail-safe Iterators industrial plant on re-create of collection instead of master collection



4. How create yous take an entry from a Collection? as well as later what is the departure betwixt the remove() method of Collection and remove() method of Iterator, which 1 yous volition work piece removing elements during iteration?

Collection interface defines remove(Object obj) method to take objects from Collection. List interface adds simply about 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, piece non iterating. Things change, when yous iterate. Suppose yous are traversing a List as well as removing solely certainly elements based on logic, hence yous demand to work Iterator's remove() method. This method removes electrical current chemical component from Iterator's perspective. If yous work Collection's or List's remove() method during iteration hence 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 departure betwixt Synchronized Collection as well as 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 departure is that Concurrent Collections has amend performance than synchronized Collection because they lock solely a portion of Map to accomplish concurrency as well as Synchronization. See the departure betwixt Synchronized Collection as well as Concurrent Collection inward Java for to a greater extent than details.



6. What is the departure betwixt Iterator as well as Enumeration? (answer)
This is a beginner score collection interview questions as well as to a greater extent than oft than non asked during interviews of Junior Java developer upwards to sense of 2 to 3 years Iterator duplicate functionality of Enumeration with 1 add-on of remove() method as well as both supply navigation functionally on objects of Collection.Another departure is that Iterator is to a greater extent than condom than Enumeration and doesn't allow simply about other thread to modify collection object during iteration except remove() method as well as 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 work Hashing? (answer)
This is a tricky inquiry inward Java because for hashing yous demand both cardinal as well as value and there is no cardinal for the shop it inward a bucket, hence how exactly HashSet store element internally. Well, HashSet is built on top of HashMap. If yous expect at source code of java.util.HashSet class, yous volition discover 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 inward the backing Map
private static concluding Object PRESENT = novel Object();

When yous telephone scream upwards 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 create yous demand to create to work a custom object equally a cardinal inward Collection classes similar Map or Set? (answer)
The reply is: If yous are using whatsoever custom object inward Map equally key, yous demand to override equals() as well as hashCode() method, as well as brand certainly they follow their contract. On the other mitt if yous are storing a custom object inward Sorted Collection e.g. SortedSet or SortedMap, yous likewise demand to brand certainly 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 departure betwixt HashMap as well as Hashtable? (answer)
This is simply about other Classical Java Collection interview asked on beginner’s score as well as 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 transcend away incorrect is that if he placed another follow-up question similar how hashMap industrial plant inward Java or tin yous supersede Hashtable with ConcurrentHashMap etc. See Hashtable vs HashMap inward Java for detailed reply of this interview question.



10. When create yous work ConcurrentHashMap inward Java? (answer)
This is simply about other advanced score collection interview questions inward Java which usually asked to cheque whether the interviewer is familiar amongst optimization done on ConcurrentHashMap or not. ConcurrentHashMap is amend suited for province of affairs where yous conduct hold multiple readers as well as one
Writer or fewer writers since Map gets locked solely during the write operation. If yous conduct hold an equal discover of reader as well as author than ConcurrentHashMap volition perform inward the draw of piece of work of Hashtable or synchronized HashMap.




11. What is the departure betwixt Set as well as List inward Java? (answer)
Another classical Java Collection interviews pop on telephonic circular or the get-go circular of interview. Most of Java programmer knows that Set doesn't allowed duplicate piece List does as well as List maintains insertion lodge piece Set doesn't. What is cardinal hither is to exhibit the interviewer that yous tin create upwards one's heed which collection is to a greater extent than suited based on requirements.



12. How create yous Sort objects on the collection? (solution)
This Collection interview inquiry serves 2 purpose it non solely essay an of import programming concept Sorting but 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 inward Java as well as when yous telephone scream upwards Collections.sort() it gets sorted based on the natural lodge specified inward compareTo() method piece Collections.sort(Comparator) volition variety objects based on compare() method of Comparator. 



13. What is the departure betwixt Vector as well as ArrayList? (answer)
One to a greater extent than beginner score collection interview questions, this is even hence real pop as well as to a greater extent than oft than non asked inward the telephonic round. ArrayList inward Java is 1 of the most used Collection shape 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 departure betwixt HashMap as well as HashSet? (answer)
This collection interview questions is asked inward conjunction amongst HashMap vs Hashtable. HashSet implements java.util.Set interface as well as that's why solely contains unique elements, piece HashMap allows duplicate values.  In fact, HashSet is truly implemented on top of java.util.HashMap. If yous expect internal implementation of java.util.HashSet, yous volition discover that it adds chemical component equally cardinal on internal map amongst same values. For a to a greater extent than detailed answer, watch HashMap vs HashSet.



15) What is NavigableMap inward Java? What is a create 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 cardinal as well as higherKey() to render keys which is greater specified cardinal 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 create 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 past times toKey to fromKey.  



16) Which 1 yous volition prefer betwixt Array as well as ArrayList for Storing object as well as why? (answer)
Though ArrayList is likewise backed upwards past times array, it offers simply about usability wages over array inward Java. Array is fixed length information structure, in 1 lawsuit created yous 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 sometime 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 yous shop an Integer object on a String array, yous volition solely going to know almost it at runtime, when it throws ArrayStoreException. On the other hand, if yous work ArrayList, compiler as well as IDE volition select grip of those mistake on the spot. So if yous know size inward advance as well as yous don't demand re-sizing than work array, otherwise work ArrayList.


17) Can nosotros supersede Hashtable amongst ConcurrentHashMap? (answer)
Answer 3: Yes nosotros tin supersede Hashtable amongst ConcurrentHashMap as well as that's what suggested inward Java documentation of ConcurrentHashMap. but yous 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) industrial plant inward Hashtable but non inward 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 inward Java 1.5 which provides amend concurrent access than Synchronized List. amend concurrency is achieved past times Copying ArrayList over each write as well as supersede amongst master 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 inward damage of Concurrency. CopyOnWriteArrayList provides amend Concurrency past times reducing controversy amid readers as well as writers. Here is a squeamish tabular array which compares performance of 3 of pop List implementation ArrayList, LinkedList as well as CopyOnWriteArrayList inward Java:
 packet or framework are most mutual inward whatsoever Core Java Interview yet a tricky 1 Top 25 Java Collection Framework Interview Questions Answers for Freshers as well as Experienced Programmers



19) Why ListIterator has added() method but Iterator doesn't or Why to add() method is declared inward ListIterator as well as non on Iterator. (answer)
Answer: ListIterator has added() method because of its might to traverse or iterate inward both administration of the collection. it maintains 2 pointers inward damage of previous as well as side past times side telephone scream upwards as well as inward a seat to add together a novel chemical component without affecting electrical current iteration.


20) When does ConcurrentModificationException occur on iteration? (answer)
When yous 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 abide by whatsoever structural alter inward Collection e.g. adding or removing of the element, in 1 lawsuit Iterator begins, it tin throw ConcurrentModificationException.  Here are simply about tips to avoid ConcurrentModification inward Java. 
 packet or framework are most mutual inward whatsoever Core Java Interview yet a tricky 1 Top 25 Java Collection Framework Interview Questions Answers for Freshers as well as Experienced Programmers



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 inward Java. Set provides uniqueness guarantee i.e.g yous tin non shop duplicate elements on it, but it's non ordered. On the other mitt List is an ordered Collection as well as likewise allowes duplicates. Map is based on hashing as well as stores cardinal as well as value inward an Object called entry. It provides O(1) performance to larn object, if yous know keys, if in that location 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 cardinal departure betwixt Set, List as well as Map are that Map doesn't implement Collection interface, piece other 2 does. For a to a greater extent than detailed answer, watch Set vs List vs Map inward Java


 packet or framework are most mutual inward whatsoever Core Java Interview yet a tricky 1 Top 25 Java Collection Framework Interview Questions Answers for Freshers as well as Experienced Programmers



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 shape added on Java 1.5, principal departure between BlockingQueue and other collection classes is that apart from storage, it likewise provides menses control. It tin live used inward inter-thread communication as well as likewise provides built-in thread-safety past times using happens-before guarantee. You tin work 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

Belum ada Komentar untuk "Top 25 Coffee Collection Framework Interview Questions Answers For Freshers As Well As Experienced Programmers"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel