a. -27 -- 27-1
b. 0 -- 216-1
c. ?215 -- 215-1
d. ?231 -- 231-1
翻譯 下面哪些是short型的取值范圍。
答案
c 解析
短整型的數(shù)據(jù)類型的長度是16 bits,有符號(hào)。另外需要說明的是java中所有的整(integral)數(shù)(包括byte,short,int,long)全是有符號(hào)的。
2. which declarations of identifiers are legal?
a. $persons
b. twousers
c. *point
d. this
e. _endline
翻譯 下面哪些是合法的標(biāo)識(shí)符。
答案
a,b,e 解析
java的標(biāo)識(shí)符可以以一個(gè)unicode字符,下滑線(_),美元符($)開始,后續(xù)字符可以是前面的符號(hào)和數(shù)字,沒有長度限制,大小寫敏感,不能是保留字。
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
翻譯 哪些是將一個(gè)十六進(jìn)制值賦值給一個(gè)long型變量。
答案
d 解析
十六進(jìn)制數(shù)以0x開頭,long型數(shù)以l(大小寫均可,一般使用大寫,因?yàn)樾懙膌和數(shù)字1不易區(qū)分)。
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";
翻譯 下面的哪些程序片斷可能導(dǎo)致錯(cuò)誤。
答案b,d 解析
a:string類型可以直接使用+進(jìn)行連接運(yùn)算。
b:string是一種object,而不是簡(jiǎn)單的字符數(shù)組,不能使用下標(biāo)運(yùn)算符取其值的某個(gè)元素,錯(cuò)誤。
c:touppercase()方法是string對(duì)象的一個(gè)方法,作用是將字符串的內(nèi)容全部轉(zhuǎn)換為大寫并返回轉(zhuǎn)換后的結(jié)果(string類型)。
d:string類型不能進(jìn)行減(-)運(yùn)算,錯(cuò)誤。
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成員變量,但是在靜態(tài)方法中不能使用類中的非靜態(tài)成員。
c:a是類person的private成員,在類外不能直接引用。
d:change(int m)方法是public方法,并且返回一個(gè)int型值,可以通過類的實(shí)例變量p引用并賦值給一個(gè)int型變量。
e:b是類teacher的public成員變量,且是int型,可以通過類的實(shí)例變量t引用并賦值給一個(gè)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
翻譯 當(dāng)frame的大小被改變時(shí)frame中的按鈕的位置可能被改變時(shí)使用的哪一個(gè)布局管理器。
答案
b 解析
a:該布局管理器將容器劃分為五個(gè)部分,容器大小的改變不會(huì)影響其中的組件的位置而是影響他們的大小。
b:該布局管理器根據(jù)放入其中的組件的最合適大小調(diào)整組件的位置,根據(jù)組件放入的順序安排,一行不能容納時(shí)放入下一行,因此容器的大小改變可能改變組件的位置。
c:該布局管理器顯示放入該容器的當(dāng)前頁中的組件,一次顯示一個(gè),容器大小的改變不能影響其中組件的位置。
d:該布局管理器將容器劃分為固定的網(wǎng)格,組件加入后占據(jù)一個(gè)單元,各組件的相對(duì)位置不會(huì)因?yàn)槿萜鞯拇笮∽兓兓淖兊闹皇墙M件的大小。
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. 第二行的聲明不會(huì)為變量myvect分配內(nèi)存空間。
b. 第二行的聲明分配一個(gè)到vector對(duì)象的引用的內(nèi)存空間。
c. 第二行語句創(chuàng)建一個(gè)vector類對(duì)象。
d. 第三行語句創(chuàng)建一個(gè)vector類對(duì)象。
e. 第三行語句為一個(gè)vector類對(duì)象分配內(nèi)存空間。
答案a,d,e 解析
sl-275中指出:要為一個(gè)新對(duì)象分配空間必須執(zhí)行new ()調(diào)用,new調(diào)用執(zhí)行以下 的操作:
1. 為新對(duì)象分配空間并將其成員初始化為0或者null。
2. 執(zhí)行類體中的初始化。(例如在類中有一個(gè)成員聲明int a=10;在第一步后a=0 ,執(zhí)行到第二步后a=10)
3. 執(zhí)行構(gòu)造函數(shù)。
4. 變量被分配為一個(gè)到內(nèi)存堆中的新對(duì)象的引用。
8. which of the following answer is correct to express the value 8 in octal number?
a. 010
b. 0x10
c. 08
d. 0x8
翻譯
下面的哪些答案可以用以表示八進(jìn)制值8。
答案
a 解析
八進(jìn)制值以0開頭,以0x開頭的為十六進(jìn)制值,八進(jìn)制中不能出現(xiàn)數(shù)字8,最大只有7。
9. which are not java keywords?
a. true
b. sizeof
c. const
d. super
e. void
翻譯
哪些不是java關(guān)鍵字。
答案a,b 解析
a: 不是,java中有true,但是這也不是關(guān)鍵字而是字面量(literal)。
b: 不是,java中不需要這個(gè)操作符,所有的類型(原始類型)的大小都是固定的。
c、d、e都是,需要說明的是const是java中未被使用的關(guān)鍵字。
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()方法判定引用值是否指向同一對(duì)象。
b. == 操作符判定兩個(gè)分立的對(duì)象的內(nèi)容和類型是否一致。
c. equals()方法只有在兩個(gè)對(duì)象的內(nèi)容一致時(shí)返回true。
d. 類file重寫方法equals()在兩個(gè)分立的對(duì)象的內(nèi)容和類型一致時(shí)返回true。
答案a,d 解析
嚴(yán)格來說這個(gè)問題的答案是不確定的,因?yàn)閑quals()方法是可以被重載的,但是按照java語言的本意來說:如果沒有重寫(override)新類的equals(),則該方法和 == 操作符一樣在兩個(gè)變量指向同一對(duì)象時(shí)返回真,但是java推薦的是使用equals()方法來判斷兩個(gè)對(duì)象的內(nèi)容是否一樣,就像string類的equals()方法所做的那樣:判定兩個(gè)string對(duì)象的內(nèi)容是否相同,而==操作符返回true的唯一條件是兩個(gè)變量指向同一對(duì)象。從這個(gè)意義上來說選擇給定的答案。從更嚴(yán)格的意義來說正確答案應(yīng)該只有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.
翻譯
下面關(guān)于繼承的哪些敘述是正確的。
a. 在java中只允許單一繼承。
b. 在java中一個(gè)類只能實(shí)現(xiàn)一個(gè)接口。
c. 在java中一個(gè)類不能同時(shí)繼承一個(gè)類和實(shí)現(xiàn)一個(gè)接口。
d. java的單一繼承使代碼更可靠。
答案a,d 解析
在java中一個(gè)類只能有一個(gè)直接父類,但是可以實(shí)現(xiàn)多個(gè)接口,在繼承的同時(shí)可以實(shí)現(xiàn)接口,之所以取消多繼承的原因是多繼承使得代碼產(chǎn)生很多問題,而使用單一繼承則可以使代碼更可靠。
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
翻譯
第十行的聲明將調(diào)用哪些方法。
答案
d 解析
變量t是一個(gè)person對(duì)象,但是它是用teacher實(shí)例化的,這個(gè)問題涉及到j(luò)ava的編譯時(shí)多態(tài)和運(yùn)行時(shí)多態(tài)的問題,就編譯時(shí)多態(tài)來說,t實(shí)際上是一個(gè)person類,這涉及到類型的自動(dòng)轉(zhuǎn)換(將一個(gè)子類的實(shí)例賦值給一個(gè)父類的變量是不用進(jìn)行強(qiáng)制類型轉(zhuǎn)換,反之則需要進(jìn)行強(qiáng)制類型轉(zhuǎn)換,而且被賦值的變量實(shí)際上應(yīng)該是一個(gè)子類的對(duì)象),如果對(duì)t調(diào)用了子類中新增的方法則造成編譯時(shí)錯(cuò)誤編譯將不能通過,而在運(yùn)行時(shí),運(yùn)行時(shí)系統(tǒng)將根據(jù)t實(shí)際指向的類型調(diào)用對(duì)應(yīng)的方法,對(duì)于本例來說,t.print(10)將調(diào)用t實(shí)際指向的teacher類的對(duì)應(yīng)方法。在java中,可以用一個(gè)子類的實(shí)例實(shí)例化父類的一個(gè)變量,而變量在編譯時(shí)是一個(gè)父類實(shí)例,在運(yùn)行時(shí)可能是一個(gè)子類實(shí)例。
13. which are not java primitive types?
a. short
b. boolean
c. unit
d. float
翻譯
下面哪些不是java的原始數(shù)據(jù)類型。
答案b,c 解析
java的原始數(shù)據(jù)類型一共就八個(gè),分別是: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
翻譯
使用"<<"和 ">>"操作符的哪些陳述是對(duì)的。
答案a,c 解析
java的移位操作符一共有三種,分別是”>>”,”>>>”,”<<”,執(zhí)行的操作分別是有符號(hào)右移,無符號(hào)右移,左移,有符號(hào)右移的意思是說移入的最高位和原最高符號(hào)位相同,無符號(hào)右移是移入位始終補(bǔ)零,左移時(shí)最低位始終補(bǔ)零,最高位被舍棄。移位操作符另一個(gè)非常值得注意的特點(diǎn)是其右操作數(shù)是取模運(yùn)算的,意思是說對(duì)于一個(gè)int型數(shù)據(jù)而言,對(duì)它移位32位的結(jié)果是保持不變而非變成零,即:a>>32的結(jié)果是a而不是0,同理,對(duì)long型數(shù)是對(duì)右操作數(shù)取64的模,a>>64==a;還有一點(diǎn)需要注意的是移位操作符”>>>”只對(duì)int型和long型有效,對(duì)byte或者short的操作將導(dǎo)致自動(dòng)類型轉(zhuǎn)換,而且是帶符號(hào)的。
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的取值范圍是哪個(gè)。
答案
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
翻譯
下面的哪些關(guān)鍵字通常用來對(duì)對(duì)象的加鎖,該標(biāo)記使得對(duì)對(duì)象的訪問是排他的
答案
b 解析
由于java是多線程的語言,多個(gè)線程可以”同時(shí)”訪問同一數(shù)據(jù)區(qū),而在處理某些數(shù)據(jù)時(shí)不希望其它的線程修改那些數(shù)據(jù)的值或者某些操作是不可打斷的,要做到這個(gè),可以使用synchronized關(guān)鍵字聲明這一點(diǎn)。
17. which is the return type of the method main()?
a. int
b. void
c. boolean
d. static
翻譯
main()方法的返回類型是什么?
答案
b 解析
在java中,程序運(yùn)行的入口就是main()方法,它必須是這樣的形式:public static void main(string args[])。但是嚴(yán)格來講這個(gè)題目的答案還可以加上a和c,因?yàn)椴]有限定是程序入口的main()方法,而main()方法是可以重載的。一般意義上的main()當(dāng)然就是指我們剛開始所說的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的取值在什么范圍內(nèi)時(shí)將打印字符串"second"。
答案
d 解析
x>0時(shí)打印"first",x>-3&&x<=0時(shí)打印"second",x<=-3時(shí)打印"third"。
這個(gè)題目沒有什么難的,只要理解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.
翻譯
給出以下關(guān)于一個(gè)使用適當(dāng)?shù)淖址g距的字體的textfield的表達(dá)式。
…
哪些敘述是對(duì)的?
a. 被顯示的字符串可以使用多種字體。
b. 一行中最大的字符數(shù)是40
c. 顯示的寬度正好是40個(gè)字符寬。
d. 用戶可以編輯字符。
答案
d 解析
對(duì)于textfield的該種形式的構(gòu)造函數(shù)來說,前一個(gè)參數(shù)是文本域中初始的字符串的顯示值,而后一個(gè)是推薦的顯示寬度,以列數(shù)表示,在構(gòu)造文本域的時(shí)候會(huì)將這個(gè)大小設(shè)置為最佳大小,如果容器的限制使得文本域不能顯示這么多也沒有辦法,一般來說是比這個(gè)大小大的,而且即使寬度很小,你也可以在文本域的一行中輸入很長的字符串,只有你不使用回車,在超過顯示寬度后文本域會(huì)自動(dòng)出現(xiàn)水平滾動(dòng)條(沒有被設(shè)置為關(guān)閉,缺省是不關(guān)閉的),而文本域的缺省編輯方式是可編輯的,一個(gè)文本域只能使用一種字體,這個(gè)字體可以在運(yùn)行的過程中動(dòng)態(tài)的改變,但是文本域中的所有字符串都將使用這個(gè)字體顯示。
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.
翻譯
關(guān)于垃圾收集的哪些敘述是對(duì)的。
a. 程序開發(fā)者必須自己創(chuàng)建一個(gè)線程進(jìn)行內(nèi)存釋放的工作。
b. 垃圾收集將檢查并釋放不再使用的內(nèi)存。
c. 垃圾收集允許程序開發(fā)者明確指定并立即釋放該內(nèi)存。
d. 垃圾收集能夠在期望的時(shí)間釋放被java對(duì)象使用的內(nèi)存。
答案
b 解析
java語言將內(nèi)存分配和釋放的工組交給了自己,程序員不必做這些工作,它提供一個(gè)系統(tǒng)級(jí)的線程跟蹤每個(gè)內(nèi)存的分配,在jvm的空閑處理中,垃圾收集線程將檢查和釋放不再使用的內(nèi)存(即可以被釋放的內(nèi)存)。垃圾收集的過程在java程序的生存期中是自動(dòng)的,不需要分配和釋放內(nèi)存,也避免了內(nèi)存泄漏。可以調(diào)用system.gc()方法建議(suggest)jvm執(zhí)行垃圾收集以使得可被釋放的內(nèi)存能立即被使用,當(dāng)此方法返回的時(shí)候,jvm已經(jīng)做了最大的努力從被丟棄的對(duì)象上回收內(nèi)存空間。程序員不能指定收集哪些內(nèi)存,一般而言也不用關(guān)心這個(gè)問題,除非是程序的內(nèi)存消耗很大,特別是有很多臨時(shí)對(duì)象時(shí)可以“建議“進(jìn)行垃圾收集以提高可用內(nèi)存。需要指出的是調(diào)用system.gc()方法不能保證jvm立即進(jìn)行垃圾收集,而只能是建議,因?yàn)槔占程的優(yōu)先級(jí)很低(通常是最低的)。