Object Superclass

AP Computer Science A· difficulty 3/5

public class Point {
    int x, y;
    public Point(int x, int y) { this.x = x; this.y = y; }
}
Point p1 = new Point(1, 2);
Point p2 = new Point(1, 2);
System.out.println(p1.equals(p2));

What is printed?

  • A

    NullPointerException

  • B

    Compile-time error

  • C

    true

  • D

    false

    check_circle

Explanation

Without overriding <code>equals</code>, the inherited Object.equals does reference comparison. p1 and p2 are distinct objects.

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

Related questions