不看OCJP考题你永远不知道自己的JAVA基础有多差(二)

上期答案

问题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)".

答案 A C F

知识点:

1. 在JAVA的泛型中不能使用基本数据类型

2. 使用迭代器遍历容器

遍历容器的三种方式

List<String> userlist = newArrayList<String>();

userlist.add(“aa");

userlist.add(“bb");

userlist.add(“cc");

//使用普通for循环

for(int i=0; i<userlist.size(); i++){ }

//使用Iterator迭代器

Iterator it = userlist.iterator();

while(it.hasNext()){ }

for (;it.hasNext();it.next() ){ }

//使用增强for循环

for(String s : userlist){ }

问题2:在算法中要求使用java.util.List数据结构,该算法要求可以方便的添加“add”一个元素,但是不要求支持随机快速访问元素,请问你会选择下列的那个类?

A.java.util.Queue

B.java.util.ArrayList

C.java.util.LinearList

D.java.util.LinkedList

答案:D

知识点:

接口List与Queue的异同

ArrayList与LinkedList的异同

Queue接口与List、Set同一级别,都是继承了Collection接口。

java.util.Queue接口,用以支持队列的常见操作。

Queue使用时要尽量避免Collection的add()和remove()方法,而是要使用offer()来加入元素,使用poll()来获取并移出元素。它们的优 点是通过返回值可以判断成功与否,add()和remove()方法在失败的时候会抛出异常。如果要使用前端而不移出该元素,使用element()或者peek()方法

ArrayList与LinkedList都是List接口的实现类

ArrayList实现了List接口,它是以数组的方式来实现的,数组的特性是可以使用索引的方式来快速定位对象的位置,因此对于快速的随机取得对象的需求,使用ArrayList实现执行效率上会比较好。

LinkedList是采用链表的方式来实现List接口的,采用链表实现的。

LinearList 双向链表,JAVA中没有这个类

问题3:

11. // 此处插入代码

12. private N min max;

13. publicN getMin() { return min; }

14. publicN getMax() { return max; }

15. publicvoid 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> {

答案: D F

知识点:

在泛型中通配符“?”的用法

泛型的上下限概念

问题4:

12. import java.util.*;

13. public class Explorer2 {

14. publicstatic 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]

Answer: E

知识点:

请参考JAVA帮助文档中关于Set接口部分的说明

public NavigableSet<E> subSet(EfromElement,

booleanfromInclusive,

EtoElement,

booleantoInclusive)

fromElement – 返回 set 的低端点

fromInclusive – 如果低端点要包含在返回的视图中,则为 true

toElement – 返回 set 的高端点

toInclusive – 如果高端点要包含在返回的视图中,则为 true

第五题

1. public class Score implements Comparable<Score>{

2. privateint wins losses;

3. publicScore(int w int l) { wins = w; losses = l; }

4. publicint getWins() { return wins; }

5. publicintgetLosses() { return losses; }

6. publicString toString() {

7. return"<" + wins + "" + losses + ">";

8. }

9. // insertcode here

10. }

下列那个方法可以使得该类完整?

A. public int compareTo(Object o){/*more codehere*/}

B. public int compareTo(Score other){/*more codehere*/}

C. public int compare(Score s1Score s2){/*more codehere*/}

D. public int compare(Object o1Object o2){/*morecode here*/}

知识点:

Comparable接口的实现

————————————————–分割线————————————————

本期新题

第六题

11. public class Person {

12. privateString name;

13. publicPerson(String name) {

14. this.name = name;

15. }

16. public int hashCode() {

17. return 420;

18. }

19. }

下列哪个叙述是正确的:

A.在HashMap查找某个Person键值所需要的时间依赖于Map的大小。

B.执行删除一个Person键的操作,将删除HashMap中所有类型为Person的键

C.在HashSet中插入第二个Person对象,,将导致第一个Person对象被移除。

D. 在HashSet中查找一个Person对象是否存在的时间是一个常数,不依赖于HashSet的大小

第七题

5. import java.util.*;

6. public class SortOf {

7.public static void main(String[] args) {

8. ArrayList<Integer> a = newArrayList<Integer>();

9. a.add(1); a.add(5); a.add(3);

11. Collections.sort(a);

12. a.add(2);

13. Collections.reverse(a);

14. System.out.println(a);

15.}

16. }

该程序片段的运行结果是?

A. [1 2 3 5]

B. [2 1 35]

C. [2 5 31]

D. [5 3 21]

E. [1 3 52]

F. 编译失败

G. 运行时有异常抛出

第八题

Given

11. public interface Status {

12./* 此处插入代码*/ int MY_VALUE = 10;

13. }

下列哪三个选项可以用于第12行

A. final

B. static

C. native

D. public

E. private

F. abstract

G. protected

第九题

5. class Atom {

6.Atom() { System.out.print("atom "); }

7. }

8. class Rock extends Atom {

9.Rock(String type) { System.out.print(type); }

10. }

11. public class Mountain extends Rock {

12.Mountain() {

13. super("granite ");

14. new Rock("granite ");

15.}

16.public static voidmain(String[] a){ newMountain(); }

17. }

上述代码片段的运行结果是?

A. 编译失败.

B. atom granite

C. granite granite

D. atom granite granite

E. An exception is thrown at runtime.

F. atom graniteatom granite

第十题

10. class Line {

11.public class Point { public int x, y;}

12.public Point getPoint() { return new Point(); }

13. }

14. class Triangle {

15.public Triangle() {

16. // 此处插入代码

17.}

18. }

在16行处插入代码获得一个Class Point的实例?

A. Point p = Line.getPoint();

B. Line.Point p = Line.getPoint();

C. Point p = (new Line()).getPoint();

D. Line.Point p = (newLine()).getPoint();

自己选择的路,跪着也要把它走完。

不看OCJP考题你永远不知道自己的JAVA基础有多差(二)

相关文章:

你感兴趣的文章:

标签云: