第一題: 

import java.io.*;

public class Test implements Serializable {
   
private Tree tree = new Tree();

   
public static void main(String[] args) {
        Test f =
new Test();
       
try {
            FileOutputStream fs =
new FileOutputStream("Forest Ser");
            ObjectOutputStream OS =
new ObjectOutputStream(fs);
            OS.writeObject(f);
            OS.close();

        }
catch (Exception ex) {
            ex.printStackTrace();

        }
    }
}

class Tree {
}

 

Given the code in the exhibit. 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.
 

答案:D 

執行結果:
java.io.NotSerializableException: Tree
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Test.main(Test.java:12)

題目範圍:資料輸入輸出 I/O

解析:

如果要將物件序列化(Serializ) 必須 implement(s) Serializable,雖然主要的Test類別有實現Serializable,不過底下所有的屬性也都必須是Serializable的,在這一題中,Tree 類別不是Serializable 因此出現錯誤。

如果將Tree 類別也implements Serializable ,那就可以正常執行。

如果非Serializable 的物件是在方法(method) 中被宣告,由於方法不會被序列化,因此內含的物件也不需要implements Serializable,而也可以解釋為:方法中的稱作變數,不稱作屬性,因此不需要implements Serializable



第二題: 

class Foo implements Serializable {
   
public int x, y;

   
public Foo(int x, int y) {
       
this.x = x;
       
this.y = y;
    }

   
private void writeObject(ObjectOutputStream s) throws IOException {
        s.writeInt(x);
        s.writeInt(y);
    }

   
private void readObject(ObjectInputStream s) throws IOException,
            ClassNotFoundException {
        // Insert code here
    }
}
 

Which code, inserted ay line 14, will allow this class to correctly serialized and desterilize? 
A. s.defaultReadObject(); 
B. this = s.defaultReadObject(); 
C. y = s.default (); x = s.readInt(); 
D. x = s.readlInt(); y = s.readInt();

答案:

執行結果:無

題目範圍:資料輸入輸出 I/O

解析:

序列化(serialize) 就是將物件轉成串流,儲存或處理
反序列化(desterilize) 就是將串流還原為物件
這裡用到writeInt() 依序x, y來序列化
因此反序列化時,應依序x, y readInt() ,還原物件

選項A. s.defaultReadObject(); 用途待查,似乎搭配defaultWriteObject()使用
選項B. this = s.defaultReadObject(); 無此用法,this 不可指定變數
選項C. y = s.default(); x = s.readInt(); 無此用法default()



第三題: 

Given the exhibit. 
        String test = "this is a test";
        String[] tokens = test.split("\s");
        System.out.println(tokens.length);

 

What is the result? 
A. 0 
B. 1 
C. 4 
D. Compilation fails 
E. An exception is thrown at runtime

答案:

執行結果:無

題目範圍:字串操作、JAVA陳述式

解析:

使用斜線表示跳脫字元
JAVA
中,沒有"\s"這個跳脫字元,不過正規表示法中,\s代表空白的意思

常見的合法的跳脫字元有:

\b

倒退←

\t

Tab

\n

換行

\f

換頁

\r

回到行首

\”

雙引號

\’

單引號

\\

斜線符號

常用的正規表示特定字元:

\d

數字

\D

非數字

\w

數字、底線、英文字母

\W

\w

\s

空白字元

\S

非空白字元

如果要在字串中表示正規表示法開頭的斜線,必須使用跳脫字元中的斜線表示才行
因此若要正確地以空白字元來分割字串,就應該這樣寫 ”\\s”

String split() 方法會將字串分割為數塊,以傳入的字串分割,回傳一個字串陣列。傳入的字串可以是正規表示式

 

第四題: 

Given the following Java code:
        Data date = new Date();
        df.setLocale(Local.ITALY);
        String s = df.format (date);

The variable df is an object of type DateFormat that has been initialized in line 11. What is the result if this code is run on December 14, 2000?
A. The value of S is 14-dec-2004
B. The value of S is Dec 14, 2000
C. An exception is thrown at runtime
D. Compilation fails because of an error in line 13

答案:

執行結果:無

題目範圍:API的使用

解析:

API稍作調查,就可以發現DateFormat 並沒有setLocale() 方法,因此在編譯時就會報錯了
如果想要得到結果,應該要這樣修改

    DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT,Locale.Ialy);
    String s = df.format(date);

執行結果會是 14-dec-2000

其中 DateFormat.DEFAULTLONGSHORT等等選擇,Locale.Ialy 指的是語言,跟時區沒有關係
如果把它改成DateFormat.LONG, Locale.TAIWAN ,就會有這個結果:20001214





第五題:(拖拉題) 

The doesFileExist method takes an array of directory names representing a path from the root filesystem and a file name. The method returns true if the file exists, falst if does not. Place the code fragments in position to complete this method. 

        public static boolean doesFileExist(String[] directories, String filename){
            Place here  String path = "";
           
for(String dir : directories){
                Place here  path = path + File.separator + dir;
            }
            Place here  File file =
new File(path, filename);
            Place here  return file.exists();
        }


Code Fragments

path = path.getSubdirectory(dir);

return !file.isNew();

return(file != null);

String path = "";

path = path.getFile(filename);

File path = new File("");

return file.exists();

return path.isFile();

File file = new File(path, filename);

path = new File(path, dir);

File path = new File(File.separator);

path = path + File.separator + dir;



答案:題目反白 

執行結果:無

題目範圍:for迴圈列舉法、撰寫程式

解析:

JAVA 1.5 版開始,增加了for迴圈列舉法的功能,專門配合陣列來使用
當需要使用到陣列中的全部元素時,用列舉法是最簡潔的,使用方法多看程式即可學會



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

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