(51) Click the exhibit button.
1. import java.io.IOException;
2. public class ExceptionTest{
3. public static void main(String[] args){
4. try {
5. methodA();
6. } catch (IOException e){
7. System.out.println("Caught IOException");
8. } catch (Exception e){
9. System.out.println("Caught Exception");
10. }
11. }
12. public void methodA (){
13. throw new IOException ();
14. }
15.}
What is the result?
A. The code will not compile.
B. The output is Caught Exception.
C. The output is Caught IOException.
D. The program executes normally without printing a message.
答案:A。13行錯,應在12行加throws IOException。另外method A是動態的,第5行沒有先new實體直接呼叫會失敗的。
(52) Click the exhibit button:
1. public class test {
2. public static String output = "";
3. public static void foo(int i){
4. try {
5. if(i==1){
6. throw new Exception ();
7. }
8. output += "1";
9. } catch (Exception e){
10. output += "2";
11. return;
12. }
13. finally {
14. output += "3";
15. }
16. output += "4";
17. }
18. public static void main(String args[]){
19. foo(0);
20. foo(1);
21.
22. }
23.}
What is the value of the variable output at line 21?
A. 11
B. 12
C. 1234
D. 13423
E. 1342
F. 134134
G. 10
答案: D。有Finally保證會執行,並且優於return
(136) Given:
1. public class ExceptionTest {
2. class TestException extends Exception {}
3. public void runTest () throws TestException {}
4. public void test () /* Point X*/ {
5. runTest ();
6. }
7. }
At point X on line 4, which code can be added to make the code compile?
A. throws Exception
B. catch (Exception e)
C. throws RuntimeException
D. catch (TestException e)
E. no code is necessary
答案: A。BD絕對不可能。要加什麼取決於有沒有"應檢核例外"(嚴重例外),所以因為第3行的自訂例外TestException,在第2行已經繼承Exception,而Exception是應檢核行例外,必須有throws。最正確寫法應是 throws TestException,不過Exception更大因此可以cover。
課本sg2-p40 Assertion Syntax
程式碼內有assert指令的話,JVM會自動跳過不執行,除非加入秘密指令:
public class AssertTest {
public static void main(String argv[]) {
double a = -2.1;
assert Math.ceil(a)==-3.0 : "Math.ceil(a) != -3.0" ;
// assert指令需要在啟動虛擬機器時加上 -ea 或是 -enableassertions 的執行參數才會執行
// 如果沒有加上執行參數則assert指令整行會被跳過不做.
// assert指令後面如果遇到false就會拋出AssertionError實體,造成程式異常中止
// assert不適當用法1 : 用assert去檢查public method的執行參數
// assert不適當用法2 : assert 後面接method呼叫
}
}
p/40.41
見到false就會被觸發。
23. int z=5;
24.
25. public void test1(int x) {
26. assert(x>0);
27. switch(x) {
28. case 2 : x=3;
29. default: assert false; } }
30.
31. private void test2(int y) { assert y<0; }
32.
33. private void test3() { assert test4(); }
34.
35. private Boolean test4() { z=6; return false; }
Which statement is true?
statement 敘述
used 使用
appropriately 適當地
A. All of the assert statements are used appropriately.
B. Only the assert statement on line 31 is used appropriately
C. The assert statements on lines 29 and 31 are used appropriately
D. The assert statements on lines 26 and 29 are used appropriately
E. The assert statements on lines 29 and 33 are used appropriately
F. The assert statements on lines 29 ,31and 33 are used appropriately
G. The assert statements on lines 26,29 and 31 are used appropriately
答案: C。
Given a method that must ensure that its parameter is not null:
11. public void someMethod(Object value) {
12. // check for null value
13. System.out.println(value.getClass());
14. }
What, inserted at line 12, is the appropriate way to handle a null value?
line 12 第12行
appropriate 合適的, 適當地
way 方式
handle 處理
A. assert value == null;
B. assert value != null, "value is null";
C. if (value == null) { throw new AssertionException("value is null"); }
D. if (value == null) { throw new IllegalArgumentException("value is null"); }
答案: D。題目說"一定要"確認不是null,所以assert是無法保證的。C的class名稱故意寫錯了不能選。
public class test {
public static void main(String[] a) {
assert a.length==1;
}
}
Which two will produce an AssertionError? (Choose two.)
produce 產生
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
答案: BE。AC沒有-ea一定錯