Java OCA OCP Practice Question 3140

Question

Consider the following program:

import java.util.*;
import java.util.concurrent.*;

public class Main {
        public static void main(String []args) {
               List list = Arrays.asList(10, 5, 10, 20);   // LINE A
               System.out.println(list);
               System.out.println(new HashSet(list));
               System.out.println(new TreeSet(list));
               System.out.println(new ConcurrentSkipListSet(list));
        }/*  w ww .j a  v  a  2s . c o m*/
}

Which one of the following options correctly describes the behavior of this program?

a) The program prints//from   w ww . j  a  va  2 s .c om
     [10, 5, 10, 20]
     [20, 5, 10]
     [5, 10, 20]
     [5, 10, 20]
b) The program prints
     [10, 5, 10, 20]
     [5, 10, 20]
     [5, 10, 20]
     [20, 5, 10]
c) The program prints
     [5, 10, 20]
     [5, 10, 20]
     [5, 10, 20]
     [5, 10, 20]
d) The program prints
     [10, 5, 10, 20]
     [20, 5, 10]
     [5, 10, 20]
     [20, 5, 10]
e) The program prints
     [10, 5, 10, 20]
     [5, 10, 10, 20]
     [5, 10, 20]
     [10, 5, 10, 20]
f) Compiler error in line marked by the comment LINE A since  List is not parameterized with the type <Integer>.


a)

Note

Here is the description of the containers that explain the output:

  • List is unsorted.
  • HashSet is unsorted and retains unique elements.
  • TreeSet is sorted and retains unique elements.
  • ConcurrentSkipListSet is sorted and retains unique elements.



PreviousNext

Related