Object Superclass

AP Computer Science A· difficulty 3/5

public class S {
    private String x;
    public S(String x) { this.x = x; }
    public boolean equals(Object o) {
        if (!(o instanceof S)) return false;
        return ((S) o).x.equals(this.x);
    }
}
// ...
S a = new S("hi");
S b = new S("hi");
System.out.println(a.equals(b) + " " + b.equals(a));

What is printed?

  • A

    true true

    check_circle
  • B

    true false

  • C

    false true

  • D

    false false

Explanation

Both objects share the same field value; equals correctly returns true in both directions.

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

Related questions