🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Comparable vs Comparator: all 10 questions

Quiz: Comparable vs Comparator · 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> a = new ArrayList<>(List.of(3, 10, 7));
        a.sort(Comparator.reverseOrder());
        System.out.println(a);
    }
}
  1. A.[3, 7, 10]
  2. B.[10, 7, 3]
  3. C.[7, 10, 3]
  4. D.[3, 10, 7]
Answer & explanation

Correct answer: [10, 7, 3]

reverseOrder flips the natural (numeric) order, so the largest comes first.

Can you get the rest right without peeking?

Take the timed Quiz: Comparable vs Comparator quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →