OCJP试题分析第一章

视频 https://edu.csdn.net/course/play/7811

Exam A

QUESTION 1
Given a pre-generics implementation of a method:
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. }
What three changes allow the class to be used with generics and avoid an unchecked warning? (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)".
Answer: ACF

随题分析:注意,此处的题意是要更改三处,而不影响程序的警告,同时更改,切记切记!!!

QUESTION 2
A programmer has an algorithm that requires a java.util.List that provides an efficient implementation of

add(0, object), but does NOT need to support quick random access. What supports these requirements?

翻译:程序员有一个算法要求一个java.util.List 提供一个能有效实施add(0, object)但并不需要支持快速随机访问什么支持这些要求

A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
Answer: D

Queue(队列),没有add()方法。

ArrayList类,有add(0,object)的方法,支持随机访问(random access)

 LinearList没有这样的东东

扫描二维码关注公众号,回复: 1374688 查看本文章

LinkedList:链表;链表需要从begin()遍历到end(),复杂度是O(n);随机访问的是ArrayList

public class LinkedList<E>
extends AbstractSequentialList<E>

implements List<E>, Queue<E>, Cloneable, java.io.Serializable

此类实现 Queue 接口,为 addpoll 等提供先进先出队列操作。

QUESTION 3
Given:
11. // insert code here
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 || added.doubleValue() < min.doubleValue())
17. min = added;
18. if (max == null || added.doubleValue() > max.doubleValue())
19. max = added;
20. }
21. }

Which two, inserted at line 11, will allow the code to compile? (Choose two.)

有哪两项,在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> {

Answer: DF

12行代码,定义的变量min,max,方法doubleValue(),可以知道min和max是抽象类Number或其子类的实例化对象。


Number抽象类,所具有的方法如下:


QUESTION 4
Given:
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. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
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
本题主要考查:

1、TreeSet 自然排序

2、subSet()方法的运用

如果同学,你连TreeSet是个啥都不懂,需要下去恶补TreeSet方面的技术点。

有同学,可能会问这啥意思呢?

在循环中,18行代码,s的值是[606,608,610,612],最后在20行代码处又加了629,所以最后s存储的值为[606,608,610,612,629]

排除法就是E。

      返回此 set 的部分视图,其元素从 fromElement(包括)到 toElement(不包括)。(如果 fromElementtoElement 相等,则返回的已排序 set 为空。)返回的已排序 set 受此 set 支持,所以在返回的已排序 set 中的更改将在此 set 中得到反映,反之亦然。返回的已排序 set 支持所有可选 Set 操作。

如果用户试图插入指定范围之外的元素,那么由此方法返回的已排序 set 将抛出 IllegalArgumentException

19行代码,给subs这个TreeSet对象赋值,subSet()方法,类似于String的截取字符串,这里是取部分tree,true表示包含元素节点,false表示不包含元素节点。因为是包含节点,所以,subs为[608,610]

QUESTION 5

Given:

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 int getLosses() { return losses; }

6. public String toString() {

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

8. }

9. // insert code here

10. }

Which method will complete this class?

下列哪个方法可以在第9行补充完此类

A. public int compareTo(Object o){/*more 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*/}

 

Answer: B


/**
 * Comparable接口实现;
 * @author 张晨光
 *
 */
public class Score implements Comparable<Score>{
	//wins:胜利;lossess:损失(损耗)
	private int wins,losses;
	public Score(int w,int l){wins=w;losses=l;}
	public int getWins(){return wins;}
	public int getLosses(){return losses;}
	public String toString(){
		return "<"+wins+","+losses+">";
	}
	
	@Override
	public int compareTo(Score o) {
		return 0;
	}
}
public interface Comparable<T>
 
 

此接口强行对实现它的每个类的对象进行整体排序。此排序被称为该类的自然排序,类的 compareTo 方法被称为它的自然比较方法Comparabale接口需要实现compareTo方法泛型是Score

compareTo就是比较两个值,如果前者大于后者,返回1,等于返回0,小于返回-1

不管这个接口还是compartor,都需要和泛型参数保持一致,如下图:


另外一个比较接口:


猜你喜欢

转载自blog.csdn.net/zhangchen124/article/details/79927577