Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

請程式設計JAVA高手來解英文題 這兩題

1. Given the following code:

1. try {

2. //some code here

3. } catch(NullPointerException e1) {

4. System.out.print”a”);

5. } catch(RunTimeException e2) {

6. System.out.print(“b”);

7. } finally {

8. System.out.print(“c”);

9. }

what is the result if NullPointerExcetption occurs on line 2?

A. c

B. a

C. ab

D. ac

E. bc

F. abc

--------------------------------------------

2. Given the following code:

1.public static void main(String[] args) {

2. try {

3. args=null;

4. args[0]=”best”;

5. System.out.println(args[0]);

6. } catch (Exception e) {

7. System.out.println(“Exception”);

8. }

9.}

what is the result?

A. test

B. Exception

C. Compilation fails

D. NullPointerException.

-----------------------------------

請說明為何要選這個?

請勿亂掰,請勿亂猜

程式設計高手來解題

奉上20點

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    1. Given the following code:

    1. try {

    2. //some code here

    3. } catch(NullPointerException e1) {

    4. System.out.print("a");

    5. } catch(RunTimeException e2) {

    6. System.out.print("b");

    7. } finally {

    8. System.out.print("c");

    9. }

    what is the result if NullPointerExcetption occurs on line 2?

    A. c

    B. a

    C. ab

    D. ac

    E. bc

    F. abc

    Ans: D

    NullPointerException 會先被第一個 catch 捉到並執行其內程式,一旦被處理後就不會再被其它的 catch 處理; finally 是不管有沒有 exception, 有沒有被 catch, 都會執行.

    --------------------------------------------

    2. Given the following code:

    1.public static void main(String[] args) {

    2. try {

    3. args=null;

    4. args[0]="best";

    5. System.out.println(args[0]);

    6. } catch (Exception e) {

    7. System.out.println("Exception");

    8. }

    9.}

    what is the result?

    A. test

    B. Exception

    C. Compilation fails

    D. NullPointerException.

    Ans: B

    由於 args 在第3行被設為 null, 所以第4行程式碼在被 evaluated 時就會產生 NullPointerException, 也就是第4、5行都不會被執行,而是直接跳到 catch 區塊中執行印出 Exception.

Still have questions? Get your answers by asking now.