32、一般在覆盖时,要同时覆盖hashCode、equals方法。

题目

32、一般在覆盖时,要同时覆盖hashCode、equals方法。


相似考题
参考答案和解析
MAC Index
更多“32、一般在覆盖时,要同时覆盖hashCode、equals方法。”相关问题
  • 第1题:

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

    A.protected

    B.public

    C.private

    D.缺省


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

  • 第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题:

    下面有关方法覆盖说法不正确的是()。

    • A、方法覆盖要求覆盖和被覆盖的方法有相同的名字,参数列以及返回值
    • B、方法覆盖要求覆盖和被覆盖的方法必须具有相同的访问权限
    • C、覆盖的方法不能比被覆盖的方法抛出更多的异常
    • D、覆盖的方法一定不能是private的

    正确答案:B

  • 第4题:

    在实际的逻辑覆盖测试中,一般以()为主设计测试用例。

    • A、条件覆盖
    • B、判定覆盖
    • C、条件组合覆盖
    • D、路径覆盖

    正确答案:C

  • 第5题:

    覆盖包括的常用覆盖方法有:()、分支覆盖、条件覆盖、判定-条件覆盖、路径覆盖。


    正确答案:语句覆盖

  • 第6题:

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

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

    正确答案:C

  • 第7题:

    实验室灭火的方法要针对起因选用合适的方法。一般小火可用湿布、石棉布或沙子覆盖燃烧物即可灭火。


    正确答案:正确

  • 第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题:

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

  • 第10题:

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


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

  • 第11题:

    问答题
    两个对像值相同(x.equals(y)==true),但却可有不同的hashcode,这句话对不对?

    正确答案: 不对,有相同的hashcode。
    解析: 暂无解析

  • 第12题:

    单选题
    下面有关方法覆盖说法不正确的是()。
    A

    方法覆盖要求覆盖和被覆盖的方法有相同的名字,参数列以及返回值

    B

    方法覆盖要求覆盖和被覆盖的方法必须具有相同的访问权限

    C

    覆盖的方法不能比被覆盖的方法抛出更多的异常

    D

    覆盖的方法一定不能是private的


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

  • 第13题:

    下列有关HashSet集合的描述中,错误的是()

    A.HashSet是Set接口的一个实现类

    B.向HashSet存入对象时,对象一般会重写hashCode ()和equals ()方法

    C.向HashSet存入对象时,对象的equals ()方法一定会被执行

    D.HashSet存储的元素是不可重复的


    答案:C
    解析:当调用HashSet集合的add()方法存入元素时,首先调用当前存入对象的hashCode()方法获得对象的哈希值,然后根据对象的哈希值计算出一个存储位置。如果该位置上没有元素,则直接将元素存入,如果该位置上有元素存在,才会调用equals()方法

  • 第14题:

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

    正确答案:D

  • 第15题:

    如果你设计了一个类,并且覆盖了equals( )方法,哪些方法你还会考虑覆盖()

    • A、clone()
    • B、toString()
    • C、wait()
    • D、finalize()
    • E、hashCode()

    正确答案:E

  • 第16题:

    逻辑覆盖测试包括的常用覆盖方法有:语句覆盖,判定覆盖,条件覆盖,判定-条件覆盖、条件组合覆盖、路径覆盖。


    正确答案:正确

  • 第17题:

    两个对像值相同(x.equals(y)==true),但却可有不同的hashcode,这句话对不对?


    正确答案: 不对,有相同的hashcode。

  • 第18题:

    软件测试的目的是__(1)__。通常__(2)__是在代码编写阶段可进行的测试,它是整个测试工作的基础。逻辑覆盖的标准主要用于__(3)__。它主要包括条件覆盖、条件组合(多重条件)覆盖、判定覆盖、条件及判定覆盖、语句覆盖和路径覆盖等几种,其中除了路覆盖外最弱的覆盖标准是__(4)__,最强的覆盖标准是__(5)__。空白(3)处应选择()

    • A、黑盒测试方法
    • B、白盒测试方法
    • C、灰盒测试方法
    • D、软件验证方法

    正确答案:B

  • 第19题:

    对电子仪器进行保养时要注意防潮,防尘,避免日晒,暂时不用时应覆盖防尘罩放置在干燥通风处,同时要定期通电除潮。


    正确答案:正确

  • 第20题:

    填空题
    覆盖包括的常用覆盖方法有:()、分支覆盖、条件覆盖、判定-条件覆盖、路径覆盖。

    正确答案: 语句覆盖
    解析: 暂无解析

  • 第21题:

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

  • 第22题:

    单选题
    如下关于Java编码描述不正确的是()
    A

    除了构建器外,不要使用和类名相同的方法名

    B

    使用equals()比较两个类的值是否相同

    C

    不要使用嵌套赋值,即在一个表达式中使用多个=

    D

    重载equals()方法时,不必要重载hashCode()方法


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

  • 第23题:

    单选题
    程序员正在进行一个项目,必须实现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
    解析: 暂无解析

  • 第24题:

    单选题
    如果你设计了一个类,并且覆盖了equals( )方法,哪些方法你还会考虑覆盖()
    A

    clone()

    B

    toString()

    C

    wait()

    D

    finalize()

    E

    hashCode()


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