1z0-819 Exam QuestionsBrowse all questions from this exam

1z0-819 Exam - Question 27


Which code fragment represents a valid Comparator implementation?

Show Answer
Correct Answer: A,C

To implement a valid Comparator in Java, you must override the compare method from the Comparator interface, which should take two arguments and return an int. Option C correctly implements the compare method, taking two strings and using the compareTo method to return a comparison result. This makes Option C a valid implementation of a Comparator. Option A incorrectly tries to use the compareTo method directly within the Comparator and Option D has an incorrect return type and method signature for a Comparator.

Discussion

6 comments
Sign in to comment
StavokOption: C
Jun 3, 2023

Correct answer is C . //A is invalid because :The type new Comparator<String>(){} must implement the inherited abstract method Comparator<String>.compare(String, String) //B is invalid because : The type Comps must implement the inherited abstract method Comparator.compare(Object, Object) //D is invalid because : The return type is incompatible with Comparator.compare(Object, Object) and Illegal modifier for the local class Comps; only abstract or final is permitted

TADIEWAOption: C
Dec 23, 2022

c is correct

pikosssOption: C
Mar 21, 2023

Tested

Mukes877Option: C
May 31, 2023

C is correct

d7bb0b2Option: C
Dec 9, 2023

c , Comparator has method compare no compareTo, can used in method compare

gnilapon.ebenezaireOption: C
Jul 17, 2024

Comparator use compare and String implement Comparable who use compareTo so C is correct