Tuesday 25 November 2014

Top Java Collections interview Questions

1.What is Java Collection Framework?

Ans : Java Collections Framework is also known as JCF. It is a set of classes and Interfaces that implement commonly used data structures like List ,Linked List etc.

2. Explain Collection Framework in detail or what all collections are present in Java Collection Framework ?
Ans : Java Collection Framework architecture consists of many collections Like List,Set ,Map etc . You can find detailed description of this framework here.


3. Why Map interface does not extends Collection interface ?
Ans : Map is a part of Collections framework but does not extend Collections Interface as its behaviour is different from normal Collections like List and set :
The reason that can be tell in an interview that :
a) Map contains key value pair to store data in it while other collections like list and set directly stores elements in them .
b) add() method is not used in map to add an element.

4. What is List interface? What are all its implementations?
Ans : List is an ordered collection of elements .The ordering is index based .List can conatin duplicate elements. The implementation of lIst are :
a) ArrayList
b) Vector
c) Linkedist
d) Stack

5. What is an arrayList?
Ans :ArrayList is an implementation of List which is backed by an array .ArrayList is re-sizable array implementation of List interface and implements all operations of List interface,permits all elements including null .

eg.
List<String> l= new ArrayList<String>();

or

ArrayList<String> l= new ArrayList<String>();

6.Internal Working of an ArrayList 

Ans : Internal working of an ArrayList is very important question with interview perspective .Interviewer asks this question to know how deeply the candidate know about this structure .As you know ArrayList is backed by an array only ,but how this array is defined and how elements are added into this array you can read it in detail here .: 

Internal Working of an ArrayList 


7. Difference between Array and arrayList.
Array
ArrayList
Arrays are fixed in size and cannot be resized dynamically
ArrayList is dynamic and resizablei in nature
Once the array is created elements cannot be added or deleted from it
ArrayList the elements can be added and deleted at runtime.
Can be multidimensional
Can’t  be multidimensional
Array can contain objects of a single data type or class
ArrayList if not used with generic can contain objects of different classes
Example :

Int [] arr={1,2};
Example :

List lis= new ArrayList();
lis.add(1);
lis.add(“John”);

8 .How to sort an array list ?
Ans : To sorting an array list we can use various techniques .

1. Without using any sort method .
2. Using sort method
3. Using Comparator and Comparable

1. To sort an array List without using sort method ,we need to use a sorting algorithm .which can be bubble sort

2. using sort method 

To sort an array List using sort method we will use collections.sort

eg. We have an array List
List l= new ArrayList();
l.add(123);
l.add(80);
l.add(67);
l.add(12);
collections.sort(l);
for (int i :l)
system.out.println(l);

Output : 12
67
80
123

3. Using Comparator and Comparable 
If you want to sort an arrayList<Object > then we need to use comparable and comparator interface .
This is very important question to sort collection using comparable and comparator interface .You can find detailed description here .

9. What is the order of the elements are stored in an array List ?
Ans : The order of the elements stored in arrayList is same they are added to the List .
For arrayList ,LinkedList,Vector  order of the elements stored in arrayList is same they are added to the List .

10.What is difference between Arrays.sort() and collections.sort() ?
Ans : The main difference between arrays.sort() and collections.sort() is that the former is used to sort arrays and latter is used to sort collections like list .You can further read detailed description on difference between Arrays.sort() and collections.sort()  here :


Difference between Arrays.sort() and collections.sort()

11. What is Linked List in Java Collections ?
Ans : Linked List is doubly linked list implementation of list interface .Please don't confuse linked list in collection with linked list in data structure as that one in data structure can be both singly link list or doubly .
Linked List implements all of the methods of list interface and permits all kind elements including null.



No comments:

Post a Comment