Core Java - Interview Questions and Answers for 'Collections' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Collections' - 168 question(s) found - Order By Newest

next 30
 Q1. Does garbage collection guarantee that a program will not run out of memory?Core Java
Ans. Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   garbage collection   java memory management   advanced     Asked in 2 Companies

Try 1 Question(s) Test


Asked multiple times in Capgemini.
 Q2. Why Char array is preferred over String for storing password?Core Java
Ans. String is immutable in java and stored in String pool. Once it's created it stays in the pool until unless garbage collected, so even though we are done with password it's available in memory for longer duration and there is no way to avoid it. It's a security risk because anyone having access to memory dump can find the password as clear text.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   string class   string   immutable  immutability   string pool   garbage collection   advanced     Asked in 6 Companies      Expert


Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q3. What is the difference between ArrayList and LinkedList ?Core Java
Ans. Underlying data structure for ArrayList is Array whereas LinkedList is the linked list and hence have following differences -

1. ArrayList needs continuous memory locations and hence need to be moved to a bigger space if new elements are to be added to a filled array which is not required for LinkedList.

2. Removal and Insertion at specific place in ArrayList requires moving all elements and hence leads to O(n) insertions and removal whereas its constant O(1) for LinkedList.

3. Random access using index in ArrayList is faster than LinkedList which requires traversing the complete list through references.

4. Though Linear Search takes Similar Time for both, Binary Search using LinkedList requires creating new Model called Binary Search Tree which is slower but offers constant time insertion and deletion.

5. For a set of integers you want to sort using quicksort, it's probably faster to use an array; for a set of large structures you want to sort using selection sort, a linked list will be faster.

  Sample Code for ArrayList

  Sample Code for LinkedList

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     collections   java   data structures   arraylist   linkedlist   arraylist vs linkedlist     Asked in 61 Companies      Basic        frequent

Try 1 Question(s) Test


Frequently asked.
 Q4. If you are given a choice to use either ArrayList and LinkedList, Which one would you use and Why ?Core Java
Ans. ArrayList are implemented in memory as arrays and hence allows fast retrieval through indices but are costly if new elements are to be inserted in between other elements. LinkedList allows for constant-time insertions or removals using iterators, but only sequential access of elements

1. Retrieval - If Elements are to be retrieved sequentially only, Linked List is preferred.

2. Insertion - If new Elements are to be inserted in between other elements , Linked List is preferred.

3. Search - Binary Search and other optimized way of searching is not possible on Linked List.

4. Sorting - Initial sorting could be pain but lateral addition of elements in a sorted list is good with linked list.

5. Adding Elements - If sufficiently large elements needs to be added very frequently ,Linked List is preferable as elements don't need consecutive memory location.

  Sample Code for ArrayList

  Sample Code for LinkedList

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   list   arraylist   linkedlist   difference between   architecture   data structure   ebay     Asked in 2 Companies      basic        frequent

Try 2 Question(s) Test


Frequently asked in all types of companies especially Indian Services companies. Frequently asked in CTS (Based on 2 feedback)
  Q5. What is the use of hashcode in Java ?Core Java
Ans. Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. The value received from hashcode() is used as bucket number for storing elements. This bucket number is the address of the element inside the set/map. when you do contains() then it will take the hashcode of the element, then look for the bucket where hashcode points to and if more than 1 element is found in the same bucket (multiple objects can have the same hashcode) then it uses the equals() method to evaluate if object are equal, and then decide if contain() is true or false, or decide if element could be added in the set or not.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   hashcode   advanced  hashtable     Asked in 33 Companies      intermediate        frequent

Try 1 Question(s) Test


Very frequently asked across all types of companies.
  Q6. Difference between HashMap and Hashtable?Core Java
Ans. Hashtable is synchronized whereas HashMap is not.HashMap allows null values whereas Hashtable doesnt allow null values.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   hashmap   map   hashtable   hashmap vs hashtable     Asked in 14 Companies      basic        frequent

Try 2 Question(s) Test


Advanced level question usually asked in High end product companies. Have been asked in Google and Amazon (Based on 1 Feedback)
  Q7. Describe, in general, how java's garbage collector works ?Core Java
Ans. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is known as garbage collection. The Java runtime environment supports a garbage collector that periodically frees the memory used by objects that are no longer needed. The Java garbage collector is a mark-sweep garbage collector that scans Java dynamic memory areas for objects, marking those that are referenced. After all possible paths to objects are investigated, those objects that are not marked (i.e. are not referenced) are known to be garbage and are collected.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   garbage collection   java memory management   advanced     Asked in 21 Companies      intermediate        frequent

Try 4 Question(s) Test


Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q8. Difference between TreeMap and HashMap ?Core Java
Ans. They are different the way their elements are stored in memory. TreeMap stores the Keys in order whereas HashMap stores the key value pairs randomly.

  Sample Code for treemap

  Sample Code for hashmap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   map   treemap   hashmap   treemap vs hashmap     Asked in 31 Companies      basic        frequent

