Wednesday 2 May 2012

JAVA interview questions 36

Q351. What is relation between ListIterator and Iterator? 
ListIterator is child interface of Iterator 

Q352. 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 Cloneable it is implementation class for Set interface 
heterogeneous objects are allowed 
it is introduced in 1.2 version 

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

Q354. What is LinkedHashSet? 
It is the child class of HashSet. The main difference between HashSet and LinkedHashSet is: In the case of HashSet insertion order is not preserved , but in the case of LinkedHashSet insertion will be preserved. 

Q355. What are major enhancements in 1.4 version of collection frame work? 
LinkedHashSet 
LinkedHashMap 
IdentityHashMap 

Q356. Explain about TreeSet? 
It is Collection object which can be used to represent a group of objects according to some sorting order. 
The underlying datastructure 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 

Q357. 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) 

Q358. 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 
o public int compare(Object ,Object) o public boolean equals(Object) 

Q359. What is Entry interface? 
It is inner interface of Map. 
In the Map each key value pair is considered as Entry object. 
interface Map{ 
//more code here 
       interface Entry{ 
           Object getKey() 
               Object getValue() 
                  Object setValue(Object new)
       }


Q360. Explain about HashMap? 
It is a Map Object which can be used used to represent a group of objects as key-value pairs. 
The underlying data structure is Hashtable 
Duplicaes keys are not allowed duplicate values are allowed 
Insertion order is not preserved because insertion is based on hashcode of keys. Heterogeneous objects are allowed for both keys and values 
null key is allowed only once 
null values are allowed multiple times 
Introduced in 1.2 version 

No comments:

Post a Comment