fbpx
Select Page

Collection Framework Interview Question and Answers

 

1) Why the collection framework is needed?

The purpose of the collection is used to store, retrieve, manipulate, sort, format, search, traverse and communicate aggregate data.

      • Reduces programming effort.
      • Increases program speed and quality.
      • Allows interoperability among unrelated APIs.
      • Reduces effort to design new APIs.
      • Fosters software reuse.

2) What is the difference between using an Object Array and using the Collection framework?

Object Array

3) Why make the collection generic?

  • Generic ensures type safety for a particular collection type.
  • Generic is also use to avoid typecasting.

 

4) Difference between List and Set?

List and Set

 

5) Difference between ArrayList and LinkedList?

ArrayList vs LinkedList

 

6) Difference between Iterator and Enumerator?

Iterator and Enumerator

 

7) Difference between HashSet and Treeset?

Hashset and Treeset

 

8) Difference between ArrayList and Vector?

ArrayList and Vector

 

 

9) What are the properties of Map?

  • maps keys to values
  • cannot contain duplicate keys
  • key maps to almost one value

10)  What are the different views of the Map?

entrySet ():-collection of key and values both.

keySet ():-collection of keys only.

values ():-collection of only values.

11) Difference between Hashtable and HashMap?

HashTable

 

12) Difference between comparable and comparator interfaces?

comparable and comparator interfaces

 

13) List the methods of the Collections class?

  • sort: sorts List elements using a merge sort algorithm, which provides a fast, stable sort.
  • shuffle: randomly permutes the elements in a List.
  • reverse: reverses the order of the elements in a List in backward directions.
  • rotate: rotates all the elements in a list according to the specified distance.
  • swap: swaps the elements with another element in a List at a specified position.
  • replaceAll: It replaces all occurrences of one specified value with another occurrence.
  • fill: overwrites every element in a List with the specified value.
  • copy: copies the entire source List into the destination List.
  • binarySearch: In an ordered List, it searches for an element using the binary search algorithm.
  • indexOfSubList: it returns the index of the first sublist of one List that is equal to another.
  • lastIndexOfSubList: it returns the index of the last sublist of one List that is equal to another.

14) What are the different ways of iterating elements?

  • Using for Loop:

for (int i=0; i<list.size (); i++)

{

System.out.println (list. get (i));

}

 

  • Using for Each:

 

for (Object o: list)

{

System.out.println (o);

}

 

  • Using Iterator:

 

Iterator it=list.iterator ();

while(it.hasNext())

{

System.out.println (it.next ());

}

15) Write a Program Print out all distinct elements from input.

public class HashSetDemo 

{

public static void main (String [] args) 

{

Scanner sc=new Scanner (System. in);

List<String> list=new ArrayList<String> ();

System.out.println (“Enter String: “);

for (int i=0; i<15;i++)

{

list. add (sc.next ());

}

System.out.println (“List with duplicates:”+list);

Set<String> set=new HashSet<String> (list);

System.out.println (“List without duplicates and unordered”+set);

}

16) Write a program to group students by passing or failing grades.

public class HashMapDemo 

{

public static void main (String[] args) 

{

Scanner sc=new Scanner (System. in);

String grade;

Map<String, Integer> stdlist=new HashMap<String, Integer>();

int passp=40;

Map<String, String> gradelist=new HashMap<String, String>();

System.out.println (“Enter Student ID and percetange rerpectively”);

for (int i=0;i<5;i++) 

{

stdlist.put (sc.next (), sc.nextInt ());

}

System.out.println (stdlist);

Iterator<Entry<String, Integer>> it=stdlist.entrySet ().iterator ();

while (it.hasNext ())

{

Entry<String, Integer> mp=it. next ();

String key=mp.getKey ();

System.out.println (key);

int val=mp.getValue ();

System.out.println (val);

if(val>=passp)

grade=”pass”;

else

grade=”fail”;

gradelist.put (key, grade);

}

System.out.println (gradelist);

sc.close ();

}

}

17) Difference between Iterator and ListIterator?

Iterator_ListIterator

 

18) List Set Interface Bulk Operations?

Suppose s1 and s2 are two separate Sets.

  • s1.containsAll (s2) — returns true if s2 has all the elements which are present in s1. 
  • s1.addAll (s2) — adds all the elements of s2 into s1.
  • s1.retainAll (s2) —adds all the elements of s2 into s1 but only common elements. 
  • s1.removeAll (s2) — removes all the elements from s1 set which are present in s2 set.

19) What are the properties of Deque?

  • It is a double-ended queue
  • It does insertion and removal from both ends
  • It behaves like a stack and queue at the same time
  • It also allows duplicate values.

20) List the methods of List Interface?

  • add (Object o)

This method inserts a value i.e. object to the ArrayList.

 Eg:-obj.add (“hello”);

  • add (int index, Object o)

It will add the value to the array list at the specified index.

obj.add (2, “bye”);

  • remove (Object o)

It removes the element from the ArrayList.

obj.remove (“Chaitanya”);

  • remove (int index)

removes element from a given index.

obj.remove (3);

  • set (int index, Object o):

 It will update an element at the specified index with the object given.

obj.set (2, “Tom”);

  • int indexOf (Object o)

It returns the index of the object. If the element is not present in the list then this method returns the value -1.

int pos = obj.indexOf (“Tom”);

  • Object get (int index)

It returns the value or object of the list which is present at the specified index.

E.g.:-String str= obj.get (2);

  • int size ()

It returns the size of the ArrayList i.e. total number of elements in the list.

int number of items = obj.size ();

  • boolean contains (Object o)

It verifies whether the given object o is present in the array list if it is present then it returns true else it returns false.

E.g.:-obj.contains (“Steve”);

  • clear (): 

It is used for removing all the elements of the list in one go. The below code will remove all the elements of list.

obj.clear ();

Author:-
Pallavi Kadam | Software Trainer
SevenMentor Pvt Ltd.

 

Call the Trainer and Book your free demo Class for JAVA now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.

 

 

Pin It on Pinterest

× How can I help you?