SEMrush

Top 15 frequently asked interview questions on Java Collections



Q 1  What is Collection ? What is a Collections Framework ? 


Collection : A collection (also called as container) is an object  that groups multiple elements into a single unit.Collections Framework : Collections framework provides unified architecture for manipulating and representing collections.


Q 2 What is the root interface in collection hierarchy ? 


Root interface in collection hierarchy is Collection interface
Collection interface is a member of the Java Collections framework. 


Q 3 What is the difference between Collection and Collections ?


Collection is  an interface while Collections is a java class , both are present in java.util package and  part of java collections framework.


Q 4 What is the Collection Hierarchy in Java ?


   Source of image :  By Ervinn at en.wikibooks [CC BY-SA 3.0 ], from Wikimedia Commons


The list of important (core) collection interfaces are 



Collection , Set , Queue , List , Map


Q 5 What is the difference between List and Set ?


List is ordered but contains duplicate elements.

Set contains only unique elements  but it is unordered 
List maintains the order in which the objects are added .


Q 6 What is the difference between Map and Set ?


Set contain only unique values.
Map object has unique keys each containing some value



Q 7  What are the classes implementing List and Set interface ?



Class implementing List interface :  ArrayList , Vector , LinkedList ,

Class implementing Set interface :  HashSet , TreeSet



Q 8  What is an iterator ?


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




Q 9 What is the difference between Iterator and Enumeration ?

The main difference between Iterator and Enumeration is that Iterator has remove() method while Enumeration doesn't.
Hence , using Iterator we can manipulate objects by adding and removing the objects from the collections. Enumeration behaves like a read only interface as it can only traverse the objects and fetch it .



Q 10 What is the difference between HashMap and Hashtable ?


It is one of the most popular collections interview question for java developer . Make sure you go through this once before appearing for the interview .

Main differences between HashMap and Hashtable are :


a.  Hashtable does not allow null keys and null values. HashMap allows one null key and any number of null values.

b. Hashtable is synchronized or thread-safe .HashMap is not synchronized or thread-safe.


Q 11 What is the difference between Iterator and ListIterator.


Using Iterator we can traverse the list of objects in forward direction . But ListIterator can traverse the collection in both directions that is forward as well as backward.




Q 12 What is the difference between Array and ArrayList in Java ?

This question checks whether student understand the concept of static and dynamic array. Some main differences between Array and ArrayList are :
a.   Array can contain primitive data types but Array is static in size .
b.   ArrayList is dynamic in size but can not contain primitive data types.


Q 13 Why Map interface does not extend the Collection interface in Java Collections Framework ?


Map interface is not compatible with the Collection interface.

Since Map requires key as well as value , for example , if we want to add key-value pair then we will use put(Object key , Object value) . So there are two parameters required to add element to the HashMap object  . In Collection interface add(Object o) has only one parameter. 


Q 14 Write the code for iterating the list in different ways in java ? 


There are two ways to iterate over the list in java :

a. using Iterator

b. using for-each loop


Q 15 Explain the importance of hashCode() and equals() method ? Explain the contract also. 

HashMap object uses Key object hashCode() method and equals() method to find out the index to put the key-value pair. If we want to get value from the HashMap same both methods are used .all the collection classes that does not allow the duplicate values use hashCode() and equals() method to find the duplicate elements.So it is very important to implement them correctly

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)



Comments

Unknown said…
Software Testing Services - RKM IT services is a software testing company, which provides a comprehensive suite of services that spans across consulting, efficient, computerization, and recital focused testing. It tries to reduce the defects that impacts business use of the application in future.