QUESTION 71
Given:
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? (choose two)
A. public int hashCode ( )
B. public Boolean equals (Key k)
C. public int compareTo (Object o)
D. public boolean equals (Object o)
Answer: ( A, D )
HashMap中的key不能重复,因此作为key的对象类必须实现hashCode和equals方法。
QUESTION 72 ---
Given the exhibit:
Which two, inserted at line 2 will allow the code to compile? (Choose Two)
A. public class MinMax< ? > {
B. public class MinMax < ? extends Number> {
C. public class MinMax <N extends Object> {
D. public class MinMax <N extends Number > {
E. public class MinMax < ? extends Object > {
F. public class MinMax < N extends Integer > {
Answer: ( D, F )
所有数字系的原始数据封装类都继承或者实现了它们的公共父类java.lang.Number类(抽象类)中定义的如下的6个数字取值方法:
public abstract int intValue();
public abstract long longValue();
public abstract float floatValue();
public abstract double doubleValue();
public byte byteValue() {
return (byte)intValue();
}
public short shortValue() {
return (short)intValue();
}
N是泛型类别变量,它相当于一个类型。
A 在类定义的时候声明泛型是不能用?这个万用字符的。
B 同上
C Object没有doubleValue()方法。
D OK.
E 同A。
F OK.
QUESTION 73
Given the exhibit:
enum Example { ONE, TWO, THREE }
Which statement is true?
A. The expressions (ONE = = ONE) and ONE.equals (ONE) are both guaranteed to be true.
B. The expression (ONE < TWO ) is guaranteed to be true and ONE.compareTo (TWO) is guaranteed to be less than one.
C. The Example values cannot be used in a raw java.util.HashMap; instead, the programmer must use a java.util.EnumMap.
D. The Example values can be used in a java.util.SortedSet, but the set will NOT be sorted because enumerated Type do NOT IMPLEMENT java.lang.Comparable.
Answer: ( A )
A 正确
B 不能用大于号和小于号比较枚举。
C 错误 枚举类型的元素可以用在HASHMAP里作为键或者值。
HashMap<Integer, Example> h = new HashMap<Integer, Example>();
h.put(1, Example.ONE);
D 错误,枚举类型的元素值是可以放入SortedSet里的例如TreeSet. 把枚举类型的元素转成字符串在放入其中,字符串就已经实现comparable接口。可以自然排序。
SortedSet<Example> s = new TreeSet<Example>();
s.add(Example.TWO);
s.add(Example.ONE);
System.out.println(s);
打印出结果[ONE, TWO]
QUESTION 74
Given the exhibit:
Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for garbage collection?
A. Line 16
B. Line 17
C. Line 18
D. Line 19
E. The object is NOT a candidate for garbage collection.
Answer: ( D )
intObj一直会被numbers引用,直到19行之后
QUESTION 75
Given:
And the command line invocation:
Java Yippee2 a b c
What is the result?
A. a b
B. b c
C. a b c
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: ( B )