Collection Framework interview questions and answers in java

In this current page we will come to know about collection framework interview questions with answers. It may possible to ask questions out of these questions. But, these questions and answers will definitely helps you to answer easily collection framework questions. If any of you are facing more then these interview questions please share with me through "Contact me here" gadget or drop me an email to "admin@javatbrains.com". 
  
1. What is a Collection Framework?
A: A Collection framework is a class library to handle groups of objects. Collection framework is implemented in java.util package.

2. Does a collection object store copies of other objects or their references?
A: A Collection object stores references of other objects.

3. What are the major interfaces in java.util package?
A: List: A List represents a group of elements arranged just  like an array. A List will grow dynamically when the elements are stored into it. List stores a group of elements. But list allow duplicate values to be stored.

    Set: Set are likes List.  A Set will not allow duplicate elements. If we try to pass the same element that is already  available in the Set, then it is not stored into that.

    Maps: Maps store elements in the form of Key and value pairs. If the key is provided then its corresponding value can be obtained. The Key should have unique values.

    Queues: A Queue represents arrangement of elements in FIFO(First In First Out) order. This means that an element that is stored as a first element into the queue will be removed first from the queue.

4. Can you store primitive data types into a collection?
A: No, Collections stores only objects.

5. How you retrieve elements from Collections?
A: Four ways to retrieve any elements from a collection object.
  • Using for-each loop.
  • Using Iterator interface.
  • Using ListIterator interface.
  • Using Enumeration interface.
6. What is for-each loop?
A: For-each loop is like for loop which repeatedly executes a group of statements for each element of the collection. The format is:

         for(variable : collection-Object){
          Statement;
      }

If collection object has n elements the loop is executed exactly n times and the variable stores each element in each step.

7. What is Iterator interface?
A: Iterator is an interface that contains methods to retrieve the elements one by one from a collection object. It has 3 methods.
  • boolean hasNext(): This method returns true if the iterator has more elements.
  • element next(): This method returns the next element in the iterator.
  • void remove():  The method removes from the collection the last element returned by the iterator.
8. What is ListIterator interface?
A: ListIterator is an interface that contains methods to retrieve the elements from a collection object, both in forward and reverse directions. It has the following important methods.
  • boolean hasNext(): This returns true if the ListIterator has more elements when traversing the list in the forward direction.
  • boolean hasPrevious(): This returns true if the ListIterator has more elements  when traversing the list in the reverse direction.
  • element next(): This returns the next element in the list.
  • element previous(): This returns the previous element in the list.
  • void remove(): This removes from the last element that was returned by the next() or previous() methods. 
9. What is Enumeration interface?
A: This interface is useful to retrieve one by one the elements like the iterator. It has 2 methods:
  • boolean hasMoreElements(): This methods tests if the Enumeration has any more elements or not.
  • element nextElement(): This returns the next element that is available in Enumeration.
10. What is the deference between Iterator and ListIterator?
A: Both are useful to retrieve elements from a collection.
     Iterator can retrieve the elements only forward direction. But, ListIterator can retrieve the elements in forward and backward direction also. 
      We can use Iterator to traverse Set and List and also Map type of Objects. But List Iterator can be used to traverse for List type Objects, but not for Set type of Objects.

11. What is the difference between Iterator and Enumeration?
A: Both are useful to retrieve elements from a collection. Iterator has methods whose names are easy to follow and Enumeration methods are difficult to remember. Also Iterator has an option to remove elements from the collection which is not available in Enumeration. So, Iterator is preferred to Enumeration.

12. What are the main implementation of the List interface?
A: The main implementation of the List interface are as follows:
  • ArrayList : Dynamically increasing array implementation of the List interface. The best all-around implementation of the List interface.
  • Vector : Synchronized dynamically growing array implementation of the List interface with additional "Legacy methods".
  • LinkedList: Doubly-Linked list implementation of the List interface. May provide better performance than the ArrayList implementation if elements are frequently inserted or deleted with in the List. Useful for Queues and double-ended Queues.
