- 5
- 0
- 约3.75万字
- 约 32页
- 2021-01-26 发布于天津
- 举报
QUESTION
QUESTION PAGE #
GIVEN THE EXHIBIT:
23.
int z = 5 F
24
25
public voidd Certkillerl (int x)
(
26
assert ( x 0 ),
27
switch (ii)(
2:8
case 2 :x = 3,
29
default : ert fak
)
30.
)
31
private void Certkiller2 (int y ) {
assert (y o) }
32
33.
private void Certklller3 () { assert ( Certkiller4()),
34
35
private Boolean Certkiller^l ()(
i = 6P return false
Which stateme nt is true?
All of the assert stateme nts are used appropriately.
Only the assert stateme nt on line 31 is used appropriately
The assert stateme nts on lines 29 and 31 are used appropriately
The assert stateme nts on lines 26 and 29 are used appropriately
The assert stateme nts on lines 29 and 33 are used appropriately
The assert stateme nts on lines 29 ,31a nd 33 are used appropriately
The assert stateme nts on lines 26,29 and 31 are used appropriately
断言应该用在 你认为”你的程序不可能产生错误的地方,而且有没有启用断言,都不会影响程序的正常运行。
断言使用限制:
不要在public方法中,用断言来检查参数的正确性;
不要让断言语句去处理一些程序必须的流程。
原因:1.public方法会被别人调用,你不能保证他一定启用断言;如果没有启用,那么用断言来做参数的检查也就没有 意义了。所以不要用断言来检查参数, 公共方法的参数一定要用代码执行检查; 2.如果用断言来控制程序执行流程,
如果没有启用断言, 那么程序就不能正确执行下去。另外, 断言语句不可以有任何边界效应,不要使用断言语句去修
改变量和改变方法的返回值 ,如果这样当启动断言和不启动断言执行的结果会截然不同。
断言的使用时机:
检查流程的不变性:在 if-else switch-case的预期结果之外可以加上断言做额外的检查。
内部执行的不变性:if(true){return ;} assert false;
检查 私有方法的参数,结果等
程序运行中的一致性
断言语句不是永远会执行,可以屏蔽也可以启用
javac -source 1.4 *.java
需要java走a启用assert;当判断条件为FALSE时就抛出错误。
Answer: ( C )
26行不合适:不要对 public方法的参数断言
29合适:程序员在程序中最不大可能到达的地方断言
31合适:断言private方法的参数
33行不合适:启用和不启用断言会产生不同的程序执行序
参考大纲:异常处理 一断言和AssertionError
11
public static void rn^in (String [ ] args) (
12
String str = *nuir*
13
if (str - = null) f
14,
system ourpnntin Cnuir)F
15
)else (str hngth () = = 0) {
也
system.outpnntln ( zero)
□
)else (
1
system cut println (some)
19
)
20
}
What is the result?
null
zero
some
Compilati on fails
An exceptio n is throw n at run time
Answer: ( D )
13行会报错,应在15行使用else if
参考大纲:流程控制
QUESTION 63
Give n the exhibit:
pubhc static void main (String ] ] args) (
12. try (
zrg 玄—null;
aegs [0] = *tesf \
System out print I
原创力文档

文档评论(0)