🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Counting Sort and Custom Sorting: all 13 questions

Quiz: Counting Sort and Custom Sorting · Question 12 of 13

Which Java comparator safely sorts an Integer[] from smallest to largest, even with very large values?

Which Java comparator safely sorts an Integer[] from smallest to largest, even with very large values?

  1. A.(a, b) -> a - b
  2. B.(a, b) -> Integer.compare(a, b)
  3. C.(a, b) -> b - a
  4. D.(a, b) -> a > b ? 1 : 0
Answer & explanation

Correct answer: (a, b) -> Integer.compare(a, b)

a - b can overflow, and a version that never returns a negative number breaks the comparator contract.

Can you get the rest right without peeking?

Take the timed Quiz: Counting Sort and Custom Sorting quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →