250+ TOP MCQs on Collections Interface and Answers

Java MCQs on collection framework of Java Programming Language.

1. Which of these interface declares core method that all collections will have?
a) set
b) EventListner
c) Comparator
d) Collection

Answer: d
Clarification: Collection interfaces defines core methods that all the collections like set, map, arrays etc will have.

2. Which of these interface handle sequences?
a) Set
b) List
c) Comparator
d) Collection

Answer: b
Clarification: None.

3. Which of this interface must contain a unique element?
a) Set
b) List
c) Array
d) Collection

Answer: a
Clarification: Set interface extends collection interface to handle sets, which must contain unique elements.

4. Which of these is a Basic interface that all other interface inherits?
a) Set
b) Array
c) List
d) Collection

Answer: d
Clarification: Collection interface is inherited by all other interfaces like Set, Array, Map etc. It defines core methods that all the collections like set, map, arrays etc will have

5. Which of these is static variable defined in Collections?
a) EMPTY_SET
b) EMPTY_LIST
c) EMPTY_MAP
d) All of the mentioned

Answer: d
Clarification: None.

6. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Array 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             int array[] = new int [5];
  7.             for (int i = 5; i > 0; i--)
  8.                 array[5 - i] = i;
  9.             Arrays.sort(array);
  10.             for (int i = 0; i < 5; ++i)
  11.             	System.out.print(array[i]);;
  12.         }
  13.     }

a) 12345
b) 54321
c) 1234
d) 5432

Answer: a
Clarification: Arrays.sort(array) method sorts the array into 1,2,3,4,5.
Output:

$ javac Array.java
$ java Array
12345

7. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Collection_Algos 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList list = new LinkedList();
  7.             list.add(new Integer(2));
  8.             list.add(new Integer(8));
  9.             list.add(new Integer(5));
  10.             list.add(new Integer(1));
  11.             Iterator i = list.iterator();
  12.             Collections.reverse(list);
  13. 	    Collections.sort(list);
  14.             while(i.hasNext())
  15. 	        System.out.print(i.next() + " ");
  16.         }
  17.     }

a) 2 8 5 1
b) 1 5 8 2
c) 1 2 5 8
d) 2 1 8 5

Answer: c
Clarification: Collections.sort(list) sorts the given list, the list was 2->8->5->1 after sorting it became 1->2->5->8.
Output:

$ javac Collection_Algos.java
$ java Collection_Algos
1 2 5 8

8. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Collection_Algos 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList list = new LinkedList();
  7.             list.add(new Integer(2));
  8.             list.add(new Integer(8));
  9.             list.add(new Integer(5));
  10.             list.add(new Integer(1));
  11.             Iterator i = list.iterator();
  12.             Collections.reverse(list);
  13. 	    Collections.shuffle(list);
  14.             while(i.hasNext())
  15. 	        System.out.print(i.next() + " ");
  16.         }
  17.     }

a) 2 8 5 1
b) 1 5 8 2
c) 1 2 5 8
d) Any random order

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top