Java Questions & Answers – Collection Algorithms

This section of our 1000+ Java MCQs focuses on collection algorithms of Java Programming Language.

1. Which of these is an incorrect form of using method max() to obtain a maximum element?
a) max(Collection c)
b) max(Collection c, Comparator comp)
c) max(Comparator comp)
d) max(List c)
View Answer

Answer: c
Explanation: Its illegal to call max() only with comparator, we need to give the collection to be searched into.

2. Which of these methods sets every element of a List to a specified object?
a) set()
b) fill()
c) Complete()
d) add()
View Answer

Answer: b
Explanation: None.

3. Which of these methods can randomize all elements in a list?
a) rand()
b) randomize()
c) shuffle()
d) ambiguous()
View Answer

Answer: c
Explanation: shuffle – randomizes all the elements in a list.
advertisement
advertisement

4. Which of these methods can convert an object into a List?
a) SetList()
b) ConvertList()
c) singletonList()
d) CopyList()
View Answer

Answer: c
Explanation: singletonList() returns the object as an immutable List. This is an easy way to convert a single object into a list. This was added by Java 2.0.

5. Which of these is true about unmodifiableCollection() method?
a) unmodifiableCollection() returns a collection that cannot be modified
b) unmodifiableCollection() method is available only for List and Set
c) unmodifiableCollection() is defined in Collection class
d) none of the mentioned
View Answer

Answer: b
Explanation: unmodifiableCollection() is available for al collections, Set, Map, List etc.

6. 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. 	    while(i.hasNext())
  13. 	        System.out.print(i.next() + " ");
  14.         }
  15.     }

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

Answer: a
Explanation: None.
Output:

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

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. 	    while(i.hasNext())
  14. 	        System.out.print(i.next() + " ");
  15.         }
  16.     }

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

Answer: b
Explanation: Collections.reverse(list) reverses the given list, the list was 2->8->5->1 after reversing it became 1->5->8->2.
Output:

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

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.