← Quiz: Nested and Anonymous Classes: all 10 questions
Quiz: Nested and Anonymous Classes · Question 8 of 10
What does this print? (code)
What does this print?
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> marks = new ArrayList<>(List.of(45, 90, 72));
Collections.sort(marks, new Comparator<Integer>() {
public int compare(Integer a, Integer b) { return b - a; }
});
System.out.println(marks);
}
}
- A.[45, 72, 90]
- B.[90, 72, 45]
- C.[45, 90, 72]
- D.Compile error
Answer & explanation
Correct answer: [90, 72, 45]
compare returns b - a, so larger numbers come first: descending order.
Can you get the rest right without peeking?
Take the timed Quiz: Nested and Anonymous Classes quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →