Sorting

AP Computer Science A· difficulty 2/5

String a = "apple";
String b = "banana";
int r = a.compareTo(b);

What is true about r?

  • A

    r is 1 always

  • B

    r == 0

  • C

    r > 0

  • D

    r < 0

    check_circle

Explanation

"apple" precedes "banana" lexicographically, so compareTo returns a negative value.

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

Related questions