Exam 1z0-816 All QuestionsBrowse all questions from this exam
Question 20

Given:

You want the code to produce this output:

John -

Joe -

Jane -

Which code fragment should be inserted on line 1 and line 2 to produce the output?

    Correct Answer: B

    To produce the desired output, the Person class needs to implement the Comparator<Person> interface for custom sorting. The compareTo method in option B is correctly written to compare the names of Person objects in reverse order, which is needed to produce the descending order output shown: 'John', 'Joe', and 'Jane'.

Discussion
Sa16253748596Option: B

A,B,C,D are all wrong. B is better than the others. Insert Comparator<Person> on line 1. Correct. Insert - public int compare(Person person) { return person.name.compareTo(this.name); } on line 2. is correct.