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

解决方案 »

  1.   

    java -ea    -ea[:<packagename>...|:<classname>]
        -enableassertions[:<packagename>...|:<classname>]
                      enable assertions
    A. java test // 没有 enable AssertionB. java -ea test // a.length = 0, AssertionErrorC. java test file1 // 没有 enable AssertionD. java -ea test file1 // a.length = 1, OKE. java -ea test file1 file2 // a.length = 2, AssertionErrorF. java -ea:test test file1 // a.length = 1, OK
      

  2.   

    java 命令执行时可以指定是否启用 assertion,-ea:test 指定启用 class test 的 assertion, 其他类如果有 assertion 的话,这里没启用-ea 启用所有的 assertion
      

  3.   

    两题考的都是 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
      

  4.   


    加红部分应该是错的吧,因为:Further, the assert construct does not throw an exception of the specified type. It can throw only an AssertionError.(参考:http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html,然后搜索字符“It can throw only an AssertionError”就可以看到了。)
      

  5.   


    加红部分也不对,正确的应该是下面加蓝的:
    The second form of the assertion statement is:     assert Expression1 : Expression2 ;
    where: Expression1 is a boolean expression. 
    Expression2 is an expression that has a value. (It cannot be an invocation of a method that is declared void.) 
    Use this version of the assert statement to provide a detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message..(参考:http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html,在Introduction里)