日期:2014-05-20  浏览次数:20682 次

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楼说的正确的