Multiple choice technology programming languages

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

The method returns a LinkedList, which maintains insertion order. Elements are added in the order B, C, A, so they iterate in that same order. The method name 'get' and variable name 'sorted' are misleading - there's no actual sorting happening. LinkedList preserves insertion order, not sorted order.