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.)
更改哪三處允許這個(gè)類使用泛型和避免一個(gè)不受檢查警告
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
隨題分析:注意,此處的題意是要更改三處,而不影響程序的警告,同時(shí)更改,切記切記!!!
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?
翻譯:程序員有一個(gè)算法要求一個(gè)java.util.List?提供一個(gè)能有效實(shí)施add(0, object),但并不需要支持快速的隨機(jī)訪問(wèn)。是什么支持這些要求呢?
?
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
Answer: D
?
Queue(隊(duì)列),沒(méi)有add()方法。
ArrayList類,有add(0,object)的方法,支持隨機(jī)訪問(wèn)(random access)
?LinearList沒(méi)有這樣的東東
LinkedList:鏈表;鏈表需要從begin()遍歷到end(),復(fù)雜度是O(n);隨機(jī)訪問(wèn)的是ArrayList
public class LinkedList<E>extends AbstractSequentialList<E>
implements List<E>, Queue<E>, Cloneable, java.io.Serializable
此類實(shí)現(xiàn) Queue 接口,為 add、poll 等提供先進(jìn)先出隊(duì)列操作。
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.)
有哪兩項(xiàng),在11行插入代碼,可以將代碼進(jìn)行編譯?
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(),可以知道m(xù)in和max是抽象類Number或其子類的實(shí)例化對(duì)象。
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()方法的運(yùn)用
如果同學(xué),你連TreeSet是個(gè)啥都不懂,需要下去惡補(bǔ)TreeSet方面的技術(shù)點(diǎn)。
有同學(xué),可能會(huì)問(wèn)這啥意思呢?
在循環(huán)中,18行代碼,s的值是[606,608,610,612],最后在20行代碼處又加了629,所以最后s存儲(chǔ)的值為[606,608,610,612,629]
排除法就是E。
? ? ? 返回此 set 的部分視圖,其元素從 fromElement(包括)到 toElement(不包括)。(如果 fromElement 與 toElement 相等,則返回的已排序 set 為空。)返回的已排序 set 受此 set 支持,所以在返回的已排序 set 中的更改將在此 set 中得到反映,反之亦然。返回的已排序 set 支持所有可選 Set 操作。
如果用戶試圖插入指定范圍之外的元素,那么由此方法返回的已排序 set 將拋出 IllegalArgumentException。
19行代碼,給subs這個(gè)TreeSet對(duì)象賦值,subSet()方法,類似于String的截取字符串,這里是取部分tree,true表示包含元素節(jié)點(diǎn),false表示不包含元素節(jié)點(diǎn)。因?yàn)槭前?jié)點(diǎn),所以,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?
下列哪個(gè)方法可以在第9行補(bǔ)充完此類
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接口實(shí)現(xiàn);* @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+">";}@Overridepublic int compareTo(Score o) {return 0;} } public interface Comparable<T>此接口強(qiáng)行對(duì)實(shí)現(xiàn)它的每個(gè)類的對(duì)象進(jìn)行整體排序。此排序被稱為該類的自然排序,類的 compareTo 方法被稱為它的自然比較方法。Comparabale接口需要實(shí)現(xiàn)compareTo方法泛型是Score
compareTo就是比較兩個(gè)值,如果前者大于后者,返回1,等于返回0,小于返回-1
不管這個(gè)接口還是compartor,都需要和泛型參數(shù)保持一致,如下圖:
另外一個(gè)比較接口:
總結(jié)
以上是生活随笔為你收集整理的OCJP试题分析第一章的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java 冒泡排序实现及口诀
- 下一篇: OCJP认证考试复习课-张晨光-专题视频