13. What are the advantages of ArrayList over Array?
A: Some of the advantages ArrayList has over arrays are:
  • It can grown dynamically
  • It provides more powerful insertion and search mechanism than arrays.
14. How to obtain array from ArrayList?
A: Array can be obtained from an ArrayList using toArray() method on ArrayList.

           List arrayList = new ArrayList();
      arrayList.add();

      Object a[] = arrayList.toArray();

15. What is auto-boxing?
A: Converting a primitive data type into an object  from automatically is called auto boxing. Auto boxing is done in generic types.
16. What is the deference between a Stack and LinkedList?
A: 1. A Stack is generally used for the purpose of evaluation of expressions. A LinkedList is used to store and retrieve data.
2. Insertions and deletion of elements only from the top of the stack is possible. Insertion and deletion of elements from any where is possible in case of a LinkedList.

17. What are the differences between ArrayList and Vector?
A: ArrayList:
  1. ArrayList object is not synchronized by default.
  2. In case of single thread ArrayList is faster that Vector.
  3. ArrayList increases their size every time 50%(Half).
    Vector: 
  1. Vector object is synchronized by default.
  2. In case of multiple threads, using Vector is advisable. With a single thread, Vector becomes slow.
18. Can you synchronize the ArrayList object?
A: Yes, we can use synchronizedList() method to synchronize the ArrayList, as

         Collections.synchronizedList(new ArrayList()); 

19. What is the load - factor of a HashMap or HashTable?
A: 0.75

20. What is the default capacity of HashMap?
A: 16

21. What is the use of load factor in HashMap?
A. The default initial capacity of the HashMap  as 16 and the load factor as 0.75. Load factor represents at what level the HashMap capacity should be doubled. The product of capacity and load factor is 16 * 0.75 =12. This represents that after storing the 12th key-value pair into the HashMap, its capacity will become 32.

22. What is the differences between HashMap and HashTable?
A: HashMap:
  1. HashMap object is not synchronized by default.
  2. In case of single threaded using HashMap is faster than HashTable.
  3. HashMap allows null key's and null values to be stored.
  4. Iterator in the HashMap is fail-fast.This means Iterator will produce exception if concurrent updates are made to the HashMap. 
    HashTable: 
  1. HashTable object is synchronized by default.
  2. In case of multiple threads, using HashTable is advisable. With a single thread HashTable becomes slow.
  3. HashTable does not allows null keys or values.
  4. Enumeration for the HashTable is not fail-fast. This means even if concurrent updations are done to HashTable, there will not be any incorrect results produced by the Enumeration. 
23. Can you make HashMap synchronized?
A: Yes, we can make HashMap object synchronized using synchronizedMap() method as shown here:

       Collections.synchronizedMap(new HashMap());

24. What is the difference between a Set and List?
A: List: 
  1. A List represents ordered collection of elements. List preserves the order of elements in which they are entered.
  2. List will allow duplicate values.
  3. Accessing elements by index is possible in lists.
  4. Lists allow null elements to be stored.
    Set:
  1. A Set represents a collection of elements. Order of the elements may change in the set.
  2. Set will not allow duplicate values to be stored.
  3. Accessing elements by their index is not possible in case of Sets.
  4. Sets will not allow null elements.
25. What is TreeSet?
A: TreeSet is a Set implementation that keeps the elements in sorted order. The elements are sorted according to the order of elements or by the comparator provided at creation time.

26. What are the different collection views that Maps provided?
A: Maps provided 3 Collection views.
  1. Key Set
  2. Values Collection
  3. Entry Set
27. What is a Key Set?
A: Key Set is a Set returned by the KeySet() method of the Map interface, it is a set that contains all the Keys present in the Map.

28. What is a Values Collection?
A: Values Collection view is a collection returned by the values() method of the Map Interface, It contains all the objects present as values in the map.

29. What is an Entry Set view?
A: Entry Set view is Set that returned by the entrySet() method in the map and contains Objects of type Map. Entry each of which has both Key and Values.

Comments

Popular posts from this blog

how to count the page views by using JSP

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Multithreading in java with example