Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 221

Given:

public class Emp {

String fName;

String lName;

public Emp (String fn, String ln) {

fName = fn;

lName = ln;

}

public String getfName() { return fName; }

public String getlName() { return lName; }

}

and the code fragment:

List emp = Arrays.asList (

new Emp ("John", "Smith"),

new Emp ("Peter", "Sam"),

new Emp ("Thomas", "Wale"));

emp.stream()

//line n1

.collect(Collectors.toList());

fName and then ascending order of lName?

Which code fragment, when inserted at line n1, sorts the employees list in descending order of

    Correct Answer: A

    To sort the employees list in descending order of fName and then ascending order of lName, the correct method chaining involves using Comparator.comparing for both fields. Using reversed() correctly reverses the order of fName. Therefore, the appropriate code fragment is .sorted(Comparator.comparing(Emp::getfName).reversed().thenComparing(Emp::getlName)). Note that 'reserved()' should indeed be corrected to 'reversed()'.

Discussion
Ritesh_Option: A

Answer is A.there is also a typo – reserved() -> reversed()

steefaandOption: A

A is correct, just fix typo reserved to reversed.