Type Here to Get Search Results !

Java Collections Interview Questions with Answers - Part 2

0
Java  Collections Interview Questions and Answers
Part 2

Q1. What are differences between ArrayList and Vector?
ArrayList Vector
1 No method is synchronized in the ArrayList class. All methods in Vector are synchronized.
2 ArrayList object is not thread safe. Vector is thread safe.
3 Relatively performance is high Relatively performance is low
4 Introduced in 1.2 version and it is non legacy Introduced in 1.0 version and it is legacy

Q2. What is difference between ArrayList and Linked List?
ArrayList LinkedList
1 The underlying data structure is resizable or growable array. The underlying data structure is Double Linked List.
2 This is Best choice if frequent operation is retrieval and worst choice if frequent operation is insertion or deletion in the middle. This is Best choice if frequent operation is insertion or deletion in the middle and worst choice if frequent operation is retrieval .
3 This class implements Serializable , Cloneable and Random Access interfaces. This class implements Serializable , Cloneable but not Random Access interface.

Q3. What is difference between size and capacity of a Collection Object?

The difference is size means number of objects present where as capacity means no of objects it can accommodate.



Q4. What are legacy classes and interfaces present in Collections framework ?

Enumeration -> Interface
Dictonary  -> Abstract class
Hashtable  -> Concrete class
Properties  -> Concrete class
Vector          -> Concrete class
Stack          -> Concrete class


Q5. What is difference between Iterator and ListIterator?

. ListIterator is the child interface of the Iterator
. Iterator is the single direction cursor where as ListIterator is bidirectional cursor.
. While iterating the elements by Iterator we can perform only read and remove operations. But by using ListIterator we can perform  read,removal,  replace and addition of new objects also.
. Iterator is applicable for every Collection implemented class object but ListIterator is applicable only for List implemented class objects.
. We can get Iterator  by using iterator() of Collection interface where as we get ListIterator by using ListIterator() method of List interface.
. both are introduced in 1.2 version

Q6. What is difference Enumeration and Iterator?
Enumeration Iterator
1 It is legacy interface and introduced in 1.0 version. It is non-legacy and introduced in 1.2 version.
2 Applicable only for legacy classes and it is not universal cursor. Applicable for any Collection implemented class object.
3 While iterating the elements we are not allowed to remove the objects just we can perform only read operation. While iterating we can perform removal also in addition to read operation.
4 By using elements() method we can get Enumeration object. By using iterator() method we can get Iterator object.

Q7. What is difference between Enum and Enumeration?

An Enum can be used to define a group of named constants .It has been introduced in 1.5 version

Example

Class Beer
{
KO,KF,RC,FO
}

Enumeration is cursor to retrieve Objects one by one from Collection objects.

Q8. What are limitations of Enumeration?

While iterating the elements we are not allowed to perform removal operation
It is applicable only for legacy classes and it is not a universal cursor.
It can retrieve the elements only in forward direction

Q9. What are major enhancements in 1.4 version of collection frame work?

The Major enhancements are the introduction of LinkedHashSet ,LinkedHashMap and IdentityHashMap


Q10. What is relation between ListIterator and Iterator?

ListIterator is child interface of Iterator

Q11. Explain about HashSet class?

The underlying data structure is Hashtable
null values are accepted
duplicates are not allowed
insertion order is based on hashcode of the object hence insertion order is not preserved
best suitable if frequent operation is search operations
HashSet class implements Serializable and Clone able
it is implementation class for Set interface
heterogeneous objects are allowed
it is introduced in 1.2 version

Q12. What is LinkedHashSet?

It is the child class of HashSet. The main difference between HashSet and LinkedHashSet is

Q13. If we are trying to insert duplicate values in Set what will happen?

If we are trying to insert duplicate objects to the HashSet , we wont get any compile time or run time errors just the add(Object o) returns false and it doesn’t add that object.

Q14. Differences between HashSet and LinkedHashSet?

HashSet LinkedHashSet
1 The Underlying datastructure is Hashtable. The underlying datastructure is combination of LinkedList and Hashtable.
2 Insertion Order is not preserved Insertion Order is preserved
3 Introduced in 1.2 version Introduced in 1.4 version

Q15. What is LinkedHashSet?


It is the child class of HashSet.In the case of HashSet insertion order is not preserved , but in the case of LinkedHashSet insertion will be preserved.


Q16. Explain about TreeSet?

It is Collection object which can be used to represent a group of objects according to some sorting order.

The underlying data structure is Balanced tree

Duplicates are not allowed

All objects are stored according to some sorting order hence insertion order is not preserved

Heterogeneous objects are not allowed violation leads to ClassCastException

For an Empty TreeSet as firs element null value can be inserted but after inserting that first value if we are trying to insert any other objects then we will get NullPointerException

For an non empty TreeSet if we are trying to inser null value at run time u will get NullPointerException


Q17. What are differences between List and Set interfaces?


List Set
1 Insertion Order is preserved Insertion Order is not preserved
2 Duplicate Objects are allowed Duplicate Objects are not allowed
3 The implemented classes are ArrayList,LinkedList , Vector and Stack classes The implemented classes are HashSet, LinkedHashSet and Tree

Q18. What is Comparable interface?

This interface can be used for defining natural sorting order of the objects.
It is present in java.lang package
It contains a method public int compareTo(Object obj1) 


Q19. What is Comparator interface?

This interface can be used for implementing customized sorting order.
It is present in java.util package
It contains two methods
public int compare(Object ,Object)
public boolean equals(Object)

Q20. What are differences between Comparable and Comparator?

Comparable Comparator
1 This can be used for natural sorting order version. This can be used for implementing customized sorting
2 This interface present in java.lang package This is present in java.util package
3 It contains only one method:
public int compareTo(Object obj1)
It contains two methods:
public int compare(Object ,Object) public Boolean equals(Object)
4 It is marker interface It is not a marker interface.



Post a Comment

0 Comments

Top Post Ad

Below Post Ad