JAVA認證歷年真題:SCJP認證套題解析[1]

1. which of the following range of short is correct?
a. -27 -- 27-1
b. 0 -- 216-1
c. ?215 -- 215-1
d. ?231 -- 231-1
翻譯 下面哪些是short型的取值範圍。
答案
c 解析
短整型的數據類型的長度是16 bits,有符號。另外需要說明的是java中所有的整(integral)數(包括byte,short,int,long)全是有符號的。
2. which declarations of identifiers are legal?
a. $persons
b. twousers
c. *point
d. this
e. _endline
翻譯 下面哪些是合法的標識符。
答案
a,b,e 解析
java的標識符可以以一個unicode字元,下滑線(_),美元符($)開始,後續字元可以是前面的符號和數字,沒有長度限制,大小寫敏感,不能是保留字。
3. which statement of assigning a long type variable to a hexadecimal value is correct?
a. long number = 345l;

b. long number = 0345;

c. long number = 0345l;

d. long number = 0x345l
翻譯 哪些是將一個十六進制值賦值給一個long型變數。
答案
d 解析

十六進制數以0x開頭,long型數以l(大小寫均可,一般使用大寫,因為小寫的l和數字1不易區分)。
4.which of the following fragments might cause errors?
a. string s = "gone with the wind";
string t = " good ";
string k = s + t;

b. string s = "gone with the wind";
string t;
t = s[3] + "one";

c. string s = "gone with the wind";
string standard = s.touppercase();

d. string s = "home directory";
string t = s - "directory";

翻譯 下面的哪些程式片斷可能導致錯誤。
答案b,d 解析
a:string類型可以直接使用+進行連線運算。

b:string是一種object,而不是簡單的字元數組,不能使用下標運算符取其值的某個元素,錯誤。

c:touppercase()方法是string對象的一個方法,作用是將字元串的內容全部轉換為大寫並返迴轉換後的結果(string類型)。

d:string類型不能進行減(-)運算,錯誤。

5. which are syntactically valid statement at// point x?
class person {
private int a;
public int change(int m){ return m; }
}
public class teacher extends person {
public int b;
public static void main(string arg[]){
person p = new person();
teacher t = new teacher();
int i;
// point x
}
}

a. i = m;

b. i = b;

c. i = p.a;

d. i = p.change(30);

e. i = t.b.
翻譯 在// point x處的哪些申明是句法上合法的。
答案d,e 解析
a:m沒有被申明過,不能使用。

b:雖然b是類teacher的public成員變數,但是在靜態方法中不能使用類中的非靜態成員。

c:a是類person的private成員,在類外不能直接引用。

d:change(int m)方法是public方法,並且返回一個int型值,可以通過類的實例變數p引用並賦值給一個int型變數。

e:b是類teacher的public成員變數,且是int型,可以通過類的實例變數t引用並賦值給一個int型變數。
6. which layout manager is used when the frame is resized the buttons´s position in the frame might be changed?
a. borderlayout

b. flowlayout

c. cardlayout

d. gridlayout
翻譯 當frame的大小被改變時frame中的按鈕的位置可能被改變時使用的哪一個布局管理器。
答案
b 解析
a:該布局管理器將容器劃分為五個部分,容器大小的改變不會影響其中的組件的位置而是影響他們的大小。

b:該布局管理器根據放入其中的組件的最合適大小調整組件的位置,根據組件放入的順序安排,一行不能容納時放入下一行,因此容器的大小改變可能改變組件的位置。

c:該布局管理器顯示放入該容器的當前頁中的組件,一次顯示一個,容器大小的改變不能影響其中組件的位置。
d:該布局管理器將容器劃分為固定的格線,組件加入後占據一個單元,各組件的相對位置不會因為容器的大小變化而變化,改變的只是組件的大小。

7. given the following code fragment:

1) public void create() {

2) vector myvect;

3) myvect = new vector();

4) }

which of the following statements are true?
a. the declaration on line 2 does not allocate memory space for the variable myvect.

