Java Collections Interview Question and Answer for freshers and experienced

Categories: Interview questions and answers Experienced Freshers Java Java Script

Java Collections Interview Question and Answer for freshers and experienced

 

Q1. What are the basic interfaces of the Java Collections Framework? 

Answer: Java Collections Framework provides a well-designed set of interfaces and classes that support operations on a collection of objects. 

The most basic interfaces that reside in the Java Collections Framework are: 

  • Collection, which represents a group of objects known as its elements. 
  • Set, which is a collection that cannot contain duplicate elements. 
  • List, which is an ordered collection and can contain duplicate elements. 
  • Map, which is an object that maps keys to values and cannot contain duplicate keys.

 

Q2. Why Collection doesn’t extend Cloneable and Serializable interfaces? 

Answer: The Collection interface specifies groups of objects known as elements. Each concrete implementation of a Collection can chooses its own way of how to maintain and order its elements. Some collections allow duplicate keys, while some other collections don’t. The semantics and the implications of either cloning or serialization come into play when dealing with actual implementations. Thus, the concrete implementations of collections should decide how they can be cloned or serialized.

 

Q3. What is an Iterator? 

Answer: The Iterator interface provides a number of methods that are able to iterate over any Collection. Each Java Collection contains the iterator method that returns an Iterator instance. Iterators are capable of removing elements from the underlying collection during the iteration.

 

Q4. What differences exist between Iterator and List Iterator? 

Answer: The differences of these elements are listed below: 

  • An Iterator can be used to traverse the Set and List collections, while the ListIterator can be used to iterate only over Lists. 
  • The Iterator can traverse a collection only in forward direction, while the ListIterator can traverse a List in both directions. 
  • The ListIterator implements the Iterator interface and contains extra functionality, such as adding an element, replacing an element, getting the index position for previous and next elements, etc.

 

Q5. What is difference between fail-fast and fail-safe? 

Answer:  The Iterator’s fail-safe property works with the clone of the underlying collection and thus, it is not affected by any modification in the collection. All the collection classes in java.util package are fail-fast, while the collection classes in java.util.concurrent are fail-safe. Fail-fast iterators throw a ConcurrentModificationException, while fail-safe iterator never throws such an exception.

 

Q6. How HashMap works in Java? 

Answer: A HashMap in Java stores key-value pairs. The HashMap requires a hash function and uses hashCode and equals methods, in order to put and retrieve elements to and from the collection respectively. When the put method is invoked, the HashMap calculates the hash value of the key and stores the pair in the appropriate index inside the collection. If the key exists, its value is updated with the new value. Some important characteristics of a HashMap are its capacity, its load factor and the threshold resizing.

 

Q7. What is the importance of hashCode() and equals() methods?

Answer:  In Java, a HashMap uses the hashCode and equals methods to determine the index of the key-value pair and to detect duplicates. More specifically, the hashCode method is used in order to determine where the specified key will be stored. Since different keys may produce the same hash value, the equals method is used, in order to determine whether the specified key actually exists in the collection or not. Therefore, the implementation of both methods is crucial to the accuracy and efficiency of the HashMap.

 

Q8. What differences exist between HashMap and Hashtable? 

Answer: Both the HashMap and Hashtable classes implement the Map interface and thus, have very similar characteristics. 

However, they differ in the following features: 

  • A HashMap allows the existence of null keys and values, while a Hashtable doesn’t allow either null keys, or null values. 
  • A Hashtable is synchronized, while a HashMap is not. Thus, HashMap is preferred in single-threaded environments, while a Hashtable is suitable for multi-threaded environments. 
  • A HashMap provides its set of keys and a Java application can iterate over them. Thus, a HashMap is fail-fast. On the other hand, a Hashtable provides an Enumeration of its keys. 
  • The Hashtable class is considered to be a legacy class.

 

Q9. What is the difference between Array and ArrayList? When will you use Array over ArrayList? 

Answer: The Array and ArrayList classes differ on the following features: 

  • Arrays can contain primitive or objects, while an ArrayList can contain only objects. 
  • Arrays have fixed size, while an ArrayList is dynamic. 
  • An ArrayList provides more methods and features, such as addAll, removeAll, iterator, etc. 
  • For a list of primitive data types, the collections use autoboxing to reduce the coding effort. However, this approach makes them slower when working on fixed size primitive data types.

 

Q10. What is difference between ArrayList and LinkedList? 

Answer:  Both the ArrayList and LinkedList classes implement the List interface, but they differ on the following features: 

  • An ArrayList is an index-based data structure backed by an Array. It provides random access to its elements with a performance equal to O(1). On the other hand, a LinkedList stores its data as list of elements and every element is linked to its previous and next element. In this case, the search operation for an element has execution time equal to O(n). 
  • The Insertion, addition and removal operations of an element are faster in a LinkedList compared to an ArrayList, because there is no need of resizing an array or updating the index when an element is added in some arbitrary position inside the collection. 
  • A LinkedList consumes more memory than an ArrayList, because every node in a LinkedList stores two references, one for its previous element and one for its next element. Check also our article ArrayList vs. LinkedList.

Top articles
Servlet interview questions and answers for experienced Published at:- Spring Interview Questions and Answer for Experienced Published at:- Spring Boot Features for Java Developers (2022) Published at:- Detecting build version and time at runtime in Spring Boot Published at:- AWS Interview Questions Answers for Freshers and Experienced Published at:- Laravel Interview Question Answer for Freshers and Experienced Published at:- Splunk Interview Questions and Answers for freshers and Experienced Published at:- IOS Interview Questions for freshers and experienced Published at:- Red Hat Interview Question Answers for Freshers and experienced Published at:- Python Interview Question and Answer Published at:- Basic Indian History General Knowledge Question and Answer (Quiz) Published at:- AWS Interview Question and Answer for freshers and Experienced Published at:- Angular 13 interview Question and Answer Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination (Set 2) Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination (Set 3) Published at:- Limited Departmental Competitive examination (LDC) examination (Water supply) Published at:- Interview Question and answer for Limited Departmental Competitive Examination Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination (Soil mechanics) Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination Sanitary Engineering Published at:- Interview Question for Limited Departmental Competitive Examination (LDC) Principles of Surveying Published at:- Interview MCQ question for railways New Lines, Doublings & Gauge Conversion Projects Published at:- MCQ Interview Question and answer for Railway Station and Passenger Amenities Published at:- Interview Question and Answer for Railway preparation Published at:- PHP MCQ Quiz Question with Answer Published at:- PHP MCQ Quiz Question with Answer (set 2) Published at:- Angular MCQ Quiz Question and Answer Published at:- Angular MCQ Quiz Question and Answer Set 2 Published at:- Java General Interview Questions and Answer Published at:- Java Threads Interview Question and Answer for experienced Published at:- Java Collections Interview Question and Answer for freshers and experienced Published at:- Garbage Collector in Java Frequently Asked Questions and Answers Published at:- Difference between Exception and Error in java Exception Handling Published at:- Java Applets Interview Questions with Answer Published at:- Java Swing Interview Questions with Answer for freshers and experienced Published at:- Java Database Connectivity (JDBC) Interview Question and answer Published at:- Remote Method Invocation (RMI) Interview Question and Answer Published at:- Jakarta Server Page (JSP) programming language Interview Question and Answer Published at:- Write a program in Java to display the first 10 terms of the following series: 10, 20, 30, 40, …….. Published at:- Write the program in Java to display the first ten terms of the following series: 1, 4, 9, 16, Published at:-
R4Rin Team
The content on R4Rin.com website is created by expert teams.