SUN認證Java2程式設計師考試(SCJP) 試題解析(1)

  前言

無論你是個新手,還是程式設計方面的專家,你都會驚異於sun公司java的無窮魅力。java帶給你的並不僅僅是面向對象、開放、平台無關、易用、安全和“write once, run anywhere”等軟體開發方面的優勢,更重要的一點是,它提供了一種新穎的表達思想的方式,一種全新的思維模式。隨著待解決問題的規模不斷膨脹,java徹底的面向對象思想的靈活性就會凸現出來。毋庸置疑,java是你開發大型軟體時最得心應手的利器或是你轉行it的入門首選。

scjp考試簡介

● 考試方式
全英文試題,以電腦作答,在授權的prometric考試中心參加考試
考試編號:310-025
先決條件:無
考試題型:複選、填空和拖拽匹配
題量:59
及格標準:61%
時限:120分鐘
費用:1250元
● 要求具備的能力
● 使用java程式語言創建java應用程式和applets。
● 定義和描述垃圾蒐集,安全性和java虛擬機(jvm)。
● 描述和使用java語言面向對象的特點。
● 開發圖形用戶界面(gui)。利用java支持的多種布局管理。
● 描述和使用java的事件處理模式。
● 使用java語言的滑鼠輸入、文本、視窗和選單視窗部件。
● 使用java的例外處理來控制程式執行和定義用戶自己的例外事件。
● 使用java語言先進的面向對象特點, 包括方法重載、方法覆蓋、抽象類、接口、final、static和訪問控制。
● 實現檔案的輸入/輸出 (i/o)。
● 使用java語言內在的執行緒模式來控制多執行緒。
● 使用java 的sockets機制進行網路通信。

例題1:
choose the three valid identifiers from those listed below.
a. idolikethelongnameclass
b. $byte
c. const
d. _ok
e. 3_case
解答:a, b, d
點評:java中的標示符必須是字母、美元符($)或下劃線(_)開頭。關鍵字與保留字不能作為標示符。選項c中的const是java的保留字,所以不能作標示符。選項e中的3_case以數字開頭,違反了java的規則。

例題2:
how can you force garbage collection of an object?
a. garbage collection cannot be forced
b. call system.gc().
c. call system.gc(), passing in a reference to the object to be garbage collected.
d. call runtime.gc().
e. set all references to the object to new values(null, for example).
解答:a
點評:在java中垃圾收集是不能被強迫立即執行的。調用system.gc()或runtime.gc()靜態方法不能保證垃圾收集器的立即執行,因為,也許存在著更高優先權的執行緒。所以選項b、d不正確。選項c的錯誤在於,system.gc()方法是不接受參數的。選項e中的方法可以使對象在下次垃圾收集器運行時被收集。

例題3:
consider the following class:
1. class test(int i) {
2. void test(int i) {
3. system.out.println(“i am an int.”);
4. }
5. void test(string s) {
6. system.out.println(“i am a string.”);
7. }
8.
9. public static void main(string args[]) {
10. test t=new test();
11. char ch=“y”;
12. t.test(ch);
13. }
14. }
which of the statements below is true?(choose one.)
a. line 5 will not compile, because void methods cannot be overridden.
b. line 12 will not compile, because there is no version of test() that rakes a char argument.
c. the code will compile but will throw an exception at line 12.
d. the code will compile and produce the following output: i am an int.
e. the code will compile and produce the following output: i am a string.
解答:d
點評:在第12行,16位長的char型變數ch在編譯時會自動轉化為一個32位長的int型,並在運行時傳給void test(int i)方法。