Object Superclass

AP Computer Science A· difficulty 3/5

public class P {
    private int x;
    public P(int x) { this.x = x; }
    public boolean equals(Object o) {
        if (!(o instanceof P)) return false;
        return ((P) o).x == this.x;
    }
}
// ...
P a = new P(3);
P b = new P(4);
System.out.println(a.equals(b));

What is printed?

  • A

    false

    check_circle
  • B

    true

  • C

    3

  • D

    4

Explanation

The x values 3 and 4 differ, so equals returns false.

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

Related questions