Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Hard

Question 31

Given code of Test.java file: 

What will be the result of compiling and executing Test class?

  • A Compilation error
  • B

    {Rob, OCP}
    {John, OCA}
    {Jack, OCP}

  • C Runtime exception
  • D

    {Jack, OCP}
    {John, OCA}
    {Rob, OCP}

Reveal correct answer

Correct answer: B

Explanation

In real world programming you will hardly find a bean class implementing Comparator but it is a legal code. A bean class generally implements 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 Student's name.


list.sort(...) accepts an argument of Comparator<Student> type. 

new Student() provides the instance of Comparator<Student> type. It sorts list in descending order of Students' name.


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