首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

SCJP考题一道:“assert”的使用解决思路

2012-02-10 
SCJP考题一道:“assert”的使用Given:Java code11. public void go(int x) {12. assert (x 0)13. switch(

SCJP考题一道:“assert”的使用
Given:

Java code
11. public void go(int x) {12. assert (x > 0);13. switch(x) {14. case 2: ;15. default: assert false;16. }17. }18. private void go2(int x) { assert (x < 0); } 



Which statement is true?
A. All of the assert statements are used appropriately.
B. Only the assert statement on line 12 is used appropriately.
C. Only the assert statement on line 15 is used appropriately.
D. Only the assert statement on line 18 is used appropriately.
E. Only the assert statements on lines 12 and 15 are used appropriately.
F. Only the assert statements on lines 12 and 18 are used appropriately.
G. Only the assert statements on lines 15 and 18 are used appropriately.

请问哪(些)个不对,错在哪里。多谢。

[解决办法]
line 15 错误。
[解决办法]
12 也错

使用assert的原则:
(1)只能验证private方法中的参数,不能验证public方法的参数;
(2)不能验证命令行参数;
(3)可以验证不应该发生的分支语句;
(4)不能以任何方式改变程序的状态。
[解决办法]
appropriately是指合适的,并不是说合法的。
3楼说的正确的

热点排行