← Quiz: Pagination and Sorting: all 10 questions
Quiz: Pagination and Sorting · Question 7 of 10
What does this print? (code)
What does this print?
import java.util.*;
public class Main {
record Course(int id, int fee) {}
public static void main(String[] args) {
List<Course> list = new ArrayList<>(List.of(
new Course(3, 500), new Course(1, 900), new Course(2, 500), new Course(4, 900)));
list.sort(Comparator.comparingInt(Course::fee).reversed().thenComparingInt(Course::id));
List<Integer> ids = new ArrayList<>();
for (Course c : list) ids.add(c.id());
System.out.println(ids);
}
}
- A.[3, 1, 2, 4]
- B.[1, 4, 2, 3]
- C.[4, 1, 3, 2]
- D.[2, 3, 1, 4]
Answer & explanation
Correct answer: [1, 4, 2, 3]
Highest fee first (900: ids 1, 4), then 500 (ids 2, 3); the id tie-breaker fixes the order inside each fee.
Can you get the rest right without peeking?
Take the timed Quiz: Pagination and Sorting quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →