Chapter 5 Collection Framework Answers
Chapter 5 Collection Framework Answers
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.
Ans: Collection Framework provides unified architecture to store and manipulate the group
of objects.
Ans: Collection is an interface that contains all the generalized method but Collections is a
utility class in java.
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.
Ans: Because TreeSet is SortedSet and sorting is applied only on comparable objects.
Ans: Queue is an interface in java and it holds multiple elements prior to processing.
Ans: PriorityQueue is a sorted Queue in java and does not allow heterogeneous and null
values.
Ans: boolean, if element is successfully added in the collection then method returns true
otherwise it will return false.
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.
Ans: If we already know the type and size of elements then it is recommended to use array
rather than collection.
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
www.sterlinginstitute.in
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
Ans: To get the object of Iterator interface we call Collection interface’s iterator() method on
the object of Collection implemented classes.
Ans: To get the object of Enumeration interface we call legacy class’s elements() method.
Ans: To get the object of ListIterator we call List interface’s listIterator() method.
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
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
Ans: Autoboxing is the automatic conversion that the Java compiler makes between the
primitive data types and their corresponding wrapper classes.
G-5, 150 New Azad Nagar Behind Akashwani, Daly College Road, Indore – 452001 (M.P)
+91-91099 13455
www.sterlinginstitute.in
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.
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.
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
java.lang package java.util package
4. It has only one method that is comparTo() It has two methods those are compare ()
method and equals () method
Ans: Both method returns integer value that can be positive, negative or zero
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
www.sterlinginstitute.in
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?
G-5, 150 New Azad Nagar Behind Akashwani, Daly College Road, Indore – 452001 (M.P)
+91-91099 13455
www.sterlinginstitute.in