class Sock {  String size;  String color;  public boolean equals(Object o) {  Sock s = (Sock) o;  return color.equals(s.color);   }  // insert code here  }  哪两个满足 hashCode 的约定?()A、public int hashCode() { return 343; }B、public int hashCode() { return size.

题目

class Sock {  String size;  String color;  public boolean equals(Object o) {  Sock s = (Sock) o;  return color.equals(s.color);   }  // insert code here  }  哪两个满足 hashCode 的约定?()

  • A、public int hashCode() { return 343; }
  • B、public int hashCode() { return size.hashCode (); }
  • C、public int hashCode() { return color.hashCode (); }
  • D、public int hashCode() { return (int) (Math.random() * 1000);
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

Given:Which method will complete this class?()

A.public int compareTo(Object o){/*more code here*/}

B.public int compareTo(Score other){/*more code here*/}

C.public int compare(Score s1,Score s2){/*more code here*/}

D.public int compare(Object o1,Object o2){/*more code here*/}


参考答案:B

第2题:

Given: 3.public class MyTagHandler extends TagSupport { 4.public int doStartTag() { 5.// insert code here 6.// return an int 7.} 8.// more code here ... 18.} There is a single attribute foo in the session scope. Which three code fragments,inserted independently atline 5,return the value of the attribute?()

  • A、Object o = pageContext.getAttribute("foo");
  • B、Object o = pageContext.findAttribute("foo");
  • C、Object o = pageContext.getAttribute("foo",PageContext.SESSION_SCOPE);
  • D、HttpSession s = pageContext.getSession();Object o = s.getAttribute("foo");

正确答案:B,C,D

第3题:

classSock{Stringsize;Stringcolor;publicbooleanequals(Objecto){Socks=(Sock)o;returncolor.equals(s.color);}//insertcodehere}哪两个满足hashCode的约定?()

A.publicinthashCode(){return343;}

B.publicinthashCode(){returnsize.hashCode();}

C.publicinthashCode(){returncolor.hashCode();}

D.publicinthashCode(){return(int)(Math.random()*1000);


参考答案:B, C

第4题:

Given the following code, which method declarations, when inserted at the indicated position, will not cause the program to fail compilation?()   public class Qdd1f {   public long sum(long a, long b) {  return a + b;  }   // insert new method declaration here  }  

  • A、public int sum(int a, int b) { return a + b; }
  • B、public int sum(long a, long b) { return 0; }
  • C、abstract int sum();
  • D、private long sum(long a, long b) { return a + b; }
  • E、public long sum(long a, int b) { return a + b; }

正确答案:A,E

第5题:

class Sock2 {   String color;   public boolean equals(Object o) {   return color.equals(((Sock2)o).color);   } }   class TestSocks {   public static void main(String [] args) {   Sock2 s1 = new Sock2(); s1.color = "blue";   Sock2 s2 = new Sock2(); s2.color = "blue";   if (s1.equals(s2)) System.out.print("equals ");   if (s1 == s2) System.out.print("== ");   }   }   结果为:()      

  • A、==
  • B、equals
  • C、equals ==
  • D、无结果输出

正确答案:B

第6题:

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

第7题:

Which statements concerning the following code are true?()   class a {   public a() {}   public a(int i) { this(); }   }   class b extends a {   public boolean b(String msg) { return false; }   }   class c extends b  {  private c() { super(); }   public c(String msg) { this(); }   public c(int i) {}   }  

  • A、The code will fail to compile.
  • B、The constructor in a that takes an int as an argument will never be called as a result of constructing an     object of class b or c.
  • C、Class c has three constructors.
  • D、Objects of class b cannot be constructed.
  • E、At most one of the constructors of each class is called as a result of constructing an object of class c.

正确答案:B,C

第8题:

程序员正在进行一个项目,必须实现equals方法与所给的hashCode方法协调运行:()publicinthashCode(){return(size.hashCode()+color.hashCode())*17;}哪一个equals方法支持此目标?()

A.无法确定

B.publicbooleanequals(Objecto){Socks=(Sock)o;returnsize.equals(s.size);}

C.publicbooleanequals(Objecto){Socks=(Sock)o;returncolor.equals(s.color);}

D.publicbooleanequals(Objecto){Socks=(Sock)o;returnsize.equals(s.size)&&color.equals(s.color);}


参考答案:D

第9题:

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

第10题:

10. interface Foo { int bar(); }  11. public class Sprite {  12. public int fubar( Foo foo) { return foo.bar(); }  13. public void testFoo() {  14. fubar(  15. // insert code here  16.);  17. }  18. }  Which code, inserted at line 15, allows the class Sprite to compile?() 

  • A、 Foo { public int bar() { return 1; } }
  • B、 new Foo { public int bar() { return 1; } }
  • C、 newFoo() { public int bar(){return 1; } }
  • D、 new class Foo { public int bar() { return 1; } }

正确答案:C

更多相关问题