第131題:

Given:

    static void test() throws Error {
        if (true)
            throw new AssertionError();
        System.out.print("test ");
    }

    public static void main(String[] args) {
        try {
            test();
        } catch (Exception ex) {
            System.out.print("exception ");
        }
        System.out.print("end ");
    }

What is the result?

A. end
B. Compilation fails.
C. exception end
D. exception test end
E. A Throwable is thrown by main.
F. An Exception is thrown by main.

答案:

題目範圍:例外處理

解析:

ErrorException都是Throwable的類別
但兩者並沒有繼承關係

在丟出Error的時候,無法被catch (Exception ex)接到,因此最後會被main方法丟出

Error沒有強制規定必須被catch,因此並不會出現Compilation fails

 

第132題:

Given:

1. class TestException extends Exception {}
2. class A {
3.    public String sayHello(String name) throws TestException {
4.        if (name == null)throw new TestException();
5.        return "Hello " + name;
6.    }
7. }
8. public class TestA {
9.    public static void main(String[] args) {
10.        new A().sayHello("Aiko");
11.    }
12.}

Which statement is true?

A. Compilation succeeds.
B. class A does not compile.
C. The method declared on line 9 cannot be modified to throw TestException.
D. TestA compiles if line 10 is enclosed in try/catch block that catches TestException.

答案:

題目範圍:例外處理

解析:

丟出的Exception必須被處理,否則會出現Compilation fails

main方法也可以丟出例外

 

第133題:

Given:

public class ClassA {
    public void methodA() {
        ClassB classB = new ClassB();
        classB.getValue();
    }
}

And:
class ClassB {
    public ClassC classC;

    public String getValue() {
        return classC.getValue();
    }
}

And:
class ClassC {
    public String value;

    public String getValue() {
        value = "ClassB";
        return value;
    }
}

And given:

ClassA a = new ClassA();
a.methodA();

What is the result?

A. Compilation fails.
B. ClassC is displayed.
C. The code runs with no output.
D. An exception is thrown at runtime.

答案:

題目範圍:物件與指標

解析:

執行時會在ClassBgetValue方法出現java.lang.NullPointerException

因為classC只是一個指標而沒有被指派物件
如果在
return之前加上classC = new ClassC();即可

 

第134題:

Given:

5. classA{
6.     void foo() throws Exception{throw new Exception();}
7. }
8. class SubB2 extends A{
9.     void foo(){System.out.println("B ");}
10. }
11. class Tester{
12.     public static void main(String[] args){
13.         A a = new SubB2();
14.         a.foo();
15.     }
16. }

What is the result?

A. B
B. B, followed by an Exception.
C. Compilation fails due to an error on line 9.
D. Compilation fails due to an error on line 14.
E. An Exception is thrown with no other output.

答案:

題目範圍:多型、例外處理

解析:

這是一個有點陷阱的題目

照多型的觀念來看,a.foo();會執行的是類別SubB2中的foo方法,而不是類別A
但是多型是在執行階段才判斷的,而編譯階段會先檢查例外處理是否完善
在這裡由於我們將物件
a 指派給A ,因此編譯階段會檢查foo方法是否有處理例外
然而即使你在這裡做了例外處理,執行時也是用不到的

 

第135題:

Given:

84. try{
85.     ResourceConnection con = resourceFactory.getConnection();
86.     Results r = con.query("GET INFO FROM CUSTOMER");
87.     info = r.getData();
88.     con.close();
89. }catch(ResourceException re){
90.     errorLog.write(re.getMessage());
91. }
92. return info;

Which statement is true if a ResourceException is thrown on line 86?

A. Line 92 will not execute.
B. The connection will not be retrieved in line 85.
C. The resource connection will not be closed on line 88.
D. The enclosing method will throw an exception to its caller.

答案:

題目範圍:例外處理、資料庫的使用

解析:

這是一個常在資料庫上發生的問題

當你的程式因為例外而中斷時,這樣的程式會造成資料庫連線無法順利關閉
如果這樣的事情重複的發生,你的伺服器將會崩潰
因為有許多未關閉但卻無人使用的連線一直開著,而且無限的增加…

你最好能在catch中放入關閉連線的程式碼
但是關閉連線的方法本身也是需要例外處理的,所以還需要再加另外一段例外處理

arrow
arrow
    文章標籤
    SCJP 考古題 JAVA
    全站熱搜

    yaya741228 發表在 痞客邦 留言(4) 人氣()