🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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);
    }
}
  1. A.[45, 72, 90]
  2. B.[90, 72, 45]
  3. C.[45, 90, 72]
  4. 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 →