Multiple choice technology

Given: 11. public static Collection get() { 12. Collection sorted = new LinkedList(); 13. sorted.add("B"); sorted.add("C"); sorted.add("A"); 14. return sorted; 15. } 16. public static void main(String[] args) { 17. for (Object obj: get()) { 18. System.out.print(obj + ", "); 19. } 20. } What is the result?

  1. A, B, C,

  2. B, C, A,

  3. Compilation fails.

  4. The code runs with no output

  5. An exception is thrown at runtime.

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

LinkedList maintains insertion order. Elements B, C, A are added in that order, so iteration returns 'B, C, A, '. The for-each loop iterates in the order elements were inserted.