美文网首页
OCP考试题目考点分析

OCP考试题目考点分析

作者: d4eef40cad6c | 来源:发表于2018-11-16 09:54 被阅读0次
这是我第一次写简书,内容是我最近准备的ocp考试题目。里面涉及到很多java的基础知识。我把前面经典易错的五道题拿出来讲解,分析考点,加粗部分为分析部分,选项部分加粗部分为答案。如有不足,请多指教。

QUESTION 1

11. public static int sum(List list) {

12. int sum = 0;

13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {//iterator是迭代器,有普通型变为泛型,所以14行不需要

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


QUESTION 2

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

image

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.

考点:内部类

注意这里有两个内部类,一个是名为A的内部类A,另一个是名为A的局部内部类A,在testFoo方法内部,局部内部类将屏蔽内部类A。

所以当调用Beta类型对象的testFoo方法时,将输出第28行的内容,new A()调用的是局部内部类A的构造函数,返回一个局部内部类A给fubar()函数,最终输出2。如果删除16,17,18行,由于局部内部类A在方法testFoo()内部屏蔽了外部的内部类A可以看出不会对局部内部产生任何影响。如果删除24,25,26行,那么本来被这个局部内部类屏蔽的不可见的局部类A 变得可见,输出自然也是1了。


QUESTION 3

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(529);

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. [529,606, 608, 610, 612,] [608, 610]

F. [606, 608, 610, 612, 629] [608, 610, 629]

G [606, 608, 610, 612, 629] [608, 610]

考点是tree的用法

treeset会对里面的数据自动排序,数字降序排列,subset 的用法

树的具体使用可以查看下面网址:

https://blog.csdn.net/yingpaixiaochuan/article/details/49019131


QUESTION 4

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.

B选项:删除HashMap中一个Person对象对应的键将会删除这个散列映射表中Person类的全部条目。错误,HashMap中Person对象的键值不是由Person对象决定的,而是程序员给定的键,例如staff.add("123-345", bob),就是把键为123-456的bob对象添加到名为staff的HashMap中,因而HashMap允许添加相同的对象。所以说,删除一个键对应的Person对象并不会删除所有的条目,他们的key都不同嘛。

C选项:向HashSet中插入另外一个Person对象将会引起第二个对象覆盖第一个对象。错误,虽然Person对象的hashCode方法返回的值都是420,这仅仅表明两个Person对象在一个entry链表中,接下来要调用equals方法,由于Person类没有equals方法,所以调用Object的equals方法返回对象的存储地址,很明显两个Person对象的存储地址是不同的。综上,HashSet中可以添加不同的Person对象,只要equals方法返回值为false就好。

D选项:判断一个HashSet中是否存在一个Person对象的次数是常数次,和map的大小无关。错误,由于Person对象的hashCode返回的值都是420,所以HashSet中的Person对象都在一个bucket中,组成了一条entry链表,查询速度与entry链表的大小息息相关。


QUESTION 5

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

构造器知识:https://blog.csdn.net/qq_36652405/article/details/79939297**

Explanation:当调用子类的构造函数的时候,如果没有显式调用父类的构造函数,那么父类的默认构造函数将被调用,如果父类没有默认的构造函数,编译器就会警告出错

Super和 this的用法:https://www.cnblogs.com/hasse/p/5023392.html

相关文章

网友评论

      本文标题:OCP考试题目考点分析

      本文链接:https://www.haomeiwen.com/subject/ceppfqtx.html