OCP考试题目考点分析

                                 OCP考试题目考点分析

     这是我第一次写简书,内容是我最近准备的ocp考试题目。里面涉及到大量的java基础知识。我把前面经典易错的五道题拿出来讲解,分析考点,加粗部分为分析部分,选项部分加粗部分为答案。如有不足,请多指教。


QUESTION 1

    Given a pre-generics implementation of a method:

//给定一个方法的pre-generics实现:

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

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> {


泛型申请不允许是问号,首先必须要有N这个泛型,然后要包含doublevalue的方法,就只有number,和integer




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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容