程序员正在进行一个项目,必须实现equals方法与所给的hashCode方法协调运行: public int hashCode() { return (size.hashCode() + color.hashCode()) * 17; } 哪一个equals方法支持此目标?()
第1题:
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);}
第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?()
第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("== "); } } 结果为:()
第4题:
HashSet子类依靠()方法区分重复元素。
第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 的约定?()
第6题:
public class Score implements Comparable
第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?()
第8题:
If the hashCode values are different, the objects might be equal.
If the hashCode values are the same, the object must be equal.
If the hashCode values are the same, the objects might be equal.
If the hashCode values are different, the objects must be unequal.
第9题:
public int hashCode() { return 343; }
public int hashCode() { return size.hashCode (); }
public int hashCode() { return color.hashCode (); }
public int hashCode() { return (int) (Math.random() * 1000);
第10题:
无法确定
public boolean equals(Object o) { Sock s = (Sock) o; return size.equals(s.size);}
public boolean equals(Object o) { Sock s = (Sock) o; return color.equals(s.color);}
public boolean equals(Object o) { Sock s = (Sock) o; return size.equals(s.size) &&color.equals(s.color); }
第11题:
1
2
Compilation fails.
The code runs with no output.
An exception is thrown at runtime.
第12题:
return super.hashCode();
return name.hashCode() + age * 7;
return name.hashCode() + comment.hashCode() /2;
return name.hashCode() + comment.hashCode() / 2 - age * 3;
第13题:
Object类中的方法public int hashCode[],在其子类中覆盖该方法时,其方法修饰符可以是( )。
A.protected
B.public
C.private
D.缺省
第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?()
第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
第16题:
String s= "hello"; String t = "hello"; char c[] = {’h’,’e’,’l’,’l’,’o’} ; Which return true?()
第17题:
Which two statements are true regarding the return values of property written hashCode and equals methods from two instances of the same class?()
第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?()
第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?()
第20题:
Compilation fails because the hashCode method is not overridden.
A HashSet could contain multiple Person objects with the same name.
All Person objects will have the same hash code because the hashCode method is not overridden.
If a HashSet contains more than one Person object with name=”Fred”, then removing another person, also with name=”Fred”, will remove them all.
第21题:
==
equals
equals ==
无结果输出
第22题:
public int hashCode()
public boolean equals(Key k)
public int compareTo(Object o)
public boolean equals(Object o)
public boolean compareTo(Key k)
第23题:
1
2
Compilation fails.
The code runs with no output.
An exception is thrown at runtime.