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

Chapter 5 Collection Framework Answers

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)
11 views6 pages

Chapter 5 Collection Framework Answers

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
You are on page 1/ 6

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
www.sterlinginstitute.in
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
www.sterlinginstitute.in
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 Arrays.asList(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
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

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
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.

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
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

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
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?

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 Collections.reverse(Collection) and Collections.sort(Collection) method.

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

Ans: By using Collections.synchronizedCollection(Collection) method.

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

You might also like