Top 50+ Java Collections Interview Questions Answers

31. Which collection interface extends Queue interface in java?

Answer: Deque interface extends Queue interface. It was added to the collection framework in Java SE6.

32. What are concrete subclasses that implement the Deque interface?

Answer: The concrete classes such as LinkedList and ArrayDeque implement Deque interface.

33. What are concrete classes that implement Map interface in java?

Answer: The concrete classes that implement Map interface, are as follows:

  • HashMap
  • HashTable
  • LinkedHashMap
  • TreeMap

34. Which interface extends Map interface and is also implemented by TreeMap class?

Answer: SortedMap interface extends Map interface.

35. Which method is used to get the total number of elements in a collection?

Answer: Collection interface provides size() method that can be used to get elements of a collection.


36. Is it a good approach to use Generic features in collections?

Or, What is the key advantage of the generic collection?

Answer: Yes, it is a good approach to use generics in collections because generics provide type safety. While iterating elements of a collection, typecasting is not required if uses generic class.

For more detail, go to this tutorial: Generics in Java.

37. What are the bulk operations supported by collection interface in java?

Answer: The collection interface supports the following bulk operations such as: add, addAll, clear, containsAll, removeAll, retainAll, etc.

38. How to check a collection is empty or not?

Answer: Collection interface provides a method isEmpty() that can be used to check a collection is empty or not.

39. Is it possible that a collection object can be cloned and serialized?

Answer: Yes, it is possible to clone and serialize a collection object because all the concrete classes in the Java Collections Framework implement the java.lang.Cloneable and java.io.Serializable interfaces, except java.util.PriorityQueue that does not implement the Cloneable interface.

40. What method will you use to add all the elements from one collection to another collection?

Answer: addAll(Collection c).


41. Why Map interface does not extend Collection interface in Java?

Answer: Map interface and its implementation classes are the part of Java collections framework. But Map is not collection and collection is not map. Map stores elements in key-value pair fashion. Hence, it does not make any sense for Map to extend Collection interface or vice-versa.

42. What are the legacy classes that are not a part of collections framework now?

Answer: The legacy classes that are not part of collections framework, are as follows:

  • Vector
  • HashTable
  • Dictionary
  • Properties
  • Stack

43. What is List in Java?

Answer: A list in Java is a collection for storing elements in sequential order. It is a sub-interface of the collection interface that is available in java.util package.

44. What are the concrete subclasses that implement List interface?

Answer: The concrete subclasses that implement List interface, are: ArrayList, LinkedList, Vector, and Stack.

45. What are the important features of list in Java?

Answer: The most important features of list in java are as follows:

  • List is an ordered collection where elements are maintained by index positions because it uses an index-based structure.
  • It allows storing duplicate elements in Java.
  • We can add an element at any position in the list.
  • List maintains insertion order.
  • It allows for storing many null elements.
  • List uses a resizable array for its implementation. It means we can increase or decrease the size of the array.
  • Using ListIterator, we can iterate elements of list in both directions.

46. How to create an object of generic list in Java?

Answer: After the introduction of Generics in Java 1.5, we can restrict the type of object that can be stored in the list.

The general syntax to create a list of objects with a generic type parameter is as:

List<T> list = new ArrayList<T>(); // Here, T is a type parameter.

For example:
a. List<String> list = new ArrayList<String>(); // Creating a list of objects of String type using ArrayList.

47. What is the result of the following code snippet?

import java.util.ArrayList;
import java.util.List;
public class Test
{
  public static void main(String[] args)
  {
     List<String> list = new ArrayList<>();
      list.add("Monday");
      list.add("Tuesday");
      list.remove(0);
      System.out.println(list.get(0));
   }
}

Answer: Output: Tuesday

48. Will the below code snippet compile fine? If yes, what will be the result of the following program?

import java.util.ArrayList;
import java.util.List;
public class Addition
{
  public static void main(String[] args)
  {
    List<String> list = new ArrayList<String>();
     list.add("Herry");
     list.add("Deep");
     list.stream().forEach(s -> System.out.println(s));
   }
}

Answer: Yes, the above code will compile successfully. Output: Herry, Deep.

49. What will be the result of the following program?

import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args)
{
  List<String> list = new ArrayList<String>();
   list.add("Dhanbad");
   list.add("Cape town");
   list.add("New York");
   list.removeIf( x -> x.length() > 10);
   System.out.println(list.size());
  }
}

Answer: Output: 3

50. Is it possible to traverse a list in both directions?

Answer: Yes, we can traverse a list in both directions using ListIterator.

51. How do we create a list from an array of objects?

Answer: Arrays class provides asList() method that can be used to create a list from an array of objects. The sample of code is as follows:

import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args)
{
    String[ ] str = {"Dhanbad", "Ranchi", "Mumbai"};
    List<String> list = Arrays.asList(str);
    System.out.println(list);
   }
}
Output:
            [Dhanbad, Ranchi, Mumbai]

Test Your Knowledge


52. Suppose that list1 is a list that contains strings Apple, Orange, and Banana and that list2 is another list that contains the strings Apple, Orange, and Guava.

Answer the following questions:

a. What will be the result of list1 and list2 after executing list1.addAll(list2)?

b. What will be the result of list1 and list2 after executing list1.add(list2)?

c. What will be the result of list1 and list2 after executing list1.removeAll(list2)?

d. What will be the result of list1 and list2 after executing list1.remove(list2)?

e. What will be the result of list1 and list2 after executing list1.retainAll(list2)?

f. What will be the result of list1 after executing list1.clear()?


Hope that this tutorial has covered all the important java collections framework interview questions with the best possible answers. I hope that you will have understood the answers of all java collections interview questions and practiced coding problems.
All the best!!!