nerdexam
Oracle

1Z0-809 · Question #192

Given: class Product { String pname; public Product(String pname) { this.pname = pname; } } and the code fragment: Product p1 = new Product("PowerCharger"); Product p2 = p1…

The correct answer is D. true false. Option D is correct because Product does not override the equals() method, so it inherits Object.equals(), which compares object references (memory addresses), not field values. p2 is assigned directly from p1 - both variables point to the same object in memory - so…

Question

Given: class Product { String pname; public Product(String pname) { this.pname = pname; } } and the code fragment: Product p1 = new Product("PowerCharger"); Product p2 = p1; System.out.println(p1.equals(p2)); Product p3 = new Product("PowerCharger"); System.out.println(p1.equals(p3)); What is the result?

Options

  • Atrue true
  • Bfalse true
  • Cfalse false
  • Dtrue false

How the community answered

(28 responses)
  • A
    4% (1)
  • B
    4% (1)
  • C
    14% (4)
  • D
    79% (22)

Explanation

Option D is correct because Product does not override the equals() method, so it inherits Object.equals(), which compares object references (memory addresses), not field values. p2 is assigned directly from p1 - both variables point to the same object in memory - so p1.equals(p2) returns true. p3 is created with new, producing a distinct object despite having an identical pname, so p1.equals(p3) returns false.

  • A (true/true) is wrong because reference equality cannot return true for two separately constructed objects.
  • B (false/true) and C (false/false) are wrong because p1 and p2 share the same reference, which Object.equals() recognizes as equal.

Memory tip: "No @Override equals = no value comparison." If you don't see equals() overridden in the class, Java falls back to == logic - only the same reference returns true.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice