Java OCA OCP Practice Question 1583

Question

What is the result of the following?

ArrayList<Integer> list = new ArrayList<>();
list.add(5);
list.add(5);
list.add(3);
 
TreeSet<Integer> set = new TreeSet<>(list);
System.out.print(set.size());
System.out.print(" " );
System.out.print(set.iterator().next());
  • A. 2 3
  • B. 2 5
  • C. 3 3
  • D. 3 5


A.

Note

First the code creates an ArrayList of three elements.

Then the list is transformed into a TreeSet.

Since sets are not allowed to have duplicates, the set only has two elements.

A TreeSet is sorted, which means that the first element in the TreeSet is 3.

Option A is correct.




PreviousNext

Related