Multiple choice technology

Given: 1. import java.util.*; 2. public class Example { 3. public static void main(String[] args) { 4. // insert code here 5. set.add(new Integer(2)); 6. set.add(new Integer(1)); 7. System.out.println(set); 8. } 9. } Which code, inserted at line 4, guarantees that this program will output [1, 2]?

  1. Set set = new TreeSet();

  2. Set set = new HashSet();

  3. Set set = new SortedSet();

  4. List set = new SortedList();

  5. Set set = new LinkedHashSet();

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

TreeSet guarantees sorted order and uses natural ordering for Integers, so it will output [1, 2]. HashSet does not guarantee order. SortedSet is an interface, not a class, and cannot be instantiated. SortedList does not exist. LinkedHashSet maintains insertion order, not sorted order.