==
equals
equals ==
无结果输出
第1题:
下列程序的运行结果为______。 class A { int b=0; } public class ex35 public static void main(String args[]) { ex35 t=new ex35(); t.method(); } void method() { A A1=new A(); A A2=new A(); A1,b=A2.b=12; boolean b=A1.equals(A2); Syatem.out.println(b); } }
A.true
B.false
C.0
D.1
第2题:
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?()
第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题:
以下哪些方法在Object类中定义()。
第5题:
public class X { public static void main (String[]args) { String s1 = new String (“true”); Boolean b1 = new Boolean (true); if (s2.equals(b1)) { System.out.printIn(“Equal”); } } } What is the result?()
第6题:
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?()
第7题:
The code will fail to compile.
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.
Class c has three constructors.
Objects of class b cannot be constructed.
At most one of the constructors of each class is called as a result of constructing an object of class c.
第8题:
return super.hashCode();
return name.hashCode() + age * 7;
return name.hashCode() + comment.hashCode() /2;
return name.hashCode() + comment.hashCode() / 2 - age * 3;
第9题:
==
equals
equals ==
无结果输出
第10题:
The program runs and prints nothing.
The program runs and prints “Equal”
An error at line 5 causes compilation to fail.
The program runs but aborts with an exception.
第11题:
The program runs and prints nothing.
The program runs and prints “Equal”
An error at line 5 causes compilation to fail.
The program runs but aborts with an exception.
第12题:
public int hashCode()
public boolean equals(Key k)
public int compareTo(Object o)
public boolean equals(Object o)
public boolean compareTo(Key k)
第13题:
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?()
第14题:
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) {} }
第15题:
程序员正在进行一个项目,必须实现equals方法与所给的hashCode方法协调运行:() public int hashCode() { return (size.hashCode() + color.hashCode()) * 17; } 哪一个equals方法支持此目标?()
第16题:
class Sock { String size; String color; public boolean equals(Object o) { Sock s = (Sock) o; return color.equals(s.color); } // insert code here } 哪两个满足 hashCode 的约定?()
第17题:
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?()
第18题:
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.
第19题:
The equals method does NOT properly override the Object.equals method.
Compilation fails because the private attribute p.name cannot be accessed in line 5.
To work correctly with hash-based data structures, this class must also implement the hashCode method.
When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.
第20题:
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);
第21题:
0
1
2
3
第22题:
1
2
Compilation fails.
The code runs with no output.
An exception is thrown at runtime.
第23题:
0
1
2
3
第24题:
无法确定
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); }