问题1. 给出如下函数:
11. public static int sum(List list) {
12. int sum = 0;
13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
14. int i = ((Integer)iter.next()).intValue();
15. sum += i;
16. }
17. return sum;
18. }
请在下列选项中选出三个改动,使得函数可以使用泛型且不会提示泛型未检测警告?
(Choose three.)
A. Remove line 14.
B. Replace line 14 with "int i = iter.next();".
C. Replace line 13 with "for (int i : intList) {".
D. Replace line 13 with "for (Iterator iter : intList) {".
E. Replace the method declaration with "sum(List<int> intList)".
F. Replace the method declaration with "sum(List<Integer> intList)".
问题2:在算法中要求使用java.util.List数据结构,该算法要求可以方便的添加“add”一个元素,但是不要求支持随机快速访问元素,请问你会选择下列的那个类?
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
问题3:
11. // 此处插入代码
12. private N min max;
13. public N getMin() { return min; }
14. public N getMax() { return max; }
15. public void add(N added) {
16. if (min == null )
17. min = added;
18. if (max == null )
19. max = added;
20. }
21. }
下列选项中,哪两个插入第11行位置后可以是的代码完整且正确:
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> {
问题4:
12. import java.util.*;
13. public class Explorer2 {
14. public static void main(String[] args) {
15. TreeSet<Integer> s = new TreeSet<Integer>();
16. TreeSet<Integer> subs = new TreeSet<Integer>();
17. for(int i = 606; i < 613; i++)
18. if(i%2 == 0) s.add(i);
19. subs = (TreeSet)s.subSet(608, true, 611, true);
20. s.add(629);
21. System.out.println(s + " " + subs);
22. }
23. }
以上代码的运行结果是
A. 编译失败.
B. 运行时有异常抛出.
C. [608 610 612 629] [608 610]
D. [608 610 612 629] [608 610 629]
E. [606 608 610 612 629] [608 610]
F. [606 608 610 612 629] [608 610 629]
第五题
1. public class Score implements Comparable<Score> {
2. private int wins losses;
3. public Score(int w int l) { wins = w; losses = l; }
4. public int getWins() { return wins; }
5. public intgetLosses() { return losses; }
6. public String toString() {
7. return "<" + wins + "" + losses + ">";
8. }
9. // insert code here
10. }
下列那个方法可以使得该类完整?
A. public int compareTo(Object o){/*more code here*/}
B. public int compareTo(Score other){/*more code here*/}
C. public int compare(Score s1Score s2){/*more code here*/}
D. public int compare(Object o1Object o2){/*more code here*/} 作者: 闾丘日月 时间: 2012-7-2 03:48
感谢楼主让我知道有OCJP这么个东西。作者: 韦念欣 时间: 2012-7-2 08:25
可以拿来练习练习作者: yulu53 时间: 2012-7-2 09:37
没答案吗?作者: 杨_扬 时间: 2012-7-2 09:54
答案见下期作者: 杨_扬 时间: 2012-7-2 09:55