第141題:

Given:

11. class A{
12.     public void process(){System.out.print("A, ");}
13. class B extends A{
14.     public void process() throws IOException{
15.     super.process();
16.     System.out.print("B, ");
17.     throw new IOException();
18. }
19. public static void main(String[] args){
20.     try{new B().process();}
21.     catch(IOException e){System.out.println("Exception");}
22. }

What is the result?

A. Exception
B. A, B, Exception
C. Compilation fails because of an error in line 20.
D. Compilation fails because of an error in line 14.
E. A NullPointerException is thrown at runtime.

答案:

題目範圍:例外處理

解析:

B類別在覆寫A類別的process()方法時,由於A.process()類別並沒有擲出IOException,因此B.process()也不可擲出IOException

如果A.process()有擲出Exception,則B.process()可以擲出IOException(因為是Exception的子類別),但裡面的super.process();必須再做例外處理

 

第142題:

Assuming that the serializeBanana() and the deserializeBanana() methods will correctly use Java serialization
and given:

13. import java.io.*;
14. class Food implements Serializable{int good = 3;}
15. class Fruit extends Food{int juice = 5;}
16. public class Banana extends Fruit{
17.     int yellow = 4;
18.     public static void main(String[] args) {
19.         Banana b = new Banana(); Banana b2 = new Banana();
20.         b.serializeBanana(b); //assume correct serialization
21.         b2 = b.deserializeBanana(); //assume correct
22.         System.out.println("restore " + b2.yellow + b2.juice + b2.good);
24.     }
25. //more Banana methods go here
50. }

What is the result?

A. restore 400
B. restore 403
C. restore 453
D. Compilation fails.
E. An exception is thrown at runtime.

答案:

題目範圍:序列化

解析:

b物件序列化,再反序列化,結果就會跟原本的b一樣

最後就是相當於b2 = b

印出來的參數當然也沒有甚麼問題

 

第143題:

Given:
System.out.printf("Pi is approximately %f and E is approximately %b", Math.PI, Math.E);
Place the values where they would appear in the output.

Pi is approximately 3.141593

and E is approximately true___

Values

3 3.141593 true Math.PI

2 2.718282 false Math.E

題目範圍:基本觀念

解析:

JAVA 在布林值中,除了null false 之外都是true

 

第144題:

Which capability exists only in java.io.BufferedWriter?

A. Closing an open stream.
B. Flushing an open stream.
C. Writing to an open stream.
D. Writing a line separator to an open stream.

答案:

題目範圍:串流

解析:

參閱:JAVA筆記:I/O的簡介

 

第145題:

Given that the current directory is empty, and that the user has read and write permissions, and the following:

11. import java.io.*;
12. public class DOS{
13.     public static void main(String[] args){
14.         File dir = new File("dir");
15.         dir.mkdir();
16.         File f1 = new File(dir, "f1.txt");
17.         try{
18.             f1.createNewFile();
19.         }catch(IOException e){;}
20.         File newDir = new File("newDir");
21.         dir.renameTo(newDir);
22.     }
23. }

Which statement is true?

A. Compilation fails.
B. The file system has a new empty directory named dir.
C. The file system has a new empty directory named newDir.
D. The file system has a directory named dir, containing a file f1.txt.
E. The file system has a directory named newDir, containing a file f1.txt.

答案:

題目範圍:檔案處理

解析:

mkdir()方法會依照名稱建立出檔案資料夾
renameTo()
方法會將指定的檔案或資料夾重新命名

arrow
arrow
    全站熱搜

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