Core Java - Interview Questions and Answers for 'Collection' | 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

   



Interview Questions and Answers - Order By Rating

   next 30
 Q31. 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


 Q32. What does the following initialization mean ?

ArrayList<LinkedList> traversalPaths = new ArrayList<LinkedList>();

What could be the use of such a collection.
Core Java
Ans. Initialize an ArrayList that will hold LinkedLists i.e every element of the arraylist will be a linked list.

Such collection could be used in algorithms that require first random access and then sequential traversal. For example - Storing traversal paths for a graph wherein we can start from any vertex. Implementing dictionary with each arraylist element holding staring with character and then linked list holding duplicate words.

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

   Like         Discuss         Correct / Improve     collections  linkedlist  arraylist


 Q33. In Java, would you like to see an explicit destructor?Core Java
Ans. Yes , a gc method to explicitly call garbage collection immediately on the object. It will provide better flexibility over application that require optimal usage of memory.

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

   Like         Discuss         Correct / Improve     destructor  garbage collection     Asked in 1 Companies      expert


 Q34. What is the quickest way to find count of duplicate elements in an arraylist, without using iteration or loops ?Core Java
Ans. We can copy the elements to a Set and then find the difference of count between ArrayList and Set. As Set don't allow duplicates , they will be removed in the set.

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

   Like         Discuss         Correct / Improve     collections  arraylist  set


 Q35. What problem we could have with ArrayList which aren't possible with Vectors ?Core Java
Ans. ArrayLists aren't synchronized and hence doesn't allow synchronized access. As multiple threads can access an arraylist in parallel, it may result in an inconsistent state.

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

   Like         Discuss         Correct / Improve     vector  arraylist  list  collections      Basic


 Q36. What is meant by "Vector is synchronized and ArrayList isn't " ?Core Java
Ans. It means that only 1 thread can access have access to Vector at a time and no parallel access is allowed whereas Array List allows parallel access by multiple threads.

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

   Like         Discuss         Correct / Improve     vectors  arraylist  collections  list  synchronization  synchronized


 Q37. Why iterators of an array list are fail fast ?Core Java
Ans. Because it's access isn't synchronized and hence access / modification by multiple threads may lead to inconsistent state.

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

   Like         Discuss         Correct / Improve     fail fast  arraylist  collections


 Q38. How would you explain the statement "Java Programs are collection of objects" ?Core Java
Ans. Java Programs are collection of objects that communicates with each other to get a task accomplished. To add to those objects, there are common spaces ( static i.e common for objects belonging to a class ) that are used too.

We can visualize objects as departments of an organization in real world. Just like Task gets initiated in one department and then files are moved across different departments to get work done. In a similar fashion, a task is initiated in one object ( having main method ) and then information ( through POJOs / DTOs ) is moved across objects to accomplish a task.

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

   Like         Discuss         Correct / Improve     objects  Java Programs are collection of objects  real life example of object communication


 Q39. What happens when you assign a pre initialized reference some other object ?Core Java
Ans. Now the reference points to a new object in memory. If that was the only reference for the previous object , it will be marked for garbage collection.

Foe example -

Object obj = new Object();
obj = new Object();

object created in first line will be eligible for garbage collection after line 2 as it looses all it's handlers.

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

   Like         Discuss         Correct / Improve     references  objects  garbage collection     Asked in 1 Companies


 Q40. Java doesn't provide exclusive access to memory like C/C++ and other lower level languages ? What are the advantanges and disadvantages ?Core Java
Ans. Yes, doesn't provide exclusive access as we cannot allocate and deallocate memory exclusively as Java internally manages it. The advantage of this is that it relieves the coder for such tasks and helps protect from many bugs that may get introduced with imperfect coding. Moreover as java garbage collector collects all unclaimed memory or objects, it helps the application from memory leaks.

On the flip side , as coder doesn't have extensive excess to memory , it is upto java to decide on the state for programming construct and data storage and hence may introduce some security risks. For example - Java keeps string literals in string pool and there is no exclusive way to remove it and hence may stay and sensitive data in string pool may introduce security issues. Moreover when we overwrite a value or object for a variable / reference, it is upto java to purge those values and hence it may stay in memory for a while till java decide that it is no longer referenced and hence should be removed and hence makes it vulnerable for inappropriate access.

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

   Like         Discuss         Correct / Improve     disadvantages of garbage collection  advantages and disadvantages of java memory management  java for security applications  java with sensitive data  memory management


 Q41. When does an object cleaned or destroyed by garbage collection ?Core Java
Ans. When it is no longer referenced or it looses all references which were earlier pointing to it.

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

   Like         Discuss         Correct / Improve     garbage collection  object destruction     Asked in 1 Companies


 Q42. How can we destroy an object ? Core Java
Ans. We cannot explicitly initiate destruction of an object as no destroy method is available in java. Only Garbage collection can destroy an object.

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

   Like         Discuss         Correct / Improve     Garbage collection  object destruction     Asked in 1 Companies


 Q43. What is the trade off between using Collections.synchronizedList() on an ArrayList and a CopyOnWriteArrayList?Core Java
Ans. Synchronized List locks the whole list to provide synchronization and thread safety during the read or write operation, while, CopyOnWriteArrayList doesn’t lock the whole list during these operations.
The CopyOnWriteArrayList class works according to its name i.e. copy-on-write which performs different actions for reading and write operations. For every write operation (add, set, remove, etc), it makes a new copy of the elements in the list. and for the read operations (get, iterator, listIterator, etc), it works on a different copy. So there is no additional overhead during a read operation and its read operation is faster than Collections.SynchronizedList(). Thus, COWAL is better for reading operation than Synchronized List.

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

   Like         Discuss         Correct / Improve     Collections.synchronizedList()  CopyOnWriteArrayList


 Q44. What is the difference between Enumeration and Iterator ?Core Java
