1Z0-808 · Question #34
public class Product { int id; String name; public Product (int id, String name) { this.id = id; this.name = name; } } And given the code fragment: 4. Product p1 = new Product (101, "Pen"); 5…
The correct answer is C. false:true. Option C is correct because p1 == p2 compares object references, not content - p1 and p2 point to two separate Product objects in memory (created on lines 4 and 5), so == returns false. However, p1.name.equals(p2.name) compares the String values "Pen" and "Pen", which are…
Question
Options
- Atrue:true
- Btrue:false
- Cfalse:true
- Dfalse:false
How the community answered
(29 responses)- A14% (4)
- B7% (2)
- C76% (22)
- D3% (1)
Explanation
Option C is correct because p1 == p2 compares object references, not content - p1 and p2 point to two separate Product objects in memory (created on lines 4 and 5), so == returns false. However, p1.name.equals(p2.name) compares the String values "Pen" and "Pen", which are identical, so .equals() returns true, giving output false:true.
Why distractors fail:
- A (true:true) -
p1 == p2can't betruebecause they are distinct objects; onlyp1 == p3would betrue(same reference, line 6). - B (true:false) - wrong on both sides;
==isfalseand.equals()istrue. - D (false:false) - gets
==right but incorrectly assumes.equals()on two identical Strings returnsfalse.
Memory tip: Think of == as asking "same house?" (same memory address) and .equals() as asking "same contents?" - two houses can look identical inside while still being different houses.
Topics
Community Discussion
No community discussion yet for this question.