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. |
|
| ||||
Interview Questions and Answers - Order By Newest | ||||
Frequently asked in high end product companies. Frequently asked in Deloitte. | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 Indian Service Companies. Tech Mahindra ( Based on 3 inputs ) and Infosys ( 3 inputs ) | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
Ans. Map is an interface where HashMap is the concrete class. | ||||
Sample Code for map 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   hashmap   map basic   frequent | ||||
| ||||
Ans. The properties class is a subclass of Hashtable that can be read from or written to a stream. | ||||
Sample Code for Load Properties using Property Class | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   collections   hashtable   map   synchronization   synchronized | ||||
| ||||
Ans. Collections class is a utility class having static methods for doing operations on objects of classes which implement the Collection interface. For example, Collections has methods for finding the max element in a Collection. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   collections   collections class   collection interface   basic interview question Asked in 6 Companies basic   frequent | ||||
Try 1 Question(s) Test | ||||
Frequently asked in CTS ( Based on 2 feedback ) | ||||
| ||||
Ans. Comparable interface is used for single sequence sorting i.e.sorting the objects based on single data member where as comparator interface is used to sort the object based on multiple data members. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   java.util   comparable   comparator   collections Asked in 23 Companies basic   frequent | ||||
| ||||
Ans. Collections are re-sizable in nature. We can increase or decrease the size as per recruitment. Collections can hold both homogeneous and heterogeneous data's. Every collection follows some standard data structures. Collection provides many useful built in methods for traversing,sorting and search. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   collections   collections classes   advantages of collections over arrays   collections vs arrays   basic interview question Asked in 6 Companies basic   frequent | ||||
| ||||
Ans. intern() method keeps the string in an internal cache that is usually not garbage collected. Moreover provide reference for scp object for corresponding string object present in heap memory. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string   intern method   garbage collection   advanced Asked in 2 Companies expert | ||||
| ||||
Ans. LinkedList and ArrayList are two different implementations of the List interface. LinkedList implements it with a doubly-linked list. ArrayList implements it with a dynamically resizing array. | ||||
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 basic   frequent | ||||
| ||||
Ans. 1. Collection should have an index for random access. 2. Collection should have ordered elements. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   collections   search algorithm   search   binary search   at&t intermediate | ||||
| ||||
Ans. No, We cannot have duplicate keys in HashMap. If we attempt to do so , the previous value for the key is overwritten. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   collections   hashmap   map   hashtable | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. TreeSet and TreeMap | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   collections   treemap   treeset   basic interview question basic   frequent | ||||