Ans. Enumeration can iterate only legacy collections like Vector , HashTable and Stack whereas Iterator can iterate both legacy and non legacy collections.

Enumeration is less safer than Iterator

Enumeration is fail safe whereas Iterator is fail fast

Iterator allows for removal of element while traversal whereas Enumeration doesn't have remove method.

Enumerations were introduced in Java 1 whereas Iterators were introduced with Java 2

Enumerations have methods like hasMoreElements and nextElement whereas Iterators have methods like hasNext, next and remove

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

   Like         Discuss         Correct / Improve     enumeration vs iterator  collections      Basic        frequent


 Q45. How is garbage collection in Java 8 different from Java 7 ? Core Java
 This question is still unanswered. Can you please provide an answer.


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

   Like         Discuss         Correct / Improve     memory management  garbage collection


 Q46. Why do we need Garbage collection ?Core Java
Ans. Garbage collection mechanism frees up the memory that is no longer needed. In languages like C / C++ the deallocation needs to be done explicitly by the programmer and hence any leniency may result in memory leak. Garbage collection in java ensures that all unused memory is reclaimed and hence there are no memory leaks.Moreover it relieves the programmer from the hassle of carefully releasing all memory.

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

   Like         Discuss         Correct / Improve     Garbage Collection  memory management     Asked in 2 Companies


 Q47. Why don't Java objects have destructors ?Core Java
Ans. Java manages memory using built-in garbage collector

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

   Like         Discuss         Correct / Improve     destructor  garbage collection


 Q48. isnt the use of HashTable and ConcurrentHashMap the same, i.e providing synchronized map collection ?Core Java
Ans. Yes, they both aim at providing synchronized access to the Map collection. The only difference is in their implementation. ConcurrentHashMap came after HashTable and hence technically more efficient as it doesn't lock the complete map while accessing it.

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

   Like         Discuss         Correct / Improve     HashTable  ConcurrentHashMap  map  Collections


 Q49. Difference between concurrentHashMap and HashTable ?Core Java
Ans. HashTable locks the complete collection to provide synchronization whereas ConcurrentHashMap only locks segments to achieve synchronization and hence better efficient and faster.

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

   Like         Discuss         Correct / Improve     concurrentHashMap  HashTable  Map  Collections     Asked in 2 Companies


 Q50. What is the difference between circular queue and priority queue ?Data Structures
Ans. https://qr.ae/TWK2Ok

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

   Like         Discuss         Correct / Improve     circular queue  priority queue  queue  data structures  collections


 Q51. What is memory leak ? How Java helps countering memory leaks compared to C++ ?Core Java
Ans. Memory Leak is a situation wherein we have a reserved memory location but the program has lost its reference and hence has no way to access it.

Java doesn't have concept of Pointers, Moreover Java has concept of Garbage collection that frees up space in heap that has lost its references.

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

   Like         Discuss         Correct / Improve     memory leak  garbage collection      intermediate        frequent


 Q52. Do you feel that its useless to define hashCode method for a class ?Core Java
Ans. Yes its useless if we are not going to use its objects within Hash collection, For example - HashSet , HashMap. HashCode is used internally by these collections for Search.

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

   Like         Discuss         Correct / Improve     hashcode  hash collections  search


 Q53. What kind of thread is garbage collection thread ?Core Java
Ans. Daemon thread

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

   Like         Discuss         Correct / Improve     garbage collection      intermediate

Try 2 Question(s) Test


 Q54. Which type of memory is cleaned / recovered by garbage collection - stack or heap ?Core Java
Ans. Heap memory.

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

   Like         Discuss         Correct / Improve     memory management  stack  heap  garbage collection     Asked in 2 Companies


 Q55. What are Collection Classes ?Core Java
Ans. Collections in java is a framework of classes that provides an abstracted data structure to store and manipulate the group of objects. Each class is unique in the way it stores , search , sort and modify the elements.

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

   Like         Discuss         Correct / Improve     collections  collection classes     Asked in 1 Companies      Basic        frequent


Frequently asked in high end product companies. Frequently asked in Deloitte.
  Q56. 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


 Q57. 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


 Q58. Which is better in terms of performance - Iterator or Spliterator ?Core Java
Ans. Spliterator has better performance potential than iterators but only if the potential is used. Spliterator can iterate streams in parallel as well as in sequence whereas iterator can only iterate in sequence.

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

   Like         Discuss         Correct / Improve     iterator  collections  streams  parallel streams  Spliterator


 Q59. What is the difference between Collections.emptyList() and creating new instance of List using new ?Core Java
Ans. But Collections.emptyList() returns an Immutable list whereas new arraylist() creates a mutable list.

Advantage of getting an empty list using Collections.emptyList is that it returns a singleton list which can be shared among many references and hence made immutable. This is good fit for situations where we would like to initialize a list to basic minimum empty to avoid null pointer exception.

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

   Like         Discuss         Correct / Improve     collections  Collections.emptyList  immutable  immutability  immutability


 Q60. Which data structure would you recommend for ordered and sorted data?Core Java
Ans. TreeSet and TreeMap are used for maintaining sorted elements.

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

   Like         Discuss         Correct / Improve     ordered collections  sorted collections     Asked in 1 Companies      basic        frequent


previous 30   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: