Friday, January 29, 2016

Popular Java interview questions 2

Popular Java interview questions 2


  1. Difference between Array and ArrayList in Java 
ArrayArrayList
ResizableNoYes
PrimitivesYesNo
Iterating valuesfor, for eachIterator , for each
Lengthlength variablesize method
PerformanceFastSlow in comparision
MultidimensionalYesNo
Add ElementsAssignment operatoradd method
2.  What is BlockingQueue in Java Collections Framework? 
BlockingQueue implements the java.util.Queue interface . BlockingQueue supports  operations that wait for the queue to become non-empty when retrieving an element , and wait  for space to become available in the queue when storing an element .
It does not accept null elements.
Blocking queues are primarily designed for the producer-consumer problems.
BlockingQueue implementations are thread-safe and can also be used in inter-thread communications.
This concurrent Collection class was added in jdk 1.5
3. Explain the importance of hashCode() and equals() method ? Explain the contract also ?
Contract of hashCode() and equals() method

a.  If  object1.equals(object2) , then  object1.hashCode() == object2.hashCode() should always be true.
b. If object1.hashCode() == object2.hashCode() is true does not guarantee object1.equals(object2)
4.  Difference between ArrayList and LinkedList in Java 
 ArrayList LinkedList
ImplementationResizable ArrayDouby-LinkedList
ReverseIteratorNoYes , descendingIterator()
Initial Capacity10Constructs empty list
get(int) operationFastSlow in comparision
add(int) operationSlow in comparisionFast
Memory OverheadNoYes
5. How ConcurrentHashMap gives better performance as compared to Synchronized HashMap?
ConcurrentHashMap maintains  a list of 16 locks by default ( number of locks equal to the initial capacity , which is by default  16) each of which is used to lock on a single bucket of the Map.This indicates that 16 threads (number of threads equal to the concurrency level , which is by  default 16) can modify the collection at the same time , given ,each thread works on different bucket. So unlike hashtable, we perform any sort of operation ( update ,delete ,read ,create) without locking on entire map in ConcurrentHashMap.
6. Difference between HashMap and Hashtable in Java 
HashMapHashtable
SynchronizedNoYes
Thread-SafeNoYes
Null Keys and Null valuesOne null key ,Any null valuesNot permit null keys and values
Iterator typeFail fast iteratorFail safe iterator
PerformanceFastSlow in comparison
Superclass and LegacyAbstractMap , NoDictionary , Yes

No comments:

Post a Comment