0% found this document useful (0 votes)
7 views6 pages

Java Collections Framework Interview Guide

Uploaded by

Furqan Belim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Java Collections Framework Interview Guide

Uploaded by

Furqan Belim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Answer Keys to Interview Questions

Chapter: Collections Framework


Q1. What is Collection?

Ans: Group of objects in known as collection.

Q2. What is Framework?

Ans: Framework is a semi built application that may contain predefined classes and functions
that can be used to process input, manage hardware devices, and interact with the system
easily.

Q3. What is Collections Framework?

Ans: Collection Framework provides unified architecture to store and manipulate the group
of objects.

Q4. Why do we use Collection Framework?


Ans: Collection Framework provides the solution of all the limitation of array like
Collections are grow able and it contains heterogeneous type of objects that’s why we use
collections.

Q5. What is the difference between Collection and Collections?

Ans: Collection is an interface that contains all the generalized method but Collections is a
utility class in java.

Q6. Why Map is not a true collection?

Ans: Because Map is not a child of Collection Interface.

Q7. What is the difference between Collection and Map?

Ans: A Collection represents a group of objects, known as its elements but Map stores group
of objects in key and value pair. A Map cannot contain duplicate keys.

Q8. What is the difference between List and Set?

S. List (I) Set (I)


No.
1. Insertion Order is preserved in List Insertion order is not preserved in Set
2. Duplicate elements are allowed in List Duplicate Elements are not allowed in Set

Q9. What is the difference between ArrayList and LinkedList?

S. ArrayList (C) LinkedList (C)


No.
1. ArrayList internally uses a dynamic LinkedList internally uses a doubly linked
array, growable array or resizable array list to store the elements.
G-5, 150 New Azad Nagar Behind Akashwani, Daly College Road, Indore – 452001 (M.P)
+91-91099 13455
[Link]
Answer Keys to Interview Questions
2. Insertion or deletion from middle is Insertion or deletion from middle is faster
slow because uses an array. If any than ArrayList because it uses a doubly
element is removed from the array, all linked list, so no bit shifting is required in
the bits are shifted in memory. memory.
3. ArrayList is better for storing and LinkedList is better for manipulating data.
accessing data.

Q10. What are the differences between Vector and ArrayList?

S. ArrayList (C) Vector (C)


No.
1. ArrayList is Asynchronous Vector is synchronized
2. ArrayList is fast because it is Vector is slow because it is
Asynchronous synchronous
3. ArrayList is not Thread-Safe Vector is Thread-Safe
4. ArrayList grows: Vector grows:
NC = CC *(3/2)+1 NC = 2 * CC
5. Used in single user application Used in multi-user application
6. ArrayList introduced in JDK 1.2 so it is Vector is a legacy class
not legacy

Q11. What are the differences between HashSet and LinkedHashSet?

S. HashSet (C) LinkedHashSet (C)


No.
1. HashSet internally uses a Hashtable LinkedHashSet internally uses a
LinkedList and Hashtable
2. Insertion order is not preserved Insertion order is preserved
3. Introduced in JDK 1.2 Introduced in JDK 1.4

Q12. Why TreeSet can contain only homogenous type of elements?

Ans: Because TreeSet is SortedSet and sorting is applied only on comparable objects.

Q13. What is Queue?

Ans: Queue is an interface in java and it holds multiple elements prior to processing.

Q14. What is PriorityQueue?

Ans: PriorityQueue is a sorted Queue in java and does not allow heterogeneous and null
values.

Q15. What are the differences between HashMap and LinkedHashMap?

S. HashMap (C) LinkedHashMap (C)


No.
1. HashMap internally uses Hashtable LinkedHashMap internally uses a
G-5, 150 New Azad Nagar Behind Akashwani, Daly College Road, Indore – 452001 (M.P)
+91-91099 13455
[Link]
Answer Keys to Interview Questions
LinkedList and Hashtable
2. Insertion order is not preserved Insertion order is preserved

Q16. What is the return type of Collection interface add(E) method?

Ans: boolean, if element is successfully added in the collection then method returns true
otherwise it will return false.

Q17. What it is the return type of Map interface put(K, V) method?

Ans: Object (value) is the return type of put(K, V) method, if we put value with duplicate key
then put method returns previous value otherwise it will return null.

Q18. Which one is better array or collection?

Ans: If we already know the type and size of elements then it is recommended to use array
rather than collection.

