Tuesday 1 May 2012

JAVA interview questions 35

Q341. 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 resizable or growable array. Insertion order is preserved 
Duplicates are allowed 
Heterogeneous objects are allowed 
null insertion is possible 
This class implements RandomAccess , Serializable , Cloneable interfaces 
Best choice for retrieval purpose and worst if our frequent operation is insertion or deletion in the middle 

Q342. What is RandomAccess Interface? 
If a collection class implements RandomAccess interface then we can access any of its element with the same speed. 
RandomAccess interface is marker interface and it dosent contains any methods. ArrayList and vector classes implements this interface. 

Q343. 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 implemetation class for List interface Introduced in 1.2 version 
Underlying data Structure is DoubleLinkedList Allows duplicates 
Insertion order is preserved 
Allows heterogeneous objects 
null insertion is possible 
LinkedList class implements Seriallizable and Cloneable interface but not RandomAccess interface 
Best choice if frequent operation is insertion or deletion an objects in middle but worst choice if frequent operation is retrieval. 

Q344. 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 growable 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 RandomAccess ,Serializable,Cloneable 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. 

Q345. How we can get synchronized version of ArrayList? 
Collections class contains synchronizedList() method for this 
Public static List synchronizedList(List l) 
EX 
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) 

Q346. What is difference between size and capacity of a Collection Object? 
size means number of objects present where as capacity means no of objects it can accommodate. 

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

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

Q349. What is difference between enum and Enumeration? 
An enum can be used to define a group of named constants .It has introduced in 1.5 version Ex 
Class Beer{
KO,KF,RC,FO
}
Enumeration is cursor to retrieve Objects one by one from Collection objects.

Q350. What is difference between Iterator and ListIterator? 
o ListIterator is the child interface of the Iterator 
o Iterator is the single direction cursor where as ListIterator is bidirectional cursor. o 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. 
o Iterator is applicable for every Collecton implemented class object but 
ListIterator is applicable only for List implemented class objects. 
o Iterator can be get by using iterator() of Collection interface where as ListIterator 
can be get by using listIterator() method of List interface 
o both are introduced in 1.2 version

No comments:

Post a Comment