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
Explanation:

To solve this question, the user must understand the syntax and behavior of the given Java code. In this code, a method named get() is defined that returns a Collection of type LinkedList containing three strings: "B", "C", and "A". The main method then iterates through each element in the Collection returned by get() and prints its value followed by a comma and a space.

Going through each option:

A. A, B, C,: This option is incorrect because the order of the strings in the Collection returned by get() is "B", "C", "A". Therefore, the output of the program will be "B, C, A,".

B. B, C, A,: This option is correct. As explained above, the Collection returned by get() contains the strings "B", "C", and "A", in that order. The main method iterates through each element in the Collection and prints its value, resulting in the output "B, C, A,".

C. Compilation fails.: This option is incorrect because there are no syntax errors or other issues in the code that would cause it to fail to compile.

D. The code runs with no output.: This option is incorrect because the main method prints output to the console, as described above.

E. An exception is thrown at runtime.: This option is incorrect because there are no errors or exceptional conditions in the code that would cause it to throw an exception.

Therefore, the correct answer is:

The Answer is: B. B, C, A,

Find more quizzes: