Q5. What is the difference between an
Interface and an Abstract class?
A. An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behaviour and all methods are implicitly abstract.
Q6. Explain different way of using thread?
A. The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance, the only interface can help.
Q7. What is an Iterator?
A. Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn.
Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.
A. An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behaviour and all methods are implicitly abstract.
Q6. Explain different way of using thread?
A. The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance, the only interface can help.
Q7. What is an Iterator?
A. Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn.
Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.
Q8. What
is difference between iterator access and index access?
A. Index based access allows access of the
element directly on the basis of index. The cursor of the data structure can
directly goto the 'n' location and get the element. It does not traverse
through n-1 elements.
In Iterator based access, the cursor has to
traverse through each element to get the desired element. So to reach the 'n'th
element it need to traverse through n-1 elements.
Insertion, updation or deletion will be faster
for iterator based access if the operations are performed on elements present
in between the data structure.
Insertion, updation or deletion will be faster
for index based access if the operations are performed on elements present at
last of the data structure.
Traversal or search in index based data structure
is faster.
ArrayList is index access and LinkedList is
iterator access.
Q9. Is it
better to have a HashMap with large number of records or n number of small
hashMaps?
A. It depends on the different scenario
one is working on:
1. If the objects in the hashMap are same then there is no point in having different hashmap as the traverse time in a hashmap is invariant to the size of the Map.
2. If the objects are of different type like one of Person class , other of Animal class etc then also one can have single hashmap but different hashmap would score over it as it would have better readability.
1. If the objects in the hashMap are same then there is no point in having different hashmap as the traverse time in a hashmap is invariant to the size of the Map.
2. If the objects are of different type like one of Person class , other of Animal class etc then also one can have single hashmap but different hashmap would score over it as it would have better readability.
Q10. Why is
it preferred to declare: List<String> list = new
ArrayList<String>(); instead of ArrayList<String> = new
ArrayList<String>();
A. It is preferred because:
1. If later on code needs to be changed from
ArrayList to Vector then only at the declaration place we can do that.
2. The most important one – If a function is
declared such that it takes list. E.g void showDetails(List list);
When the parameter is declared as List to the
function it can be called by passing any subclass of List like ArrayList, Vector,
LinkedList making the function more flexible.
Q11. Can a
null element added to a Treeset or HashSet?
A. A null element can be added only if the set
contains one element because when a second element is added then as per set definition
a check is made to check duplicate value and comparison with null element will
throw NullPointerException.
HashSet is based on HashMap and can contain null element.
HashSet is based on HashMap and can contain null element.
Q12. What
are the classes implementing List interface?
A.There are three classes that implement List interface:
1) ArrayList : It is a resizable array implementation. The size of the ArrayList can be increased dynamically also operations like add,remove and get can be formed once the object is created. It also ensures that the data is retrieved in the manner it was stored. The ArrayList is not thread-safe.
2) Vector: It is thread-safe implementation of ArrayList. The methods are wrapped around a synchronized block.
3) LinkedList: the LinkedList also implements Queue interface and provide FIFO(First In First Out) operation for add operation. It is faster if than ArrayList if it performs insertion and deletion of elements from the middle of a list.
1) ArrayList : It is a resizable array implementation. The size of the ArrayList can be increased dynamically also operations like add,remove and get can be formed once the object is created. It also ensures that the data is retrieved in the manner it was stored. The ArrayList is not thread-safe.
2) Vector: It is thread-safe implementation of ArrayList. The methods are wrapped around a synchronized block.
3) LinkedList: the LinkedList also implements Queue interface and provide FIFO(First In First Out) operation for add operation. It is faster if than ArrayList if it performs insertion and deletion of elements from the middle of a list.
| Prev | Next>> |