Question 3
Back to Exercise
What is the output of the following Java program?
class Point {
int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
}
public class Main {
public static void main(String[] args) {
Point p = new Point(1, 2), q = new Point(1, 2), r = p;
System.out.println((p == q) + " " + p.equals(q) + " " + (p == r) + " " + p.equals(r));
}
}
- A false false true true Correct
- B false true true true
- C true true true true
- D false false false true
Correct Answer: Option A
Without an override, equals is Object.equals, which compares references.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!