本帖最后由 张凯 于 2012-7-13 17:55 编辑
第一题:(答案F)
Given the following method signatures from ArrayList:
boolean add(E e)
protected void removeRange(int fromIndexInclusive, int toIndexExclusive)
int size()
and given:
Java代码
1.2. import java.util.*;
2.3. public class MyUtil extends ArrayList {
3.4. public static void main(String[] args) {
4.5. MyUtil m = new MyUtil();
5.6. m.add("w"); m.add("x"); m.add("y"); m.add("z");
6.7. m.removeRange(1,3);
7.8. System.out.print(m.size() + " ");
8.9. MyUtil m2 = new MyUtil2().go();
9.10. System.out.println(m2.size());
10.11. }
11.12. }
12.13. class MyUtil2 {
13.14. MyUtil go() {
14.15. MyUtil m2 = new MyUtil();
15.16. m2.add("1"); m2.add("2"); m2.add("3");
16.17. m2.removeRange(1,2);
17.18. return m2;
18.19. } }
What is the result?
A. 1 1
B. 1 2
C. 2 1
D. 2 2
E. An exception is thrown at runtime.
F. Compilation fails due to a single error.
G. Compilation fails due to multiple errors.
第二题:(答案:F)
Given:
Java代码
1.1. public class Hose <E extends Hose> {
2.2. E innerE;
3.3. public static E doStuff(E e, Hose<E> e2) {
4.4. // insert code here
5.5. }
6.6. public E getE() {
7.7. return innerE;
8.8. } }
Which can be inserted, independently at line 4, for the code to compile? (Choose all that apply.)
A. return e;
B. return e.getE();
C. return e2;
D. return e2.getE();
E. return new Hose().getE();
F. Compilation fails regardless of which return is inserted.
请大家帮忙解释下原因,多谢啦!
--------------------------------------------------------------------------------
问题补充:第一题:removeRange是protected方法没错,但是在MyUtil2中调用这个方法的还是MyUtil的对象呀。MyUtil m2 = new MyUtil(); .......; m2.removeRange(1,2); 这跟在MyUtil自己的main方法中调用不是一样的吗?
比如这样:
class A {
protected void foo() {}
}
class B extends A {
}
class C {
void bar() {
B b = new B();
b.foo(); // 这里也是在子类之外,却是可以调用的。
}
}
第2题:
如果去掉static的话,答案是AD,好费解哦,为什么是AD呢?
还请大家多多帮忙! |