Java Collections Interview Questions and Answers
Part 1
Q1. What are differences between arrays and collections?
Sr. No. | Array | Collections |
---|---|---|
i | Arrays are fixed in size and hence once we created an array we are not allowed to increase or decrease the size based on our requirement. | Collections are grow able in nature and hence based on our requirement we can increase or decrease the size. |
ii | Memory point of view arrays are not recommended to use | Memory point of view collections are recommended to use. |
iii | Performance point of view arrays are recommended to use | Performance point of view collections are not recommended to use. |
iv | Arrays can hold only homogeneous elements | Collections can hold both homogeneous and heterogeneous elements. |
v | Arrays can hold both primitives as well as objects | Collections can hold only objects. |
vi | For any requirement, there is no ready method support compulsory programmer has to code explicitly. | For every requirement ready made method support is available. Being a programmer we have to know how to use those methods and we are not responsible to implement those. |
Q2. What are limitations of object Arrays?
The main limitations of Object arrays are:-
These are fixed in size i.e. once we created an array object there is no chance of increasing or decreasing size based on our requirement. Hence If we don’t know size in advance , arrays are not recommended to use. Arrays can hold only homogeneous elements.
There is no underlying data structure for arrays and hence no ready made method support for arrays. Hence for every requirement programmer has to code explicitly.So to over come these problems collections are recommended to use.
Q3. What is Collection framework?
It defines set of classes and inter faces which can be used for representing a group of objects as single entity.
Q4. What is Collection API ?
It defines set of classes and interfaces which can be used for representing a group of objects as single entity.
Q5. Explain about Collection interface?
• This interface can be used to represent a group of objects as a single entity.
• It acts as root interface for entire collection framework.
• It defines the most commonly used methods which can be applicable for any collection implemented class object.
Q6. What is difference between Collections and Collection?
Collection is an interface which can be used for representing a group of individual objects as single entity and it acts as root interface of collection frame work.
Collections is an utility class to define several utility methods for Collection implemented class objects.
Q7. Explain about List interface?
List interface is a child interface of Collection interface. This can be used to represent group of individual objects in as a single entity where
• Duplicates are allowed
• Insertion order is preserved
Q8. Explain about Set interface?
Set is a child interface of Collection interface. it can be used to represent a group of individual objects as a single entity where
• Duplicate objects are not allowed.
• Insertion order is not preserved
Q9. Explain about SortedSet interface?
it is child interface of Set interface. it can be used to represent a group of individual objects in to a single entity where
• All the objects are arranged in some sorting order (Can be natural sorting order or customized).
• Duplicates are not allowed.
Q10. Explain about NavigableSet ?
It is child interface of SortedSet and provides several utility methods for navigation purposes
• It doesn’t allows duplicates
• Insertion order is preserved
• It is introduced in 1.6 version
Q11. Explain about Queue interface?
If we want to represent a group of individual objects prior to processing, then we should go for Queue interface. It is child interface of Collection interface.
It has introduced in 1.5 version.
Q12. Explain about Map interface?
Remember it is not a child Interface of Collection Interface and hence Map and Collection Interfaces doesn’t have any relationship.
• It can be used for representing a group of Objects as key, value pairs.
• Both keys and values should be objects
• Keys can t be duplicated but values can be duplicated.
• it has introduced in 1.2 version
Q13. Explain about Sorted Map ?
• If we want to represent a group of objects as key value pairs where all the entries are arranged according some sorting order of keys then we should go for Sorted Map.
• It is child interface of Map.
• It has introduced in 1.2 version
Q14. Explain about Navigable Map?
• It is child interface of Sorted Map and defines several method for navigation purpose
• It is introduced in 1.6 version
Q15. Explain about ArrayList class?
ArrayList is a Collection which can be used to represent a group of objects as a single entity.
• it is a implemented class for List interface
• Introduced in 1.2 version
• The underlying data structure is resize-able or grow able array.
• Insertion order is preserved
• Duplicates are allowed
• Heterogeneous objects are allowed
• null insertion is possible
• This class implements Random Access , Serializable , Clone-able interfaces
• Best choice for retrieval purpose and worst if our frequent operation is insertion or deletion in the middle.
Q16. What is Random Access Interface?
• If a collection class implements Random Access interface then we can access any of its element with the same speed.
• Random Access interface is marker interface and it dosen't contains any methods.
• ArrayList and vector classes implements this interface.
Q17. Explain about LinkedList class?
LinkedList is a Collection implemented class which can be used for representing a group of objects as a single entity. LinkedList is the implementation class for List interface
• Introduced in 1.2 version
• Underlying data Structure is Double LinkedList
• Allows duplicates
• Insertion order is preserved
• Allows heterogeneous objects
• null insertion is possible
• LinkedList class implements Serializable and Clone-able interface but not Random Access interface
• Best choice if frequent operation is insertion or deletion an objects in middle but worst choice if frequent operation is retrieval.
Q18. Explain about Vector class?
Vector is a legacy collection class which can be used to represent a group of objects.
• Introduced in 1.0 version. it is legacy class
• The underlying data structure is resizable or grow able array.
• Insertion order is preserved
• Duplicates are allowed
• Heterogeneous objects are allowed
• It is a implemented class for List interface
• null insertion is possible
• Vector class implements Random Access ,Serializable,Clone-able interfaces
• Best Choice if frequent operation is retrieval and worst choice if frequent operation is insertion or deletion in the middle.
• All methods present in Vector class are synchronized hence Vector class object is thread safe.
Q19. How we can get synchronized version of ArrayList?
Collections class contains synchronizedList() method for this
Public static List synchronizedList(List l)
Example:
ArrayList l= new ArrayList();
List l2=Collections.synchronizedList(l);
Similarly we can get synchronized versions of Set and Map objects by the following methods.
Public static List synchronizedSet(Set s) Public static List synchronizedMap(Map m)
Q20. what are differences between arrays and ArrayList?
Refer ans no 1 .
Recommended Posts
Java Collections Interview Questions Part 2
Java Collections Interview Questions Part 3
Core Java Important Questions Part 1
Core Java Important Questions Part 2
Core Java Important Questions Part 3
Core Java Important Questions Part 4
Core Java Important Questions Part 5
Core Java Important Questions Part 6
Core Java Important Questions Part 7
Core Java Important Questions Part 8
Core Java Important Questions Part 9
Core Java Important Questions Part 10
Java MCQs with Answers Set III
If you have any doubts join the discussion below ,our Moderator will reply to your Queries
Thanks for simple answers
ReplyDelete