Try 1 Question(s) Test


Advanced level question frequently asked in US based companies. Recently asked in EMC and Intuit.
  Q9. Can you provide some implementation of a Dictionary having large number of words ? Solution
Ans. Simplest implementation we can have is a List wherein we can place ordered words and hence can perform Binary Search.

Other implementation with better search performance is to use HashMap with key as first character of the word and value as a LinkedList.

Further level up, we can have linked Hashmaps like ,

hashmap {
a ( key ) -> hashmap (key-aa , value (hashmap(key-aaa,value)
b ( key ) -> hashmap (key-ba , value (hashmap(key-baa,value)
....................................................................................
z( key ) -> hashmap (key-za , value (hashmap(key-zaa,value)
}

upto n levels ( where n is the average size of the word in dictionary.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   hashmap   binary search   search algorithm   advanced   architecture   data structure     Asked in 6 Companies        frequent

Try 1 Question(s) Test


 Q10. Will this code give error if i try to add two heterogeneous elements in the arraylist. ? and Why ?

List list1 = new ArrayList<>();
list1.add(5);
list1.add("5");
Ans. If we don't declare the list to be of specific type, it treats it as list of objects.

int 1 is auto boxed to Integer and "1" is String and hence both are objects.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   arraylist   list   autoboxing   wrapper classes      expert        rare


 Q11. Let's suppose we have an to destroy objects, Design a program to implement garbage collection ?Design
Ans. Step 1 - We can create a Registry Class having Map of all created objects as key and References list as value.

Step 2 - Whenever we create an object , we should update the Registry in the constructor to add the new object.

Step 3 - Whenever we assign a new reference to the object , we need to update the entry in Map. Similarly if the reference get's removed ( end of scope etc ), we need to remove the entry of reference from the list.

Step 4 - We can have threaded code to monitor the Map to see if any object looses all it's references and should call the method to destroy object and clean the memory.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     garbage collection


 Q12. What is a ConcurrentHashMap ?Core Java
Ans. ConcurrentHashMap is a hashMap that allows concurrent modifications from multiple threads as there can be multiple locks on the same hashmap.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   hashmap   map   concurrenthashmap   concurrenthashmap  concurrency   general electric   ge     Asked in 32 Companies        rare


 Q13. Collections.sort can only be performed on ..

a. Set
b. List
c. Map
d. Any Collection implementation
Core Java
Ans. List

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     collections   java   sorting

Try 2 Question(s) Test


 Q14. Why HashTable has been deprecated ?Core Java
Ans. HashTable has been deprecated. As an alternative, ConcurrentHashMap has been provided. It uses multiple buckets to store data and hence much better performance than HashTable. Moreover, there is already a raw type HashMap. The only difference between the HashTable and HashMap is that Hashtable is synchronized whereas HashMap is not. Most of the synchronized collections have been deprecated and their raw alternative have been presented as preferred.Synchronization has a cost. Using synchronized collection in places where there is no need of it leads to useless utilization of resources. As these collections are rarely used in a static context or shared among threads, Java might have thought it better to just provide the raw collection and let developers implement synchronization if he feels the need to do so. HashMap is now presented as the default and the preferred way of using Map with read optimized hashing, and ConcurrentHashMap has been provided for synchronized access which provides better performance than HashTable. Because of this, Java thought it right to deprecate the use of HashTable.'

Synchronization has a cost. Using synchronized collection at a place where there is hardly any need of it would means useless utilization of resources. As these collections are rarely used in static context or shared among threads, Java might have thought it better to just provide the raw collection and let developer implement synchronization if he feels the need to do so.

As HashMap has been presented as default and preferred way of using Map with read optimized hashing, and ConcurrentHashMap has been provided for synchronized access which provides better performance than HashTable, Java thought it right to deprecate the use of HashTable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hashtable  synchronized collections  Why synchronized collections have been deprecated  Why HashTable has been deprecated  HashTable vs HashMap      expert

Try 3 Question(s) Test


 Q15. Difference beween HashSet and TreeSet ?Core Java
Ans. 1. HashSet doesnt maintain its elements in any specific order and is all random whereas TreeSet maintains elements in natural order 9 order defined by the equals method of TreeSet element type )

2. TreeSet doesnt allow null elements whereas HashMap does.

3. As TreeSet orders elements and is hence insertion is comparatively slower.

4. HashSet performs basic operations like add(), remove(), contains(), size() etc. in a constant size time. A TreeSet performs these operations at the order of log(n) time.

5. HashMap in Java internally backs a HashSet. A NavigableMap backs a TreeSet internally.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     collections  hashset  treeset  set   hashet vs treeset     Asked in 4 Companies      Basic        frequent

Try 1 Question(s) Test


Frequently asked in high end product companies. Frequently asked in Deloitte.
  Q16. How is Hashmap internally implemented in Java ?Core Java
Ans. https://medium.com/javarevisited/internal-working-of-hashmap-in-java-97aeac3c7beb#:~:text=HashMap%20internally%20uses%20HashTable%20implementation,the%20entries%20into%20the%20map.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hashmap  collections  hashmap internal implementation     Asked in 20 Companies      expert        frequent


Frequently asked in Indian Service Companies. Tech Mahindra ( Based on 3 inputs ) and Infosys ( 3 inputs )
  Q17. What is the difference between List, Set and Map ?

or

What are the different Java Collections Interfaces ?
Core Java
Ans. List - Members are stored in sequence in memory and can be accessed through index.
Set - There is no relevance of sequence and index. Sets doesn't contain duplicates whereas multiset can have duplicates.
Map - Contains Key , Value pairs.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   list   set   map   list vs set vs map     Asked in 8 Companies      basic        frequent

Try 1 Question(s) Test


 Q18. Can we use null as a key for a map collection ?Core Java
Ans. Yes , for HashMap. HashMap implements Map interface. HashMap allows one null key and any number of null values.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   hashmap   map   hashtable   yes-no     Asked in 1 Companies


  Q19. Difference between Vector and ArrayList ?Core Java
Ans. Vectors are synchronized whereas Array lists are not.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   basic interview question   vector   arraylist   collections   synchronization   vector vs arraylist     Asked in 35 Companies      basic        frequent


 Q20. Should we override finalize method ?
Ans. Finalize is used by Java for Garbage collection. It should not be done as we should leave the Garbage Collection to Java itself.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   garbage collection   java memory management   finalize

Try 2 Question(s) Test


 Q21. What are strong, soft, weak and phantom references in Java ?Core Java
Ans. Garbage Collector wont remove a strong reference.

A soft reference will only get removed if memory is low.

A weak reference will get removed on the next garbage collection cycle.

A phantom reference will be finalized but the memory will not be reclaimed. Can be useful when you want to be notified that an object is about to be collected.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   memory management   jvm   garbage collections   references   strong reference   soft reference   weak reference   phantom reference   architecture     Asked in 2 Companies


 Q22. What are the Disadvantages of using Collection Classes over Arrays ?Core Java
Ans. Collections can only hold objects, It can't hold primitive data types.

Collections have performance overheads as they deal with objects and offer dynamic memory expansion. This dynamic expansion could be a bigger overhead if the collection class needs consecutive memory location like Vectors.

Collections doesn't allow modification while traversal as it may lead to concurrentModificationException.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   collections classes   disadvantages of collections over arrays   collections vs arrays


 Q23. Can we call the garbage collector explicitly ?Core Java
Ans. Yes, We can call garbage collector directly but it doesn't guarantees that the gc will start executing immediately. This gc( ) method appears in both Runtime and System classes of java.lang package.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   garbage collection   java memory management   jvm   gc   yes no     Asked in 4 Companies      expert

Try 1 Question(s) Test


 Q24. What is comparator interface used for ?Core Java
Ans. The purpose of comparator interface is to compare objects of the same class to identify the sorting order. Sorted Collection Classes ( TreeSet, TreeMap ) have been designed such to look for this method to identify the sorting order, that is why class need to implement Comparator interface to qualify its objects to be part of Sorted Collections.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap   treeset   comparator     Asked in 2 Companies      Intermediate

Try 2 Question(s) Test


 Q25. If I try to add Enum constants to a TreeSet, What sorting order will it use ?
Ans. Tree Set will sort the Values in the order in which Enum constants are declared.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   set   treeset   enum   sorting   technical lead


 Q26. What is ConcurrentModificationException ?Core Java
Ans. This is the exception that is thrown when we try to modify the non concurrent collection class while iterating through it.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   concurrentmodificationexception   exception  concurrency     Asked in 14 Companies      intermediate


 Q27. What is a BlockingQueue?Data Structures
Ans. BlockingQueue is a Queue that supports operations that wait for the queue to become non-empty when retrieving and removing an element, and wait for space to become available in the queue when adding an element.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     blockingqueue   collections   java   concurrent collections     Asked in 10 Companies


 Q28. Which of the following collections stores its elements in insertion Order ?

a. HashMap
b. TreeMap
c. LinkedHashMap
d. LinkedMap
Core Java
Ans. LinkedHashMap

  Sample Code for LinkedHashMap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     linkedhashmap   collections   java   map


 Q29. In what order the elements of a HashSet are retrieved ?

a. Random Order
b. Insertion Order
c. Natural Sorting Order
d. Inverse Natural Sorting Order
Core Java
Ans. Random Order

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     collections   set   hashset   java

Try 1 Question(s) Test


 Q30. Which of the following doesn't extend Collection interface ?

a. Set
b. List
c. Map
d. Queue
Core Java
Ans. Map

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   collection interface


next 30

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: