1z0-809 Exam QuestionsBrowse all questions from this exam

1z0-809 Exam - Question 144


Given the code fragments:

and

Which two modifications enable to sort the elements of the emps list? (Choose two.)

Show Answer
Correct Answer: BC

To enable sorting of the elements in the emps list, the Person class must implement the Comparable interface and provide an implementation for the compareTo method. Replacing line n1 with 'class Person implements Comparable<Person>' (Option C) makes the Person class implement Comparable, which provides the compareTo method that Collections.sort requires. Inserting the method 'public int compareTo(Person p) { return this.name.compareTo(p.name); }' at line n2 (Option B) provides the necessary logic for comparing Person objects based on their names. With these modifications, the Collections.sort(emps) call will work correctly, because the Person objects will know how to compare themselves.

Discussion

4 comments
Sign in to comment
Manuel7000
Mar 29, 2021

Agree and tested

samtash1034Options: BC
Jan 6, 2022

b,c,tested

steefaandOptions: BC
Feb 7, 2024

B and C are true.

DarGrinOptions: BC
Jun 2, 2024

Anser is B and C, since class Person should implement Comparable Interface and implement its method : int compareTo(T o)