b. the declaration on line 2 allocates memory space for a reference to a vector object.

c. the statement on line 2 creates an object of class vector.

d. the statement on line 3 creates an object of class vector.

e. the statement on line 3 allocates memory space for an object of class vector
翻譯
給出下面的代碼片斷。。。下面的哪些陳述為true(真)?
a. 第二行的聲明不會為變數myvect分配記憶體空間。
b. 第二行的聲明分配一個到vector對象的引用的記憶體空間。
c. 第二行語句創建一個vector類對象。
d. 第三行語句創建一個vector類對象。
e. 第三行語句為一個vector類對象分配記憶體空間。
答案a,d,e 解析

sl-275中指出:要為一個新對象分配空間必須執行new xxx()調用,new調用執行以下 的操作:

1. 為新對象分配空間並將其成員初始化為0或者null。

2. 執行類體中的初始化。(例如在類中有一個成員聲明int a=10;在第一步後a=0 ,執行到第二步後a=10)

3. 執行構造函式。

4. 變數被分配為一個到記憶體堆中的新對象的引用。
8. which of the following answer is correct to express the value 8 in octal number?
a. 010

b. 0x10

c. 08

d. 0x8
翻譯
下面的哪些答案可以用以表示八進制值8。
答案
a 解析
八進制值以0開頭,以0x開頭的為十六進制值,八進制中不能出現數字8,最大只有7。
9. which are not java keywords?
a. true

b. sizeof

c. const

d. super

e. void
翻譯
哪些不是java關鍵字。
答案a,b 解析
a: 不是,java中有true,但是這也不是關鍵字而是字面量(literal)。

b: 不是,java中不需要這個操作符,所有的類型(原始類型)的大小都是固定的。

c、d、e都是,需要說明的是const是java中未被使用的關鍵字。
10. which of the following statements are true?
a. the equals() method determines if reference values refer to the same object.

b. the == operator determines if the contents and type of two separate objects match.

c. the equals() method returns true only when the contents of two objects match.

d. the class file overrides equals() to return true if the contents and type of two separate objects match.
翻譯
下面的哪些敘述為真。a. equals()方法判定引用值是否指向同一對象。

b. == 操作符判定兩個分立的對象的內容和類型是否一致。

c. equals()方法只有在兩個對象的內容一致時返回true。

d. 類file重寫方法equals()在兩個分立的對象的內容和類型一致時返回true。
答案a,d 解析
嚴格來說這個問題的答案是不確定的,因為equals()方法是可以被重載的,但是按照java語言的本意來說:如果沒有重寫(override)新類的equals(),則該方法和 == 操作符一樣在兩個變數指向同一對象時返回真,但是java推薦的是使用equals()方法來判斷兩個對象的內容是否一樣,就像string類的equals()方法所做的那樣:判定兩個string對象的內容是否相同,而==操作符返回true的唯一條件是兩個變數指向同一對象。從這個意義上來說選擇給定的答案。從更嚴格的意義來說正確答案應該只有d。

11. which statements about inheritance are true?
a. in java programming language only allows single inheritance.

b. in java programming language allows a class to implement only one
interface.

c. in java programming language a class cannot extend a class and implement
a interface together.

d. in java programming language single inheritance makes code more
reliable.
翻譯
下面關於繼承的哪些敘述是正確的。

a. 在java中只允許單一繼承。

b. 在java中一個類只能實現一個接口。

c. 在java中一個類不能同時繼承一個類和實現一個接口。

d. java的單一繼承使代碼更可靠。
答案a,d 解析
在java中一個類只能有一個直接父類,但是可以實現多個接口,在繼承的同時可以實現接口,之所以取消多繼承的原因是多繼承使得代碼產生很多問題,而使用單一繼承則可以使代碼更可靠。
12.
1) class person {

2) public void printvalue(int i, int j) {/*…*/ }

3) public void printvalue(int i){/*...*/ }

