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

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.

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

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:

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

advertisement

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn