Java OCA OCP Practice Question 3143

Question

Which code, when inserted at (1), in the equalsImpl() method will provide a correct implementation of the equals() method?.

public class Pair {
  int a, b;//w  ww  .  j  a va  2 s  .  c om
  public Pair(int a, int b) {
    this.a = a;
    this.b = b;
  }

  public boolean equals(Object o) {
    return (this == o) || (o instanceof Pair) && equalsImpl((Pair) o);
  }

  private boolean equalsImpl(Pair o) {
    // (1) INSERT CODE HERE ...
  }
}

Select the three correct answers.

  • (a) return a == o.a || b == o.b;
  • (b) return false;
  • (c) return a >= o.a;
  • (d) return a == o.a;
  • (e) return a == o.a && b == o.b;


(b), (d), and (e)

Note

(a) and (c) fail to satisfy the properties of an equivalence relation.

(a) is not transitive and (c) is not symmetric.




PreviousNext

Related