Wednesday 19 October 2016

Beginners Level(0-1 yrs ) Interview Question based on Java Collections

What is the root interface in collection hierarchy ?
Root interface in collection hierarchy is Collection interface .

What is the difference between Collection and Collections ?
Collection is  an interface while Collections is a java class , both are present in java.util package and  part of java collections framework.

Which collection classes are synchronized or thread-safe ?
Stack, Properties , Vector and Hashtable can be used in multi threaded environment because they are synchronized classes (or thread-safe).

Name the core Collection  interfaces ?
The list of core collection interfaces are : just mention the important ones
Important : Collection , Set , Queue , List , Map
Maps do not extend this interface, yet they are part of java collection framework
Other interface also in the list :  SortedSet, SortedMap , Deque, ListIterator etc.

What is the difference between List and Set ?
Set contain only unique elements while List can contain duplicate elements.
Set is unordered while List is ordered . List maintains the order in which the objects are added .

What is the difference between Map and Set ?
Map object has unique keys each containing some value, while Set contain only unique values.

What are the classes implementing List and Set interface ?
Class implementing List interface :  ArrayList , Vector , LinkedList ,
Class implementing Set interface :  HashSet , TreeSet

What is an iterator ?
Iterator is an interface . It is found in java.util package. It provides methods to iterate over any Collection.

What is the difference between Iterator and Enumeration ?

The main difference between Iterator and Enumeration is that Iterator has remove() method while Enumeration doesn't.
Hence , using Iterator we can manipulate objects by adding and removing the objects from the collections. Enumeration behaves like a read only interface as it can only traverse the objects and fetch it .

Which design pattern followed by Iterator ?
It follows iterator design pattern. Iterator design pattern provides us to navigate through the collection of objects by using a common interface without letting us know about the underlying implementation.
Enumeration is an example of Iterator design pattern.

Which methods you need to override to use any object as key in HashMap ?
To use any object as key in HashMap , it needs to implement equals() and hashCode() method .

What is the difference between Queue and Stack ?
Queue is a data structure which is based on FIFO ( first in first out ) property . An example of Queue in real world is buying movie tickets in the multiplex or cinema theaters.
Stack is a data structure which is based on LIFO (last in first out) property . An example of Stack in real world is  insertion or removal of CD  from the CD case.

How to reverse the List in Collections ?
There is a built in reverse method in Collections class . reverse(List list) accepts list as parameter.
Collections.reverse(listobject);

How to convert the array of strings into the list ?
Arrays class of java.util package contains the method asList() which accepts the array as parameter.
So,

String[]  wordArray =  {"Hello" , "World" , "from Java"};
List wordList =  Arrays.asList(wordArray);

No comments:

Post a Comment