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
Options
- Atrue true
- Bfalse true
- Cfalse false
- Dtrue false
How the community answered
(28 responses)- A4% (1)
- B4% (1)
- C14% (4)
- D79% (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
truefor two separately constructed objects. - B (false/true) and C (false/false) are wrong because
p1andp2share the same reference, whichObject.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.