Q19. What are the limitations of array?

Ans: i) Arrays are fixed in size

ii) Arrays contain only homogenous type of elements

Q20. How to convert an array into a collection?

Ans: We can use [Link](array) it will return Object of List.

Q21. What are the implemented data structures of collection Classes?

S. Collection Classes Implemented Data Structures


No.
1. ArrayList Dynamic Array or Growable Array or
Resizable Array
2. LinkedList Doubly LinkedList
3. Stack Stack
4. Vector Dynamic Array or Growable Array or
Resizable Array
5. HashSet Hash table
6. TreeSet Balanced Binary Tree
7. Queue Queue
8. HashMap Hash table
9. TreeMap Balanced Binary Tree
10. Hashtable Hash table

Q22. What are the differences between HashMap and Hashtable?

S. HashMap Hashtable
No.
1. HashMap is Asynchronous Hashtable is Synchronous
G-5, 150 New Azad Nagar Behind Akashwani, Daly College Road, Indore – 452001 (M.P)
+91-91099 13455
[Link]
Answer Keys to Interview Questions
2. It is not Thread-Safe It is Thread-Safe
3. It permits null values as key and value It does not permit null values
both
4. HashMap is fast because it is Hashtable is slow because it is
asynchronous synchronous
5. It is used in single user applications It is used in multi-user applications

Q23. How to get the object of Iterator interface?

Ans: To get the object of Iterator interface we call Collection interface’s iterator() method on
the object of Collection implemented classes.

Q24. How to get the object of Enumeration interface?

Ans: To get the object of Enumeration interface we call legacy class’s elements() method.

Q25. How to get the object of ListIterator interface?

Ans: To get the object of ListIterator we call List interface’s listIterator() method.

Q26. Differentiate Iterator and Enumeration interface?

S. Iterator Enumeration
No.
1. Iterator is a universal cursor Enumeration is only applicable for
legacy classes like Vector, Stack, and
Hashtable
2. Having capability to remove elements Does not capable remove elements
3. There are 3 methods in Iterator interface There are 2 methods in Enumeration
inteface
4. Introduced in JDK 1.2 Introduced in JDK 1.0
5. Iterator is Fail-Fast in nature Enumeration is Fail-Safe in nature
6. Modifications are not allowed while Modifications are allowed while
iterating so it is considered as secure iterating elements so it is not secure

Q27. What is Fail-fast?

Ans: If the collection is structurally modified at any time after the iterator is created, in
anyway except through the Iterator’s own remove () method, then Iterator will raise
ConcurrentModificationException is known as Fail-fast

Q28. What is Auto-boxing and Auto un-boxing?

Ans: Autoboxing is the automatic conversion that the Java compiler makes between the
primitive data types and their corresponding wrapper classes.

Auto un-boxing is the automatic conversion of an object of a wrapper type to its


corresponding primitive value.

G-5, 150 New Azad Nagar Behind Akashwani, Daly College Road, Indore – 452001 (M.P)
+91-91099 13455
[Link]
Answer Keys to Interview Questions
Q29. What is the importance of equals () and hashCode () methods in Collection
classes? Why do we override them?

Ans: The equals() method is used to verify the equality of objects. Its default implementation
checks the object references of two objects to verify their equality. It is used in collections to
search an object to check the existence or remove it from collection.

hashCode() method returns a unique integer value for the object in runtime.

To satisfy the contract between hashCode() and equals () method it is mandatory to override
both the methods.

Q30. What is the contract between hashCode () and equals () method?

Ans: The contract between hashCode () and equals () method is:

i) If two objects are equal, then they must have the same hash code.

ii) If two objects have the same hash code, they may or may not be equal.

Q31. Differentiate Comparable and Comparator interfaces?

S. Comparable Comparator
No.
1. Comparable is used for Default Natural Comparator is used for Customized
Sorting Order Sorting Order
2. It uses primary key to order collection Number of comparator are depending
on number of attributes to be sorted
3. Comparable interface is available in Comparator interface is available in
[Link] package [Link] package
4. It has only one method that is comparTo() It has two methods those are compare ()
method and equals () method

Q32. What is the return type of compare () and compareTo() method?

Ans: Both method returns integer value that can be positive, negative or zero

i) Negative indicates first object is smaller than second object

ii) Positive indicates first object is greater than second object

iii) Zero indicates that both objects are equal

Q33. Which design pattern is followed by Iterator interface?

Ans: Iterator design pattern is followed by Iterator interface.

Q34. How many Comparable and Comparator can be created for a class?

G-5, 150 New Azad Nagar Behind Akashwani, Daly College Road, Indore – 452001 (M.P)
+91-91099 13455
[Link]
Answer Keys to Interview Questions
Ans: Only one comparable can be created for a class but number of comparator is depending
on number of attributes to be sorted.

Q35. How will you decide, how many Comparator you need to create?

Ans: Number of comparator is depending on number of attributes to be sorted.

Q36. How will you reverse or sort a collection in Java?

Ans: By using [Link](Collection) and [Link](Collection) method.

Q37. How can you synchronize a non-synchronized collection?

Ans: By using [Link](Collection) method.

G-5, 150 New Azad Nagar Behind Akashwani, Daly College Road, Indore – 452001 (M.P)
+91-91099 13455
[Link]

Common questions

Powered by AI

Both HashMap and Hashtable utilize hashing to store key-value pairs, but they differ significantly in synchronization and handling of null values. Hashtable is synchronized, providing thread safety but at a performance cost; HashMap is unsynchronized and faster but requires manual synchronization in multi-threaded contexts. Additionally, HashMap allows null keys and values, increasing flexibility, while Hashtable disallows them, reflecting its legacy design for stricter data integrity .

The Iterator interface follows the Iterator design pattern, enabling sequential access to elements within a collection without exposing the underlying structure. This pattern provides a uniform traversal interface for various collection types, supports operations like element removal during iteration, and enhances encapsulation by decoupling collection operations from their implementations. It also facilitates the introduction of additional collection types without altering client code, promoting flexibility and scalability .

Vector is synchronized, making it thread-safe for concurrent modifications, but this synchronization leads to slower performance compared to ArrayList, which is not synchronized. ArrayList is faster in single-threaded applications due to its asynchronous nature but is not thread-safe, requiring external synchronization when used concurrently. This difference guides their use; Vector is preferable in multi-threaded environments where simplicity in handling thread safety is desired .

The hashCode() and equals() contract ensures consistent behavior in collections that rely on hashing, like HashMap. The contract stipulates that equal objects must have equal hash codes, ensuring correct retrieval and storage. Violating this contract leads to defective collection behavior, such as allowing inequality-based checking to fail, which could cause incorrect key-value pairs to be stored or retrieved falsely, undermining the functionality and reliability of the collection .

Iterator is a universal cursor available for Collections and allows element removal during iteration, enhancing security by preventing modifications. Enumeration, however, is limited to legacy classes like Vector and lacks the ability to remove elements directly, allowing modifications that pose security risks during iteration. Iterator is considered Fail-Fast, throwing exceptions on concurrent modifications, whereas Enumeration is Fail-Safe, permitting secure but less efficient iteration .

ArrayList is less efficient for insertion and deletion from the middle of the list because it uses a dynamic array, which requires shifting elements in memory. In contrast, LinkedList is more efficient for these operations since it uses a doubly linked list, allowing direct modification without shifting. Consequently, ArrayList is better for scenarios involving frequent access or searches, while LinkedList is preferable for contexts with frequent insertions and deletions .

Maps are not considered true collections because they do not implement the Collection interface. While a Collection represents a group of objects, a Map stores data in key-value pairs and does not allow duplicate keys, distinguishing its structure and usage from typical Collection types .

Auto-boxing and auto-unboxing simplify Java code by automatically converting primitive data types to their corresponding wrapper classes and vice versa when used with collections. This conversion facilitates the storage of primitive types in object-based collections like ArrayList. However, risks include potential performance overhead due to frequent conversions and the inadvertent introduction of null object errors when unboxing a null element, leading to NullPointerExceptions .

TreeSet only allows homogeneous elements because it is a SortedSet that maintains elements in a sorted order. This sorting requires elements to be comparable to one another, necessitating homogeneity in the type of elements stored for consistent ordering. This restriction limits its use to contexts where such ordered, comparable elements are available, like maintaining a sorted list of numbers or strings .

Overriding equals() and hashCode() is crucial because these methods are used to determine object uniqueness in hashing-based collections like HashMap. The equals() method defines object equality, while hashCode() provides a unique identifier for the object. The contract between the two methods requires that equal objects have identical hash codes; failing to override these can lead to incorrect storage and retrieval behavior, such as allowing duplicate entries or losing objects in such collections .

You might also like