程序员正在进行一个项目,必须实现equals方法与所给的hashCode方法协调运行:  public int hashCode() {    return (size.hashCode() + color.hashCode()) * 17;    }    哪一个equals方法支持此目标?() A、 无法确定B、 public boolean equals(Object o) {  Sock s = (Sock) o; return size.equals(s.size);}C、 public bo

题目

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

  • A、 无法确定
  • B、 public boolean equals(Object o) {  Sock s = (Sock) o; return size.equals(s.size);}
  • C、 public boolean equals(Object o) {  Sock s = (Sock) o; return color.equals(s.color);}
  • D、 public boolean equals(Object o) {  Sock s = (Sock) o; return size.equals(s.size) &&color.equals(s.color);  }

相似考题

1.试题六(共 15分)阅读以下说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。【说明】已知类 LinkedList 表示列表类,该类具有四个方法:addElement()、lastElement()、umberOfElement()以及removeLastElement()。四个方法的含义分别为:void addElement(Object): 在列表尾部添加一个对象;Object lastElement(): 返回列表尾部对象;int numberOfElement(): 返回列表中对象个数;void removeLastElement(): 删除列表尾部的对象。现需要借助LinkedList来实现一个Stack栈类, Java代码1和Java代码2分别采用继承和组合的方式实现。【Java代码1】public class Stack extends LinkedList{public void push(Object o){ //压栈addElement(o);}public Object peek(){ //获取栈顶元素return (1) ;}public boolean isEmpty(){ //判断栈是否为空return numberOfElement() == 0;}public Object pop(){ //弹栈Object o = lastElement();(2) ;return o;}}【Java代码2】public class Stack {private (3) ;public Stack(){list = new LinkedList();}public void push(Object o){list.addElement(o);}public Object peek(){//获取栈顶元素return list. (4) ;}public boolean isEmpty(){//判断栈是否为空return list.numberOfElement() == 0;}public Object pop(){ //弹栈Object o = list.lastElement();list.removeLastElement();return o;}}【问题】若类LinkedList新增加了一个公有的方法removeElement(int index),用于删除列表中第index个元素,则在用继承和组合两种实现栈类Stack的方式中,哪种方式下Stack对象可访问方法removeElement(int index)? (5) (A. 继承 B. 组合)

更多“程序员正在进行一个项目,必须实现equals方法与所给的hashCode方法协调运行:  public int hashCode() {    return (size.hashCode() + color.hashCode()) * 17;    }    哪一个equals方法支持此目标?() A、 无法确定B、 public boolean equals(Object o) {  Sock s = (Sock) o; return size.equals(s.size);}C、 public boo”相关问题
  • 第1题:

    程序员正在进行一个项目,必须实现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

  • 第2题:

    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

  • 第3题:

    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

  • 第4题:

    HashSet子类依靠()方法区分重复元素。

    • A、toString()、equals()
    • B、clone()、equals()
    • C、hashCode()、equals()
    • D、getClass()、clone()

    正确答案:C

  • 第5题:

    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);

    正确答案:B,C

  • 第6题:

    public class Score implements Comparable {  private int wins, losses;  public Score(int w, int 1) { wins = w; losses = 1; }  public int getWins() { return wins; }  public int getLosses() { return losses; }  public String toString() {  return “<“ + wins + “,“ + losses + “>”; }  // insert code here  }  Which method will complete this class?() 

    • A、 public int compareTo(Object o) {/*mode 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

  • 第7题:

    public class Key {  private long id1;  private long 1d2;  // class Key methods  }  A programmer is developing a class Key, that will be used as a key in a standard java.util.HashMap. Which two methods should be overridden to assure that Key works correctly as a key?()

    • A、 public int hashCode()
    • B、 public boolean equals(Key k)
    • C、 public int compareTo(Object o)
    • D、 public boolean equals(Object o)
    • E、 public boolean compareTo(Key k)

    正确答案:A,D

  • 第8题:

    多选题
    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
    解析: 暂无解析

  • 第9题:

    多选题
    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);


    正确答案: B,A
    解析: 暂无解析

  • 第10题:

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

     无法确定

    B

     public boolean equals(Object o) {  Sock s = (Sock) o; return size.equals(s.size);}

    C

     public boolean equals(Object o) {  Sock s = (Sock) o; return color.equals(s.color);}

    D

     public boolean equals(Object o) {  Sock s = (Sock) o; return size.equals(s.size) &&color.equals(s.color);  }


    正确答案: A
    解析: 暂无解析

  • 第11题:

    单选题
    public class ClassA{ public int getValue(){ int value=0; boolean setting=true; String title="Hello"; if(value||(setting && title=="Hello")){return 1;} if(value==1&title.equals("Hello")){return 2;} } } And: ClassA a=new ClassA(); a.getValue(); What is the result?()
    A

    1

    B

    2

    C

    Compilation fails.

    D

    The code runs with no output.

    E

    An exception is thrown at runtime.


    正确答案: D
    解析: 暂无解析

  • 第12题:

    单选题
    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;


    正确答案: D
    解析: 暂无解析

  • 第13题:

    Object类中的方法public int hashCode[],在其子类中覆盖该方法时,其方法修饰符可以是( )。

    A.protected

    B.public

    C.private

    D.缺省


    正确答案:B
    所有的类都是Object的子类,如果要覆盖Object的equals方法则必须覆盖hasCode方法,覆盖时的属性是public。

  • 第14题:

    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

  • 第15题:

    import java.util.*;  class KeyMaster {  public int i;  public KeyMaster(int i) { this.i = i; }  public boolean equals(Object o) { return i == ((KeyMaster)o).i; }  public int hashCode() { return i; }  }  public class MapIt {  public static void main(String[] args) {  Set set = new HashSet();  KeyMaster k1 = new KeyMaster(1);  KeyMaster k2 = new KeyMaster(2);  set.add(k1); set.add(k1);  set.add(k2); set.add(k2);  System.out.print(set.size() + “:”);  k2.i = 1;  System.out.print(set.size() + “:”);  set.remove(k1);  System.out.print(set.size() + “:”);  set.remove(k2);  System.out.print(set.size()); }  }  What is the result?() 

    • A、 4:4:2:2
    • B、 4:4:3:2
    • C、 2:2:1:0
    • D、 2:2:0:0
    • E、 2:1:0:0
    • F、 2:2:1:1
    • G、 4:3:2:1

    正确答案:F

  • 第16题:

    String s= "hello";     String t = "hello";  char c[] = {’h’,’e’,’l’,’l’,’o’} ;     Which return true?()   

    • A、 s.equals(t);
    • B、 t.equals(c);
    • C、 s==t;
    • D、 t.equals(new String("hello"));
    • E、 t==c;

    正确答案:A,C,D

  • 第17题:

    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

  • 第18题:

    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

  • 第19题:

    public class ClassA {  public int getValue() {  int value=0;  boolean setting = true;  String title=”Hello”;  (value || (setting && title == “Hello”)) { return 1; }  (value == 1 & title.equals(”Hello”)) { return 2; }  }  } And:  ClassA a = new ClassA();  a.getValue();  What is the result?() 

    • A、 1
    • B、 2
    • C、 Compilation fails.
    • D、 The code runs with no output.
    • E、 An exception is thrown at runtime.

    正确答案:C

  • 第20题:

    单选题
    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.


    正确答案: D
    解析: 暂无解析

  • 第21题:

    单选题
    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
    解析: 暂无解析

  • 第22题:

    多选题
    public class Key {  private long id1;  private long 1d2;  // class Key methods  }  A programmer is developing a class Key, that will be used as a key in a standard java.util.HashMap. Which two methods should be overridden to assure that Key works correctly as a key?()
    A

    public int hashCode()

    B

    public boolean equals(Key k)

    C

    public int compareTo(Object o)

    D

    public boolean equals(Object o)

    E

    public boolean compareTo(Key k)


    正确答案: C,E
    解析: 暂无解析

  • 第23题:

    单选题
    public class ClassA {  public int getValue() {  int value=0;  boolean setting = true;  String title=”Hello”;  (value || (setting && title == “Hello”)) { return 1; }  (value == 1 & title.equals(”Hello”)) { return 2; }  }  } And:  ClassA a = new ClassA();  a.getValue();  What is the result?()
    A

     1

    B

     2

    C

     Compilation fails.

    D

     The code runs with no output.

    E

     An exception is thrown at runtime.


    正确答案: D
    解析: 暂无解析