Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard

Question 12

Question ID: UKOCP26465


Given code of Test.java file:


What is the result?

  • A Runtime exception
  • B Compilation error
  • C

    {Rob, OCP}

    {John, OCA}

    {Jack, OCP}

  • D

    {Jack, OCP}

    {John, OCA}

    {Rob, OCP}

Reveal correct answer

Correct answer: C

Explanation

UKOCP26465:

In real-world programming, you will hardly find a bean class implementing Comparator, but it is a legal code. A bean class generally implements a Comparable interface to define natural ordering.


Student class in this case correctly implements Comparator<Student> interface by overriding compare(Student, Student) method. Note, this compare method will sort in descending order of the Student's name.


list.sort(...) accepts an argument of Comparator<Student> type.  new Student() provides the instance of Comparator<Student> type. It sorts the list in descending order of Students' names.


Output is:

{Rob, OCP}

{John, OCA}

{Jack, OCP}

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need