nerdexam
Oracle

1Z0-851 · Question #164

Given: public class Person { private String name, comment; private int age; public Person(String n, int a, String c) { name = n; age = a; comment = c; } public boolean equals(Object o) { if (! (o…

The correct answer is B. return name.hashCode() + age * 7. See the full explanation below for the reasoning.

Question

Given: public class Person { private String name, comment; private int age; public Person(String n, int a, String c) { name = n; age = a; comment = c; } public boolean equals(Object o) { if (! (o instanceof Person)) return false; 19, Person p = (Person)o; return age == p.age && name.equals(p.name); } } What is the appropriate definition of the hashCode method in class Person?

Options

  • Areturn super.hashCode();
  • Breturn name.hashCode() + age * 7;
  • Creturn name.hashCode() + comment.hashCode() / 2;
  • Dreturn name.hashCode() + comment.hashCode() / 2 - age * 3;

How the community answered

(29 responses)
  • A
    10% (3)
  • B
    83% (24)
  • C
    3% (1)
  • D
    3% (1)

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice