Wednesday, 2 May 2012

JAVA interview questions 37


Q361. Explain about LinkedHashMap? 
It is child class of HashMap. It is exactly same as HashMap except the following difference. In the case of HashMap the insertion order is not preserved but in the case of LinkedHashMap insertion order is preserved. Introduced in 1.4 version 

Q362. What is IdentityHashMap? 
It is exactly same as HashMap except the following difference. 
In the HashMap JVM uses equals() method to identify duplicate keys but in the case of IdentityHashMap JVM uses == operator for this. 
 
Q363. What is WeakHashMap? 
It is exactly same as HashMap except the following difference. 
In case of HashMap an Object is not eligible for garbage collection if it is associated with 
HashMap even though it dosent have any external references. ie HashMap dominates garbage collector. 
But in case of WeakHashMap , if an Object is not having any external references then it is always eligible for garabage collectoion even though it is associated with weakHashMap. ie garbage collector dominates WeakHashMap 

Q364. What is TreeMap? 
TreeMap can be used to store a group of objects as key-value pairs where all the entries are arranged according to some sorting order of keys. 
The underlying data structure is RED-BLACK Tree 
Duplicates keys are not allowed but values can be duplicated. 
Insertion order is not preserved because insertion is based on some sorting order 
If we are depending on Natural sorting order then keys should be homogeneous(violation leads to ClassCastException) but values need not homogeneous 
In case of customized sorting order we can insert heterogeneous keys and values 
For empty TreeMap as first entry with null values are allowed but after inserting that entry if we are trying to insert any other entry we will get NullPointerException For non empty TreeMap if we are trying to insert null keys we will get 
NullPointerException 
There are no restrictions for null values. 

Q365. What is Hashtable? 
Hashtable is a legacy Map and can be used to store objects as key value pairs. 
The underlying data sturucture is Hashtabe 
Duplicates keys are not allowed but duplicate values are allowed null insertion is not possible for both keys and values 
all methods are synchronized 
insertion order is not preserved because it is based on hashcode of keys heterogeneous Objects are allowed for both keys and values 
introduced in 1.0 version it is legacy class 

Q367. What is PriorityQueue? 
It represents a data structure to hold group of individual objects prior to processing based on 
some priority .it can be natural sorting order and it can be customized sorting order described by Comparator. 
It is the implementation class of Queue interface. 
Insertion order is not preserved because here insertion is done based on some sorting order 
Duplicates are not allowed 
null insertion is not possible even as first element also 
If we are depending on natural sorting order Objects should be homogeneous violation leads to ClassCastException 
If we are depending on customized sorting order Objects can be heterogeneous also. 

Q368. What is Arrays class? 
It is utility class for arrays. 
It defines several utility methods for arrays like sorting an array or searching an element in array 
present in java.util package 

Q369. We are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList? 
ArrayList 

Q370. Why ArrayList is faster than Vector? 
All methods present in the Vector are synchronized and hence any method can be executed by only one thread at a time. It slows down the execution. 
But in ArrayList, no method is synchronized and hence multiple thread are allowed execute simultaneously which speed up the execution.

No comments:

Post a Comment