OCJP标准题库试题之————第一次

寻思了好久还是决定要考OCJP认证,跟着培训的进度估计要10次课
程。每次听课结束后,来总结下在课堂上学到的东西。

依题而论!! 今天开始。

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

考题都是英文的的,一般的英文都能看懂的。此题:让我们选择3项来让我们使用泛型,并且不会弹出来泛型警告!!。

A:移除14行,我们使用泛型的目的就是直接得到数据对象,不需要强制转换,而14行使用了强制转换,当然要移除14行 :A√ B×

C:替换13行:for(int i : intlist) 泛型的基本循环的3种形式之一 ,C√ D ×

E:替换方法名字: sum(List intlist) 泛型。使用基本类型要用其包装类作为泛型。所以 E× F√

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?
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
Answer: D
Section: (none)

题目的意思:又一个算法需要用list的实现接口的add方法。但是不需要支持快速的随机访问。 选择哪个数据结构来完成

A:队列,肯定不行它没有实现list接口。A×

B: ArrayList。有 add方法,支持快速方法有 gen(index)方法 ,底层是用数组实现的。不符合题意:B×

C: C向这个结构,查jdk api根本不存在

D:链表结构,支持add方法。缺点访问速度慢。链表结构,优点:添加删除的速度快。底层是引用实现的 D√

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.)
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

选择两项来完成编译 。要继承,就限定了类的使用。我们看到有doublevalue。那么肯定是数字了。数字 那就 D和F 了

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

考察 TreeSet的使用, Set集合。无序,但是值不能重复。实现不重复的原因是。执行equals和hashcode方法。。检查数据是否重复。看题目,17 18行。就是给s里面添加; 606,608,610,612 4个数字。subset方法。从608 到611的数字。true包括608和611。填充到子集合中。sub:608,610. 然后s:添加了629。.打印 的时候。s:606,608,610,612,629. sub:608,610。结果就是E了

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?
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接口。实现这个接口,就先实现里面的compareTo方法。泛型跟结构一致。选择B

QUESTION 6
Given:
11. public class Person {
12. private name;
13. public Person(String name) {
14. this.name = name;
15. }
16. public int hashCode() {
17. return 420;
18. }
19. }
Which statement is true?
A. The time to find the value from HashMap with a Person key depends on the size of the map.
B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.
C. Inserting a second Person object into a HashSet will cause the first Person object to be removed as a
duplicate.
D. The time to determine whether a Person object is contained in a HashSet is constant and does NOT
depend on the size of the map.
Answer: A
Se

题目问题:那一个说法是正确的。A:从Hashmap找一个值的时间依赖于这个集合的大小。A√。hashmap不存在重复值。该类重写了hashcode。这个方法永远为真。检验是否重复还有equlas方法。所以A正确

QUESTION 7
Given:
5. import java.util.*;
6. public class SortOf {
7. public static void main(String[] args) {
8. ArrayList<Integer> a = new ArrayList<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. }
What is the result?
A. [1, 2, 3, 5]
B. [2, 1, 3, 5]
C. [2, 5, 3, 1]
D. [5, 3, 2, 1]
E. [1, 3, 5, 2]
F. Compilation fails.
G. An exception is thrown at runtime.
Answer: C

一个ArrayList集合。add 1 5 3. 又使用了工具类Collections.sort排序。1,3,5. 添加2 。又reverse反转了这个顺序。就是2,5,3,1 。就是C √

QUESTION 8
Given
11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. } Which three are valid on line
12?
(Choose three.)
A. final
B. static
C. native
D. public
E. private
F. abstract
G. protected
Answer: ABD

接口常量的定义:public final static 方法:pulic abstract。选择 ABD√

QUESTION 9
Given:
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 void main(String[] a) { new Mountain(); }
17. }
What is the result?
A. Compilation fails.
B. atom granite
C. granite granite
D. atom granite granite
E. An exception is thrown at runtime.
F. atom granite atom granite
Answer: F

调用构造方法的先后。顺序:先调用父类的,再调用子类的。那么:就先打印了atom granite 然后。 new Rock(“granite ”) 先 atom 后granite 。, 就是F了。super只能存在方法的第一行

这里写图片描述

QUESTION 10
Click the Exhibit button. Which three statements are true? (Choose three.)


A. Compilation fails.
B. The code compiles and the output is 2.
C. If lines 16, 17 and 18 were removed, compilation would fail.
D. If lines 24, 25 and 26 were removed, compilation would fail.
E. If lines 16, 17 and 18 were removed, the code would compile and the output would be 2.
F. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1.
Answer: BEF

内部类在方法中定义,也可以在类内部定义,。能编译会输出2.B√。16,17,18。无关紧要。有没有一个样。, 25,26,27,如果没会输出 1 所以BEF √

QUESTION 11
Given:
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. // insert code here
17. }
18. }
Which code, inserted at line 16, correctly retrieves a local instance of a Point object?
A. Point p = Line.getPoint();
B. Line.Point p = Line.getPoint();
C. Point p = (new Line()).getPoint();
D. Line.Point p = (new Line()).getPoint();
Answer: D

内部类的实例化操作。由于不是静态的A B×。同级内部类。选择D

QUESTION 12
Given:
11. class Alpha {
12. public void foo() { System.out.print("Afoo "); }
13. }
14. public class Beta extends Alpha {
15. public void foo() { System.out.print("Bfoo "); }
16. public static void main(String[] args) {
17. Alpha a = new Beta();
18. Beta b = (Beta)a;
19. a.foo();
20. b.foo();
21. }
22. }
What is the result?
A. Afoo Afoo
B. Afoo Bfoo
C. Bfoo Afoo
D. Bfoo Bfoo
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: D

多态强制转换,分类引用子类对象。 调用的还是Beta。所以是D项。

今天就到这里了。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一缕阳光直射你的心扉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值