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
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
Explanation: None.
3. Which of these methods can randomize all elements in a list?
a) rand()
b) randomize()
c) shuffle()
d) ambiguous()
View Answer
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
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
Explanation: unmodifiableCollection() is available for al collections, Set, Map, List etc.
6. What will be the output of the following Java program?
import java.util.*;
class Collection_Algos
{
public static void main(String args[])
{
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
a) 2 8 5 1
b) 1 5 8 2
c) 2
d) 2 1 8 5
View Answer
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?
import java.util.*;
class Collection_Algos
{
public static void main(String args[])
{
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
Collections.reverse(list);
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
a) 2 8 5 1
b) 1 5 8 2
c) 2
d) 2 1 8 5
View Answer
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]
- Apply for Java Internship
- Check Programming Books
- Practice Programming MCQs
- Apply for Computer Science Internship
- Practice Information Technology MCQs