Popular Java interview questions 2
- Difference between Array and ArrayList in Java
| Array | ArrayList | |
|---|---|---|
| Resizable | No | Yes |
| Primitives | Yes | No |
| Iterating values | for, for each | Iterator , for each |
| Length | length variable | size method |
| Performance | Fast | Slow in comparision |
| Multidimensional | Yes | No |
| Add Elements | Assignment operator | add 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
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.
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 | |
|---|---|---|
| Implementation | Resizable Array | Douby-LinkedList |
| ReverseIterator | No | Yes , descendingIterator() |
| Initial Capacity | 10 | Constructs empty list |
| get(int) operation | Fast | Slow in comparision |
| add(int) operation | Slow in comparision | Fast |
| Memory Overhead | No | Yes |
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
| HashMap | Hashtable | |
|---|---|---|
| Synchronized | No | Yes |
| Thread-Safe | No | Yes |
| Null Keys and Null values | One null key ,Any null values | Not permit null keys and values |
| Iterator type | Fail fast iterator | Fail safe iterator |
| Performance | Fast | Slow in comparison |
| Superclass and Legacy | AbstractMap , No | Dictionary , Yes |
No comments:
Post a Comment