|
第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();
|
|
答案:D
|
|
題目範圍:串流
|
|
解析:
因為讀出串流時,順序是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);
|
|
答案:F
|
|
題目範圍:基本觀念
|
|
解析:
其實這題有點怪怪的,因為|%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.
|
|
答案:B
|
|
題目範圍:序列化
|
|
解析:
序列化時,被序列化物件內的所有除了方法之外的內容,都會被序列化 這裡的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.
|
|
答案:E
|
|
題目範圍:靜態類別
|
|
解析:
System.console() 才是正確的使用方法
console 是Singleton型態的類別 Singleton:建構子是private,被設定為不可new 成物件,而本身是靜態類別,可以直接使用
|
yaya741228 發表在 痞客邦 留言(13) 人氣(10,833)
請問一下 總共有幾題? 還有這是改版後的題庫嗎?(OCJP) 謝謝
總共有244題 這是改版前的題庫 雖然我還沒看過最新的OCJP, 不過根據之前幾次的改版, 我想這次也不會差太多 之後有空可能會考慮更近改版^^
*****
感謝版主製作 這樣在看考題比較方便 不然看PDF檔都會先看到答案
多謝您的支持^^ 自己偷看到答案真的超不開心的XD
*****
大大一系列的 SCJP 6.0考古題 文章,對準備要考照的人真是一大福音~
抱歉 不好意思 ..@@ 請問題庫哪邊可以找的到呢 可否透露一下 >< 快考試了 一直無方向
去GOOGLE搜尋一下其實都找的到 如果需要的話留個mail寄給你也可以
*****
*****
請問一下版主你會把151~244題貼完嗎?? 雖然我是有題庫,但是這樣答案有擋住看比較有效率 或者哪邊有類似這樣的東西可以看的
*****
*****
*****
版主您好: 請問您會將剩下的題目(151~244)解析貼完嗎?因為覺得您的解析詳細,整理得很清楚,對考照的人幫助很大,希望能繼續貼完~
非常感謝您, 非常詳盡的解說,我從這邊學到很多,謝謝!