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.


Correct Option: B

AI Explanation

To determine the result of the given code, let's go through each line and understand what is happening:

Line 11: The get() method is declared with a return type of Collection. Line 12: A new LinkedList object called sorted is created. Line 13: The elements "B", "C", and "A" are added to the sorted collection. Line 14: The sorted collection is returned. Line 16: The main method is declared. Line 17: A for-each loop is used to iterate over the elements of the collection returned by the get() method. Line 18: The current element is printed. Line 19: A comma and a space are printed after each element.

Based on the code, the elements in the sorted collection are added in the order "B", "C", and "A".

Therefore, when the for-each loop in line 17 iterates over the collection, the elements will be printed in the order they were added: "B", "C", and "A".

So, the result of the code will be:

B, C, A,

Therefore, the correct answer is B) B, C, A.

Find more quizzes: