Java Questions & Answers – Collections Interface

This section of our 1000+ Java MCQs focuses 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
Explanation: None.

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

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

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

Answer: d
Explanation: 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
View Answer

Answer: d
Explanation: None.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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
View Answer

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

advertisement
$ javac Array.java
$ java Array
12345

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

advertisement
  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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: shuffle – randomizes all the elements in a list.
Output:

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

(output will be different on your system)

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.