4) }

5) public class teacher extends person {

6) public void printvalue() {/*...*/ }

7) public void printvalue(int i) {/*...*/}

8) public static void main(string args[]){

9) person t = new teacher();

10) t.printvalue(10);

11) }

12) }

which method will the statement on line 10 call?
a. on line 2

b. on line 3

c. on line 6

d. on line 7
翻譯
第十行的聲明將調用哪些方法。
答案
d 解析
變數t是一個person對象,但是它是用teacher實例化的,這個問題涉及到java的編譯時多態和運行時多態的問題,就編譯時多態來說,t實際上是一個person類,這涉及到類型的自動轉換(將一個子類的實例賦值給一個父類的變數是不用進行強制類型轉換,反之則需要進行強制類型轉換,而且被賦值的變數實際上應該是一個子類的對象),如果對t調用了子類中新增的方法則造成編譯時錯誤編譯將不能通過,而在運行時,運行時系統將根據t實際指向的類型調用對應的方法,對於本例來說,t.print(10)將調用t實際指向的teacher類的對應方法。在java中,可以用一個子類的實例實例化父類的一個變數,而變數在編譯時是一個父類實例,在運行時可能是一個子類實例。

13. which are not java primitive types?
a. short

b. boolean

c. unit

d. float
翻譯
下面哪些不是java的原始數據類型。
答案b,c 解析
java的原始數據類型一共就八個,分別是:byte,short,int,long,boolean,char,float,double。注意這些是大小寫敏感的,而boolean是booelan的封裝類(wrapper class)。
14. use the operators "<<", ">>", which statements are true?
a. 0000 0100 0000 0000 0000 0000 0000 0000<<5 gives

1000 0000 0000 0000 0000 0000 0000 0000

b. 0000 0100 0000 0000 0000 0000 0000 0000<<5 gives

1111 1100 0000 0000 0000 0000 0000 0000

c. 1100 0000 0000 0000 0000 0000 0000 0000>>5 gives

1111 1110 0000 0000 0000 0000 0000 0000

d. 1100 0000 0000 0000 0000 0000 0000 0000>>5 gives

0000 0110 0000 0000 0000 0000 0000 0000
翻譯
使用"<<"和 ">>"操作符的哪些陳述是對的。

答案a,c 解析
java的移位操作符一共有三種,分別是”>>”,”>>>”,”<<”,執行的操作分別是有符號右移,無符號右移,左移,有符號右移的意思是說移入的最高位和原最高符號位相同,無符號右移是移入位始終補零,左移時最低位始終補零,最高位被捨棄。移位操作符另一個非常值得注意的特點是其右運算元是取模運算的,意思是說對於一個int型數據而言,對它移位32位的結果是保持不變而非變成零,即:a>>32的結果是a而不是0,同理,對long型數是對右運算元取64的模,a>>64==a;還有一點需要注意的是移位操作符”>>>”只對int型和long型有效,對byte或者short的操作將導致自動類型轉換,而且是帶符號的。
15. which of the following range of int is correct?
a. -27 -- 27-1

b. 0 -- 232-1

c. ?215 -- 215-1

d. ?231 -- 231-1
翻譯
int的取值範圍是哪個。
答案
d 解析
int型是32位的。參看第一題的論述。
16. which keyword should be used to enable interaction with the lock of an
object? the flag allows exclusive access to that object.
a. transient

b. synchronized

c. serialize

d. static
翻譯
下面的哪些關鍵字通常用來對對象的加鎖,該標記使得對對象的訪問是排他的
答案
b 解析
由於java是多執行緒的語言,多個執行緒可以”同時”訪問同一數據區,而在處理某些數據時不希望其它的執行緒修改那些數據的值或者某些操作是不可打斷的,要做到這個,可以使用synchronized關鍵字聲明這一點。

17. which is the return type of the method main()?
a. int

b. void

c. boolean

