第146題:

Given:

1. import java.io.*;
2. public class Foo implements Serializable{
3.     public int x, y;
4.     public Foo(int x, int y){this.x = x; this.y = y;}
5.
6.     private void writeObject(ObjectOutputStream s)
7.                             throws IOException{
8.         s.writeInt(x); s.writeInt(y);
9.     }
10.
11.     private void readObject(ObjectInputStream s)
12.     throws IOException, ClassNotFoundException{
13.
14.         //insert code here
15.

16.     }
17. }

Which code, inserted at line 14, will allow this class to correctly serialize and deserialize?

A. s.defaultReadObject();
B. this = s.defaultReadObject(),
C. y = s.readInt(); x = s.readInt();
D. x = s.readInt(); y = s.readInt();

答案:

題目範圍:串流

解析:

因為讀出串流時,順序是x 然後y,因此寫入時應該也要一樣先x 然後y

defaultReadObject 是搭配defaultWriteObject 使用的

 

第147題:

Given:

1. public class LineUp{
2.     public static void main(String[] args){
3.         double d = 12.345;
4.         //insert code here
5.     }

6. }

Which code fragment, inserted at line 4, produces the output |12.345|?

A. System.out.printf("|%7d| \n", d);
B. System.out.printf("|%7f| \n", d);
C. System.out.printf("|%3.7d| \n", d);
D. System.out.printf("|%3.7f| \n", d);
E. System.out.printf("|%7.3d| \n", d);
F. System.out.printf("|%7.3f| \n", d);

答案:

題目範圍:基本觀念

解析:

其實這題有點怪怪的,因為|%7.3f|印出來的不是|12.345|而是| 12.345| (1前面有空白)

小數點左邊代表輸出數字的全長(含小數點);右邊代表浮點數的精度
12.345
總長度是6,所以1前面會出現1個空白

 

第148題:

Given:

12. import java.io.*;
13. public class Forest implements Serializable{
14.     private Tree tree = new Tree();
15.     public static void main(String[] args){
16.         Forest f = new Forest();
17.         try{
18.             FileOutputStream fs = new FileOutputStream("Forest.ser");
19.             ObjectOutputStream os = new ObjectOutputStream(fs);
20.             os.writeObject(f); os.close();
21.         }catch(Exception ex){ex.printStackTrace();}
22. }}
23.
24. class Tree{}

What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. An instance of Forest is serialized.
D. An instance of Forest and an instance of Tree are both serialized.

答案:

題目範圍:序列化

解析:

序列化時,被序列化物件內的所有除了方法之外的內容,都會被序列化
這裡的
Tree因為沒有實現Serializable無法被序列化,會在執行時跳出java.io.NotSerializableException

 

第149題:

Place the Fragments into the program, so that the program will get lines from a text file. display them, and then close all the resources.

Program

Code Fragments

import java.io.*;

class ReadFile {
    public static void main(String[] args) {
        try {
            File x1 = new File("MyText.txt");
            ____FileReader x2 = new ____FileReader(x1);
            BufferedReader x4 = new BufferedReader(x2);
            String x3 = null;
            while ((x3 = x4.readLine()) != null) {
                System.out.println(x3);
            }
            x4.___close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

BufferedReader
StreamReader
FileReader
readLine
readLn
read
closeFile
close
x1
x2
x3
x4

題目範圍:開檔讀檔

解析:

 

第150題:

Given:

5. import java.io.*;
6. public class Talk{
7.     public static void main(String[] args){
8.         Console c = new Console();
9.         String pw;
10.         System.out.print("password: ");
11.         pw = c.readLine();
12.         System.out.println("got" + pw);
13.     }
14. }

If the user types the password aiko when prompted, what is the result?

A. password:
 got

B. password:
 got aiko

C. password: aiko
 got aiko

D. An exception is thrown at runtime.
E. Compilation fails due to an error on line 8.

答案:

題目範圍:靜態類別

解析:

System.console() 才是正確的使用方法

console Singleton型態的類別
Singleton
:建構子是private,被設定為不可new 成物件,而本身是靜態類別,可以直接使用

arrow
arrow
    全站熱搜

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