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

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


参考解析

解析: 暂无解析

相关考题:

试题六(共 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. 组合)

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

classSock2{Stringcolor;publicbooleanequals(Objecto){returncolor.equals(((Sock2)o).color);}}classTestSocks{publicstaticvoidmain(String[]args){Sock2s1=newSock2();s1.color=blue;Sock2s2=newSock2();s2.color=blue;if(s1.equals(s2))System.out.print(equals);if(s1==s2)System.out.print(==);}}结果为:()A.==B.equalsC.equals==D.无结果输出

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

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;

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.

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、equalsC、equals ==D、无结果输出

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:2B、 4:4:3:2C、 2:2:1:0D、 2:2:0:0E、 2:1:0:0F、 2:2:1:1G、 4:3:2:1

HashSet子类依靠()方法区分重复元素。A、toString()、equals()B、clone()、equals()C、hashCode()、equals()D、getClass()、clone()

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

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

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.

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*/}

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.

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)

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、 1B、 2C、 Compilation fails.D、 The code runs with no output.E、 An exception is thrown at runtime.

单选题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:2B 4:4:3:2C 2:2:1:0D 2:2:0:0E 2:1:0:0F 2:2:1:1G 4:3:2:1

多选题Which two statements are true regarding the return values of property written hashCode and equals methods from two instances of the same class?()AIf the hashCode values are different, the objects might be equal.BIf the hashCode values are the same, the object must be equal.CIf the hashCode values are the same, the objects might be equal.DIf the hashCode values are different, the objects must be unequal.

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

多选题class Sock {  String size;  String color;  public boolean equals(Object o) {  Sock s = (Sock) o;  return color.equals(s.color);   }  // insert code here  }  哪两个满足 hashCode 的约定?()Apublic int hashCode() { return 343; }Bpublic int hashCode() { return size.hashCode (); }Cpublic int hashCode() { return color.hashCode (); }Dpublic int hashCode() { return (int) (Math.random() * 1000);

单选题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==BequalsCequals ==D无结果输出

单选题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 “”; }  // 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*/}

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

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

多选题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?()Apublic int hashCode()Bpublic boolean equals(Key k)Cpublic int compareTo(Object o)Dpublic boolean equals(Object o)Epublic boolean compareTo(Key k)

单选题public class ClassA{ public int getValue(){ int value=0; boolean setting=true; String title="Hello"; if(value||(setting title=="Hello")){return 1;} if(value==1title.equals("Hello")){return 2;} } } And: ClassA a=new ClassA(); a.getValue(); What is the result?()A1B2CCompilation fails.DThe code runs with no output.EAn exception is thrown at runtime.

单选题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 1B 2C Compilation fails.D The code runs with no output.E An exception is thrown at runtime.

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