d. static
翻譯
main()方法的返回類型是什麼?
答案
b 解析
在java中,程式運行的入口就是main()方法,它必須是這樣的形式:public static void main(string args[])。但是嚴格來講這個題目的答案還可以加上a和c,因為並沒有限定是程式入口的main()方法,而main()方法是可以重載的。一般意義上的main()當然就是指我們剛開始所說的main()方法了。
18. given the following code:

if (x>0) { system.out.println("first"); }

else if (x>-3) { system.out.println("second"); }

else { system.out.println("third"); }

which range of x value would print the string "second"?
a. x > 0

b. x > -3

c. x <= -3

d. x <= 0 & x > -3
翻譯
給出下面的代碼:

x的取值在什麼範圍內時將列印字元串"second"。
答案
d 解析
x>0時列印"first",x>-3&&x<=0時列印"second",x<=-3時列印"third"。

這個題目沒有什麼難的,只要理解if語句的語法就可以了。
19. given the following expression about textfield which use a proportional
pitch font.

textfield t = new textfield("they are good",40);

which statement is true?
a. the displayed string can use multiple fonts.

b. the maximum number of characters in a line will be 40.

c. the displayed width is exactly 40 characters.

d. the user can edit the characters.
翻譯
給出以下關於一個使用適當的字元間距的字型的textfield的表達式。

哪些敘述是對的?

a. 被顯示的字元串可以使用多種字型。

b. 一行中最大的字元數是40

c. 顯示的寬度正好是40個字元寬。

d. 用戶可以編輯字元。
答案
d 解析
對於textfield的該種形式的構造函式來說,前一個參數是文本域中初始的字元串的顯示值,而後一個是推薦的顯示寬度,以列數表示,在構造文本域的時候會將這個大小設定為最佳大小,如果容器的限制使得文本域不能顯示這么多也沒有辦法,一般來說是比這個大小大的,而且即使寬度很小,你也可以在文本域的一行中輸入很長的字元串,只有你不使用回車,在超過顯示寬度後文本域會自動出現水平滾動條(沒有被設定為關閉,預設是不關閉的),而文本域的預設編輯方式是可編輯的,一個文本域只能使用一種字型,這個字型可以在運行的過程中動態的改變,但是文本域中的所有字元串都將使用這個字型顯示。
20. which statements about the garbage collection are true?
a. the program developer must create a thread to be responsible for free
the memory.

b. the garbage collection will check for and free memory no longer needed.

c. the garbage collection allow the program developer to explicity and
immediately free the memory.

d. the garbage collection can free the memory used java object at expect
time.
翻譯
關於垃圾收集的哪些敘述是對的。

a. 程式開發者必須自己創建一個執行緒進行記憶體釋放的工作。

b. 垃圾收集將檢查並釋放不再使用的記憶體。

c. 垃圾收集允許程式開發者明確指定並立即釋放該記憶體。

d. 垃圾收集能夠在期望的時間釋放被java對象使用的記憶體。
答案
b 解析
java語言將記憶體分配和釋放的工組交給了自己,程式設計師不必做這些工作,它提供一個系統級的執行緒跟蹤每個記憶體的分配,在jvm的空閒處理中,垃圾收集執行緒將檢查和釋放不再使用的記憶體(即可以被釋放的記憶體)。垃圾收集的過程在java程式的生存期中是自動的,不需要分配和釋放記憶體,也避免了記憶體泄漏。可以調用system.gc()方法建議(suggest)jvm執行垃圾收集以使得可被釋放的記憶體能立即被使用,當此方法返回的時候,jvm已經做了最大的努力從被丟棄的對象上回收記憶體空間。程式設計師不能指定收集哪些記憶體,一般而言也不用關心這個問題,除非是程式的記憶體消耗很大,特別是有很多臨時對象時可以“建議“進行垃圾收集以提高可用記憶體。需要指出的是調用system.gc()方法不能保證jvm立即進行垃圾收集,而只能是建議,因為垃圾收集執行緒的優先權很低(通常是最低的)。