Java OCA OCP Practice Question 3278

Question

Consider the following program:

import java.util.*;

public class Main {
     public static void main(String []args) {
             Set<Integer> set = new TreeSet<Integer>();
             set.add(5);/*ww w.j av a2  s  . com*/
             set.add(10);
             set.add(3);
             set.add(5);
             System.out.println(set);
     }
}

What will be the output of this program?

  • a) [5, 10, 3, 5]
  • b) [5, 10, 3]
  • c) [3, 5, 10]
  • d) [10, 5, 3]


c)

Note

TreeSet is a sorted set; hence, all the inserted items are sorted in ascending order.

Also, since TreeSet is a Set, it will remove any duplicate item inserted.




PreviousNext

Related