Sorting

AP Computer Science A· difficulty 2/5

ArrayList<String> list = new ArrayList<String>();
list.add("banana");
list.add("apple");
list.add("cherry");
// After Collections.sort(list):
System.out.print(list.get(0));

What is printed?

  • A

    Compile error: String not Comparable

  • B

    banana

  • C

    cherry

  • D

    apple

    check_circle

Explanation

String implements Comparable<String> using lexicographic order. After sorting, "apple" comes first.

Want 10 more like this — adaptive to your weak spots?

Related questions