11. public void addStrings(List list) {
12. list.add(”foo”);
13. list.add(”bar”);
14. }
What must you change in this method to compile without warnings?()
A. add this code after line 11: list = (List
B. change lines 12 and 13 to: list.add
C. change the method signature on line 11 to: public void addStrings(List< extends String> list) {
D. change the method signature on line 11 to: public void addStrings(List< super String> list) {
E. No changes are necessary. This method compiles without warnings.
您可能感兴趣的试卷
你可能感兴趣的试题
11. public static void append(List list) { list.add(”0042”); }
12. public static void main(String[] args) {
13. List
14. append(intList);
15. System.out.println(intList.get(0));
16. }
What is the result?()
A. 42
B. 0042
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
E. Compilation fails because of an error in line 14.
1. import java.util.*;
2. public class Test {
3. public static void main(String[] args) {
4. List
5. // insert code here
6. }
7. }
Which four, inserted at line 5, will allow compilation to succeed?()
A. String s = strings.get(0);
B. Iterator i1 = strings.iterator();
C. String[] array1 = strings.toArray();
D. Iterator
E. String[] array2 = strings.toArray(new String[1]);
F. Iterator
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
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
A. The hashCode method for a given class can be used to test for object equality and object inequality for that class.
B. The hashCode method is used by the java.util.SortedSet collection class to order theelements within that set.
C. The hashCode method for a given class can be used to test for object inequality, but NOT object equality, for that class.
D. The only important characteristic of the values returned by a hashCode method is that the distribution of values must follow a Gaussian distribution.
E. The hashCode method is used by the java.util.HashSet collection class to group the elements within that set into hash buckets for swift retrieval.
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.
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 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;
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
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*/}
最新试题
What is the result?()
Which code, inserted at line 4, guarantees that this program will output [1, 2]?()
Which statement is true?()
Given: foo and bar are public references available to many other threads, foo refers to a Thread and bar is an Object. The thread foo is currently executing bar.wait(). From another thread, what provides the most reliable way to ensure that foo will stop executing wait()?()
What is the result?()
Which statement is true?()
Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5?()
What is the result?()
What is the result?()
What is the appropriate definition of the hashCode method in class Person?()