<<< Java Collections Interview Questions and Answers >>>

Java Collections - Questions & Answers

Here is the list of Java Collections Interview Questions. The answers are short and to the point.These questions are frequently asked by the recruiters. So we try our best to provide you the Java Collections interview questions. A list of frequently asked Collections interview questions and answers are given below.

«« | 1 2 3 4 5 | »»

1) What is Collection ?

Ans :A collection is an object that groups multiple elements into a single unit.

2) What is a Collections Framework ?

Ans : Collections framework provides unified architecture for manipulating and representing collections.

3) What are the benefits of Java Collections Framework ?

Ans :Benefits of Collections Framework are :

  1. Improves program quality and speed
  2. Increases the chances of reusability of software
  3. Decreases programming effort.

4) What is difference between an ArrayList and a Vector?

Ans : The difference between ArrayList and Vector :

ArrayList Vector
1. ArrayList is not thread-safe. 1. Vector is thread-safe.
2. The ArrayList increases its array size by 50 percent. 2. A Vector defaults to doubling the size of its array.
3. The performance is faster. 3. The performance is slower.
4. Memory Requirement is more. 4. Memory Requirement is Less.
5. ArrayList is not synchronized. 5. Vector is synchronized.

5) What is difference between a HashMap and a HashTable?

Ans : The difference between a HashMap and a HashTable :

HashMap HashTable
1. HashMap is not synchronized. 1. Hashtable is synchronized.
2. HashMap permits null values and only one null key. 2. Hashtable doesn't allow key or value as null.
3. HashMap is better for single threaded environment. 3. ashtable is suitable for multi-threaded environment.
4. The performance is faster. 4. The performance is slower.

6) What is the difference between ArrayList and LinkedList?

Ans : The difference between ArrayList and LinkedList :

ArrayList LinkedList
1. ArrayList uses a dynamic array. 1. LinkedList uses doubly linked list.
2. ArrayList is not efficient for manipulation because a lot of shifting is required. 2. LinkedList is efficient for manipulation.
3. ArrayList is better to store and fetch data. 3. LinkedList is better to manipulate data.

7) What is an Iterator ?

Ans :Iterator is an interface . It is found in java.util package. It provides methods to iterate over any Collection.

8) What is the difference between Iterator and Enumeration ?

Ans : The difference between Iterator and Enumeration :

Iterator Enumeration
1. Iterator has remove() method. 1. Enumeration doesn't.
2. Iterator is fail-fast. 2. Enumeration is not fail-fast.
3. Iterator is slower than Enumeration. 3. Enumeration is faster than Iterator.
4. Using Iterator we can manipulate objects by adding and removing the objects from the collections. 4. Enumeration behaves like a read only interface as it can only traverse the objects and fetch it .

9) What is difference between fail-safe and fail-fast?

Ans :

Iterator fail-safe property work with the clone of underlying collection, hence it’s not affected by any modification in the collection. By design, all the collection classes in java. util package are fail-fast whereas collection classes in java.util.concurrent are fail-safe.

Fail-fast iterators throw ConcurrentModificationException whereas fail-safe iterator never throws ConcurrentModificationException.

10) What is the difference between Map and Set ?

Ans :Set contains values only whereas Map contains key and values both.

«« | 1 2 3 4 5 | »»