Hi Friends,
In this post, I am sharing interview questions for java freshers for round - 2.
Question 1:
Write down the definition of Iterable interface.
Answer:
public interface Iterable{
public Iterator<T> iterator();
}
Question 2:
How does HashSet work?
Answer:
When we create HashSet instance, it internally creates instance of HashMap in its constructor. So when we add a duplicate key, that key is compared in HashMap entries. Actually, it is put in HashMap.
So, when we call add(3), then the value 3 is stored as key in HashMap and some dummy value [new Object] is stored as value.
So, in HashSet add() method is defined as:
public boolean add(E e){
return map.put(e, PRESENT) == null;
}
Note: map.put() returns null, if it adds key-value pair. If key already exists , it returns value.
So, if key is not present in HashMap, HashMap returns null and add() method of HashSet returns true.Else it returns false.
Question 3:
What copy technique is originally used by HashSet clone() method?
Answer:
There are two copy techniques in every object oriented programming language: deep copy and shallow copy.
To create a clone or copy of the Set object, HashSet internally uses shallow copy in clone() method, the elements themselves are not cloned. In other words, a shallow copy is made by copying the reference of the object.
Question 4:
What is the difference between HashSet and TreeSet?
Answer:
There are multiple differences between HashSet and TreeSet which are described below:
- Ordering of the elements
- Null Value: TreeSet doesn't allow null value.
- HashSet implements Set interface while TreeSet implements NavigableTreeSet interface.
- HashSet uses equals() method for comparison while TreeSet uses compareTo() method for comparison.
Note: Underlying data structure of TreeSet is Red-Black Tree which is a BST and thus is sorted. For it to be sorted, it uses comparator. The default comparator is not null safe, that's why TreeSet doesn't allow null value.
So, when we call TreeSet.add(null); , It compiles but at runtime, it throws NullPointerException.
Question 5:
How fail fast iterator come to know that the internal structure is modified?
Answer:
Iterator read internal data structure (object array) directly. The internal data structure (i.e. object array) should not be modified while iterating through the collection.
To ensure this, it maintains an internal flag "mods". Iterator checks the mods flag , whenever it gets the next value (using hasNext() method and next() method). Value of mods flag changes whenever there is an structural modification. Thus indicating iterator to throw ConcurrentModificationException.
Question 6:
What is fail safe iterator?
Answer:
Fail safe iterator makes copy of the internal data structure (object array) and iterates over the copied data structure.
Any structural modification done to the iterator affects the copied data structure. So , original data structure remains structurally unchanged.
Hence, no ConcurrentModificationException is thrown by the fail safe iterator.
Question 7:
What is the difference between Collections and Collection?
Answer:
java.util.Collections is a class which contains static methods only and most of the methods throw NullPointerException if object or class passed to them is null.
java.util.Collection is an interface. Which is the base interface for all other collections like List, Map, Set etc.
Question 8:
What is the difference between Iterator and ListIterator?
Answer:
We can use Iterator to traverse Set and List collections whereas ListIterator can be used with Lists only.
Iterator can traverse in forward direction only whereas ListIterator can be used to traverse in both the directions.
Question 9:
How many ways are there to traverse or loop Map, HashMap , TreeMap in java?
Answer:
We have 4 ways to traverse:
- Take map.keySet() and loop using foreach loop
- Take map.keySet() and loop using Iterator
- Take map.entrySet() and loop using foreach loop
- Take map.entrySet() and loop using iterator.
That's all from this post.
Thanks for reading!!
No comments:
Post a Comment