Java Multiple Choice Questions & Answers on Collection Algorithms for Freshers

1. Which of these is an incorrect form of using method max() to obtain maximum element?

a) max(Collection c)
b) max(Collection c, Comparator comp)
c) max(Comparator comp)
d) max(List c)
Answer: c

Explanation: Its illegal to call max() only with comparator, we need to give the collection to be serched 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
3. Which of these methods can randomize all elements in a list?

a) rand()
b) randomize()
c) shuffle()
d) ambigous()
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()
Answer: c

Explanation: singletonList() returns the object as an immutable List. This is a 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

Explanation: unmodifiableCollection() is available for al collections, Set, Map, List etc.
6. Which of these is static variable defined in Collections?

a) EMPTY_SET
b) EMPTY_LIST
c) EMPTY_MAP
d) All of the mentioned
Answer: d
7. What is the output of this program?
  1.     import java.util.*;
  2.     class Collection_Algos {
  3.         public static void main(String args[]) {
  4.             LinkedList list = new LinkedList();
  5.             list.add(new Integer(2));
  6.             list.add(new Integer(8));
  7.             list.add(new Integer(5));
  8.             list.add(new Integer(1));
  9.             Iterator i = list.iterator();
  10.      while(i.hasNext())
  11.          System.out.print(i.next() + " ");
  12.         }
  13.     }
a) 2 8 5 1
b) 1 5 8 2
c) 2
d) 2 1 8 5
Answer: a
8. What is the output of this program?
  1.     import java.util.*;
  2.     class Collection_Algos {
  3.         public static void main(String args[]) {
  4.             LinkedList list = new LinkedList();
  5.             list.add(new Integer(2));
  6.             list.add(new Integer(8));
  7.             list.add(new Integer(5));
  8.             list.add(new Integer(1));
  9.             Iterator i = list.iterator();
  10.             Collections.reverse(list);
  11.      while(i.hasNext())
  12.          System.out.print(i.next() + " ");
  13.         }
  14.     }
a) 2 8 5 1
b) 1 5 8 2
c) 2
d) 2 1 8 5
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.
9. What is the output of this program?
  1.     import java.util.*;
  2.     class Collection_Algos {
  3.         public static void main(String args[]) {
  4.             LinkedList list = new LinkedList();
  5.             list.add(new Integer(2));
  6.             list.add(new Integer(8));
  7.             list.add(new Integer(5));
  8.             list.add(new Integer(1));
  9.             Iterator i = list.iterator();
  10.             Collections.reverse(list);
  11.      Collections.sort(list);
  12.             while(i.hasNext())
  13.          System.out.print(i.next() + " ");
  14.         }
  15.     }
a) 2 8 5 1
b) 1 5 8 2
c) 1 2 5 8
d) 2 1 8 5
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.
10. What is the output of this program?
  1.     import java.util.*;
  2.     class Collection_Algos {
  3.         public static void main(String args[]) {
  4.             LinkedList list = new LinkedList();
  5.             list.add(new Integer(2));
  6.             list.add(new Integer(8));
  7.             list.add(new Integer(5));
  8.             list.add(new Integer(1));
  9.             Iterator i = list.iterator();
  10.             Collections.reverse(list);
  11.      Collections.shuffle(list);
  12.             while(i.hasNext())
  13.          System.out.print(i.next() + " ");
  14.         }
  15.     }
a) 2 8 5 1
b) 1 5 8 2
c) 1 2 5 8
d) Any random order
Answer: d

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

Related

Multiple Choice Questions 2040545064119516718

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item