LearnFrenzy
Sign in Take a free test

Classes and Objects · Predict the Output · Question 3

equals() without an override Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links