Oracle
1Z0-809 · Question #240
Given the code fragments: class Person // line n1 { String name; Person(String name) { this.name = name; } } // line n2 and List<Person> emps = new ArrayList<>(); // code that adds objects of the…
The correct answer is B. At line n2 insert public int compareTo (Person p) { return this.name.compareTo (p.name); } C. Replace line n1 with class Person implements Comparable<Person>. You've hit your session limit · resets 5:20pm (America/New_York)
Question
Given the code fragments:
class Person // line n1
{
String name;
Person(String name) {
this.name = name;
}
} // line n2
and
List<Person> emps = new ArrayList<>();
// code that adds objects of the Person class to the emps list goes here */
Collections.sort(emps);
Which two modifications enable to sort the elements of the emps list? (Choose two.)
Options
- AReplace line n1 with class Person extends Comparator<Person>
- BAt line n2 insert public int compareTo (Person p) { return this.name.compareTo (p.name); }
- CReplace line n1 with class Person implements Comparable<Person>
- DAt line n2 insert public int compare (Person p1, Person p2) { return p1.name.compareTo (p2.name); }
- EAt line n2 insert public int compareTo (Person p, Person p2) { return p1.name.compareTo (p2.name); }
- FReplace line n1 with class Person implements Comparator<Person>
How the community answered
(40 responses)- A8% (3)
- B70% (28)
- D15% (6)
- E5% (2)
- F3% (1)
Explanation
You've hit your session limit · resets 5:20pm (America/New_York)
Community Discussion
No community discussion yet for this question.