nerdexam
Oracle

1Z0-851 · Question #216

Given: public class Person { private String name; public Person(String name) { this.name = name; } public boolean equals(Object o) { if ( ! ( o instanceof Person) ) return false; Person p = (Person)…

The correct answer is B. A HashSet could contain multiple Person objects with the same name. See the full explanation below for the reasoning.

Question

Given: public class Person { private String name; public Person(String name) { this.name = name; } public boolean equals(Object o) { if ( ! ( o instanceof Person) ) return false; Person p = (Person) o; return p.name.equals(this.name); } } Which statement is true?

Options

  • ACompilation fails because the hashCode method is not overridden.
  • BA HashSet could contain multiple Person objects with the same name.
  • CAll Person objects will have the same hash code because the hashCode method is not overridden.
  • DIf a HashSet contains more than one Person object with name="Fred", then removing another Person,

How the community answered

(25 responses)
  • A
    8% (2)
  • B
    72% (18)
  • C
    4% (1)
  • D
    16% (4)

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice