Sorting

AP Computer Science A· difficulty 2/5

public class Score implements Comparable<Score> {
    private int v;
    public Score(int x) { v = x; }
    public boolean compareTo(Score o) { return v < o.v; }
}

What occurs?

  • A

    Compiles with warning

  • B

    Runtime error

  • C

    Compiles fine

  • D

    Compile error: compareTo must return int

    check_circle

Explanation

Comparable<T> requires int compareTo(T). Returning boolean does not satisfy the interface, so the class fails to compile.

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

Related questions