nerdexam
Oracle

1Z0-809 · Question #84

return output; } and the code fragment: Book b1 = new Book (101, "Java Programming"); Book b2 = new Book (102, "Java Programming"); System.out.println (b1.equals(b2)); //line n2 Which statement is…

The correct answer is B. The program prints false. Option B is correct because b1 and b2 are two distinct Book objects with different IDs (101 vs. 102). Even if the Book class overrides equals() to compare by ID, the IDs differ, so the method returns false; and if equals() is not overridden, Java falls back to Object.equals()…

Question

return output; } and the code fragment: Book b1 = new Book (101, "Java Programming"); Book b2 = new Book (102, "Java Programming"); System.out.println (b1.equals(b2)); //line n2 Which statement is true?

Options

  • AThe program prints true.
  • BThe program prints false.
  • CA compilation error occurs. To ensure successful compilation, replace line n1 with: book1.equals (Book b2);
  • DA compilation error occurs. To ensure successful compilation, replace line n2 with: System.out.println (b1.equals((Object) b2));

How the community answered

(58 responses)
  • A
    3% (2)
  • B
    83% (48)
  • C
    10% (6)
  • D
    3% (2)

Explanation

Option B is correct because b1 and b2 are two distinct Book objects with different IDs (101 vs. 102). Even if the Book class overrides equals() to compare by ID, the IDs differ, so the method returns false; and if equals() is not overridden, Java falls back to Object.equals(), which compares memory references - and since b1 and b2 are separate instances, it still returns false.

Option A is wrong because no scenario with two different object references (and different IDs) would produce true. Option C is wrong because b1.equals(b2) is perfectly valid Java syntax - no compilation error occurs; equals() takes a parameter, not a declaration. Option D is wrong for the same reason: casting b2 to Object is unnecessary because equals(Object o) already accepts any object by design, and the code compiles fine as written.

Memory tip: "Unless you @Override equals() to compare fields, Java compares addresses, not contents - two new objects are never == or .equals() by default."

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice