Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 135

Given:

Which statement will refactor line 2 to use a lambda expression?

    Correct Answer: A

    To refactor the given code to use a lambda expression, and assuming 'fruits' is not a raw type but a List<String>, the correct statement would be: fruits.sort((String d, String e) -> (e.compareTo(d)));. This correctly uses lambda syntax to implement the Comparator's compare method, comparing two strings in reverse order.

Discussion
ASPushkin

there is no right answer new Comparator() -> new Comparator<String>() even if List fruit is a raw type list List.of("banana", "orange", "apple", "lemon"); is an Unmodifiable List so there is java.lang.UnsupportedOperationException on fruits.sort()

aruni_mishraOption: D

Note: "fruits" is Raw use of parameterized class 'List', issue with existing code: the "new Comparator" needs to be typecast as new Comparator<String>(){}, to have a valid override of public int compare(String m, String n) {} after if, we typecast fruit list, below will work: -fruits.sort((m, n) -> n.compareTo(m)); -fruits.sort((String d, String e) -> (e.compareTo(d))); -fruits.sort((a, b) -> { return b.compareTo(a); });

d7bb0b2Option: A

no answer correct list.of is inmutable. if that is corrected then: A and D are correct (correction sintax for ;)

SampsOption: A

A is the most correct but it gives a compilation error as a result of ';'

duydnOption: A

If 1st line is List<String> -> A will be correct answer.