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: The List interface handles sequences as it allows ordered collections of elements. Lists can contain duplicate elements and provide precise control over the position of each element. On the other hand, Set does not allow duplicates and does not guarantee order, Comparator is used for sorting and comparing, and Collection is a more general interface that doesn’t specifically handle sequencing.

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

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: The Collections class has only these three static variables.

  • EMPTY_LIST => The empty list (immutable).
  • EMPTY_MAP => The empty map (immutable).
  • EMPTY_SET => The empty set (immutable).
Free 30-Day C++ Certification Bootcamp is Live. Join 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:

$ 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.

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.