Sorting

AP Computer Science A· difficulty 3/5

public class Person implements Comparable<Person> {
    private String name;
    public Person(String n) { name = n; }
    public int compareTo(Person other) {
        return name.compareTo(other.name);
    }
    public String getName() { return name; }
}
// Usage:
Person a = new Person("Bob");
Person b = new Person("Alice");
System.out.print(a.compareTo(b) > 0);

What is printed?

  • A

    0

  • B

    true

    check_circle
  • C

    false

  • D

    Compile error

Explanation

"Bob".compareTo("Alice") returns a positive int (B comes after A). So the expression is true.

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

Related questions

AP Computer Science A · Sorting Practice Question | Acemy