REGISTER FREE to Receive Instant Job

Saturday, 6 December 2014

Java Collections Interview Questions

1- What is framework?
A framework is a collection of classes and interfaces that facilitate development of specific kind of applications by providing an architectural model and reusable components.
2- What is Collection?
Collection Framework provides a unified model to work with different types of data structures such as array, linked list, binary search tree, hash table etc…
Collection framework provides many interfaces (List, Set, Queue, Deque etc.) and classes (ArrayList, HashSet, TreeSet, LinkedList, LinkedHashSet, Vectoretc).
At the core of collection framework is an interface named java.util.Collection. This interface describes common behavior of all collections.
3- What is the difference between ArrayList and Vector?
No.
ArrayList
Vector
1)
ArrayList is not synchronized.
Vector is synchronized.
2)
ArrayList is not a legacy class.
Vector is a legacy class.
3)
ArrayList increases its size by 50% of the array size.
Vector increases its size by doubling the array size.
4-What is the difference between ArrayList and LinkedList?
No.
ArrayList
LinkedList
1)
ArrayList uses a dynamic array.
LinkedList uses doubly linked list.
2)
ArrayList is not efficient for manipulation because a lot of shifting is required.
LinkedList is efficient for manipulation.
3)
ArrayList is better to store and fetch data.
LinkedList is better to manipulate data.
5- What is the difference between Iterator and ListIterator?
Iterator traverses the elements in forward direction only whereas ListIterator traverses the elements in forward and backward direction.
No.
Iterator
ListIterator
1)
Iterator traverses the elements in forward direction only.
ListIterator traverses the elements in backward and forward directions both.
2)
Iterator can be used in List, Set and Queue.
ListIterator can be used in List only.
6- What is the difference between Iterator and Enumeration?
No.
Iterator
Enumeration
1)
Iterator can traverse legacy and non-legacy elements.
Enumeration can traverse only legacy elements.
2)
Iterator is fail-fast.
Enumeration is not fail-fast.
3)
Iterator is slower than Enumeration.
Enumeration is faster than Iterator.
7- What is the difference between HashMap and Hashtable?
No.
HashMap
Hashtable
1)
HashMap is not synchronized.
Hashtable is synchronized.
2)
HashMap can contain one null key and multiple null values.
Hashtable cannot contain any null key or null value.
8-What is the difference between Comparable and Comparator?
1- Comparable interface is used to make a class comparable i.e. if a class is comparable, order of any two objects of the class can be obtained. This interface is found in java.lang package and contains only one method named compareTo(Object) (java.lang.Comparable).
2- Comparator interface is used to define multiple sorting orders for the objects of a class. Multiple sorting orders can be defined using Comparator because Comparator interface is implemented in a separate class i.e. for defining multiple sorting orders for the objects of a class, multiple implementations of Comparator interface can be provided.
No.
Comparable
Comparator
1)
Comparable provides only one sort of sequence.
Comparator provides multiple sort of sequences.
2)
It provides one method named compareTo().
It provides one method named compare().
3)
It is found in java.lang package.
it is found in java.util package.
4)
If we implement Comparable interface, actual class is modified.
Actual class is not modified.
9- What is the role of load factor in hashing function?
Load factor is a constant whose value can be between 0 to 1. Value of load factor determines when the size of buckets is to be modified. In a hash table performance of insertion & search operation and space utilization are two inversely proportional objectives i.e. if space utilization is maximized performance is decreased because optimization of space utilization results in Hash collisions which degrades performance.
10- Difference between Vector and ArrayList.
Vector – Vector is a legacy collection that represents dynamic array. After the introduction of Collection framework Vector is modified to make it compatible to the Collection framework.
Major difference between Vector and ArrayList is of thread safety.
Vector is thread-safe whereas ArrayList is not.
Vector contains methods of Collection and List interface as well as its own legacy methods.
Apart from iterator a vector provides Enumeration for traversing its elements.
Legacy methods of vector:-
addElement( ) :- is used to add an element to a vector.
                public boolean addElement(Object element);
removeElement( ) :- is used to remove an element from the vector.
                public boolean removeElement (Object element)
capacity( ):- retuns the capacity of the vector. capacity( ) method represents number of elements that can be added to a vector without changing its size.
                public int capacity( );
elements( ):- returns an enumeration for traversing contents of a vector.

                public Enumeration elements( );

2 comments:



  1. The blog or and best that is extremely useful to keep I can share the ideas
    of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
    tanki online | 2048 game|

    ReplyDelete

Popular Posts

Powered by Blogger.

About Me

My photo

JobsAcid is a 'job search engine' to provide finding a job on the internet easier for the user. It has designed as huge selection of job offerings on the same search engine.