assert和assertion

Question 64
Given:
8. public class test {
9. public static void main(String [] a) {
10. assert a.length == 1;
11. }
12. }
Which two will produce an AssertionError? (Choose two.)
A. java test
B. java -ea test
C. java test file1
D. java -ea test file1
E. java -ea test file1 file2
F. java -ea:test test file1
Answer: BE
问题一(1):一道Java,完全不明白它在考什么???是Java命令行吗?什么意思啊?
请问这道题该怎么做啊???
谢谢。。。


关于assert的  个问题。谢谢,,,
Question 65
Given:
12. public class AssertStuff {
13.
14. public static void main(String [] args) {
15. int x= 5;
16. inty= 7;
17.
18. assert (x> y): “stuff”; //问题二(1):这是什么语句啊?有什么作用?
19. System.out.println(“passed”);
20. }
21. }
And these command line invocations:
java AssertStuff
java -ea AssertStuff //问题二(2):这又是什么语句啊?有什么作用?
What is the result?
A. passed
stuff
B. stuff
passed
C. passed
An AssertionError is thrown with the word “stuff” added to the stactrace.
D. passed
An AssertionError is thrown without the word “stuff” added to th
stack trace.
E. passed
An AssertionException is thrown with the word “stuff” added to th
stack trace.
F. passed
An AssertionException is thrown without the word “stuff”added to th
stack trace.
Answer: C


==========================================================================
java 命令执行时可以指定是否启用 assertion,

-ea:test 指定启用 class test 的 assertion, 其他类如果有 assertion 的话,这里没启用

-ea 启用所有的 assertion

两题考的都是 assert和assertion
assert是JDK1.4(&+)中新增的关键字,其功能称作assertion
assert 条件表达式            如果条件表达式不成立(false)则报AssertionException
assert 条件表达式:something  同上,同时something参与构造AssertionException,具体未知
assertion在一般情况下是关闭的,通过java -ea 可以打开该功能,关闭为 -da

题一、
A. java test                  \\正确 assertion此时是关闭的,assert a.length == 1不被执行
B. java -ea test              \\报错 此时assertion功能被打开,assert a.length == 1被执行,a为空,显然不成立
C. java test file1            \\正确 原因同A
D. java -ea test file1        \\正确 assertion功能被打开,且传入了一个参数,assert a.length == 1被执行,a.length == 1成立
E. java -ea test file1 file2  \\错误 过程同上,但此时传入了二个参数a.length等于2
F. java -ea:test test file1  \\正确 java -ea:test表示仅仅打开test的assertion功能

题二、
assert (x> y): “stuff”; //如果x>y不成立,报AssertionException,“stuff”参与构造该AssertionException
java -ea AssertStuff //打开assertion功能,并执行AssertStuff


你可能感兴趣的:(java,C++,c,C#,F#)