OCJP--SCJP--JAVA考试题

QUESTION 61

GIVEN THE EXHIBIT:

Which statement is true?

A. All of the assert statements are used appropriately.

B. Only the assert statement on line 31 is used appropriately

C. The assert statements on lines 29 and 31 are used appropriately D. The assert statements on lines 26 and 29 are used appropriately E. The assert statements on lines 29 and 33 are used appropriately F. The assert statements on lines 29 ,31and 33 are used appropriately G. The assert statements on lines 26,29 and 31 are used appropriately

断言应该用在“你认为”你的程序不可能产生错误的地方,而且有没有启用断言,都不会影响程序的正常运行。 断言使用限制:

1. 不要在public方法中,用断言来检查参数的正确性; 2. 不要让断言语句去处理一些程序必须的流程。

原因:1.public方法会被别人调用,你不能保证他一定启用断言;如果没有启用,那么用断言来做参数的检查也就没有意义了。所以不要用断言来检查参数,公共方法的参数一定要用代码执行检查; 2.如果用断言来控制程序执行流程,如果没有启用断言, 那么程序就不能正确执行下去。另外,断言语句不可以有任何边界效应,不要使用断言语句去修改变量和改变方法的返回值 ,如果这样当启动断言和不启动断言执行的结果会截然不同。 断言的使用时机:

1. 检查流程的不变性:在if-else switch-case 的预期结果之外可以加上断言做额外的检查。 2. 内部执行的不变性:if(true){return ;} assert false; 3. 检查 私有方法的参数,结果等 4. 程序运行中的一致性

断言语句不是永远会执行,可以屏蔽也可以启用 javac –source 1.4 *.java

需要java –ea 启用assert; 当判断条件为FALSE时就抛出错误。

Answer: ( C )

26行不合适:不要对public方法的参数断言

29合适:程序员在程序中最不大可能到达的地方断言 31合适:断言private方法的参数

33行不合适:启用和不启用断言会产生不同的程序执行序

参考大纲:异常处理 — 断言和AssertionError

QUESTION 62

GIVEN THE EXHIBIT:

What is the result? A. null B. zero C. some

D. Compilation fails

E. An exception is thrown at runtime

Answer: ( D )

13行会报错,应在15行使用else if 参考大纲:流程控制

QUESTION 63 Given the exhibit:

What is the result? A. test

B. Exception

C. Compilation fails

D. NullPointerException

Answer: ( C )

18行出错,应该先catch子异常,再catch Exception; 13行把args赋null ,14行会报NullPointerException

如果没有第13行运行时14行会报ArrayIndexOutOfBoundsException异常。 参考大纲:异常处理

QUESTION 64 Given the exhibit:

What is the result? A. Compilation fails B. aAaA aAa AAaa AaA C. AAaa AaA aAa aAaA D. AaA AAaa aAaA aAa E. aAa AaA aAaA AAaa

F. An exception is thrown at runtime

Answer: ( C )

第10行将对strings这个集合做自然排序(ASCII小到大,一个一个比较) Collections.sort(List list) 对list进行排序,对set不能排序!

List里可以放对象,所以当list里面存放的是对象的时候就不能用Collections.sort(List list)去排序了。因为JVM不知道用什么规则去排序!!只有把对象类实现Comparable接口,然后改写compareTo() 参考大纲:集合

QUESTION 65 Given the exhibit:

What is the result? A. 0 B. 1

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4