250+ TOP MCQs on Collection Algorithms and Answers

Java MCQs 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)

Answer: c
Clarification: 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()

Answer: b
Clarification: None.

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

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

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

Answer: c
Clarification: 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

Answer: b
Clarification: 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

Answer: a
Clarification: None.
Output:

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

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

Answer: b
Clarification: 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

contest

Leave a Reply

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