OCJP(310-065)

单选题Given: Which statement is true?()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 o

题目
单选题
Given: Which statement is true?()
A

Compilation fails because the hashCode method is not overridden.

B

A HashSet could contain multiple Person objects with the same name.

C

All Person objects will have the same hash code because the hashCode method is not overridden.

D

If a HashSet contains more than one Person object with name="Fred", then removing another Person,also with name="Fred", will remove them all.

如果没有搜索结果,请直接 联系老师 获取答案。
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第1题:

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;  Person p = (Person)o;  return age == p.age && name.equals(p.name);  }  }  What is the appropriate definition of the hashCode method in class Person?() 

  • A、 return super.hashCode();
  • B、 return name.hashCode() + age * 7;
  • C、 return name.hashCode() + comment.hashCode() /2;
  • D、 return name.hashCode() + comment.hashCode() / 2 - age * 3;

正确答案:B

第2题:

public class Person {  private name;  public Person(String name) {  this.name = name;  }  public int hashCode() {  return 420;  }  }  Which is true?() 

  • A、 The time to find the value from HashMap with a Person key depends on the size of the map.
  • B、 Deleting a Person key from a HashMap will delete all map entries for all keys of typePerson.
  • C、 Inserting a second Person object into a HashSet will cause the first Person object to beremoved as a duplicate.
  • D、 The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map.

正确答案:A

第3题:

Which two are true?()

A.A finalizer may NOT be invoked explicitly.

B.The finalize method declared in class Object takes no action.

C.super.finalize()is called implicitly by any over riding finalize method.

D.The finalize method for a given objec twill be called no more than once by the garbage collector.

E.The order in which finalize will be called on two objects is based on the order in which the two objects became finalizable.


参考答案:B, D

第4题:

Which of the following statements are true?() 

  • A、 The equals() method determines if reference values refer to the same object.
  • B、 The == operator determines if the contents and type of two separate objects match.
  • C、 The equals() method returns true only when the contents of two objects match.
  • D、 The class File overrides equals() to return true if the contents and type of two separate objects        match.

正确答案:A,D

第5题:

Given: String value = getServletContext().getInitParameter("foo"); in an HttpServlet and a web applicationdeployment descriptor that contains: foo frodo Which two are true?()

  • A、The foo initialization parameter CANNOT be set programmatically.
  • B、Compilation fails because getInitParameter returns type Object.
  • C、The foo initialization parameter is NOT a servlet initialization parameter.
  • D、Compilation fails because ServletContext does NOT have a getInitParameter method.
  • E、The foo parameter must be defined within the  element of the deployment descriptor.

正确答案:A,C

第6题:

1. public class Person {  2. private String name;  3. public Person(String name) { this.name = name; }  4. public boolean equals(Person p) {  5. return p.name.equals(this.name);  6. }  7. }  Which is true?() 

  • A、 The equals method does NOT properly override the Object.equals method.
  • B、 Compilation fails because the private attribute p.name cannot be accessed in line 5.
  • C、 To work correctly with hash-based data structures, this class must also implement the hashCode method.
  • D、 When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.

正确答案:A

第7题:

11. public static void main(String[] args) {  12. Object obj = new Object() {  13. public int hashCode() {  14. returns 42; 15. }  16. };  17. System.out.println(obj.hashCode());  18. }    What is the result? () 

  • A、 42
  • B、 An exception is thrown at runtime.
  • C、 Compilation fails because of an error on line 12.
  • D、 Compilation fails because of an error on line 16.
  • E、 Compilation fails because of an error on line 17.

正确答案:A

第8题:

Given:What is the result when method testIfA is invoked?()

A.True

B.Not true

C.An exception is thrown at runtime.

D.Compilation fails because of an error at line 12.

E.Compilation fails because of an error at line 19.


参考答案:A

第9题:

Which two statements are true regarding the return values of property written hashCode and equals methods from two instances of the same class?()

  • A、 If the hashCode values are different, the objects might be equal.
  • B、 If the hashCode values are the same, the object must be equal.
  • C、 If the hashCode values are the same, the objects might be equal.
  • D、 If the hashCode values are different, the objects must be unequal.

正确答案:C,D

第10题:

public class Person {  private 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 is true?() 

  • A、 Compilation fails because the hashCode method is not overridden.
  • B、 A HashSet could contain multiple Person objects with the same name.
  • C、 All Person objects will have the same hash code because the hashCode method is not overridden.
  • D、 If a HashSet contains more than one Person object with name=”Fred”, then removing another person, also with name=”Fred”, will remove them all.

正确答案:B

更多相关问题