例題1:
Choose the three valid identifiers from those listed below.
A. IDoLikeTheLongNameClass
B. $byte
C. const
D. _ok
E. 3_case
解答:A, B, D
點(diǎn)評:Java中的標(biāo)示符必須是字母、美元符($)或下劃線(_)開頭。關(guān)鍵字與保留字不能作為標(biāo)示符。選項(xiàng)C中的const是Java的保留字,所以不能作標(biāo)示符。選項(xiàng)E中的3_case以數(shù)字開頭,違反了Java的規(guī)則。
例題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
點(diǎn)評:在Java中垃圾收集是不能被強(qiáng)迫立即執(zhí)行的。調(diào)用System.gc()或Runtime.gc()靜態(tài)方法不能保證垃圾收集器的立即執(zhí)行,因?yàn),也許存在著更高優(yōu)先級的線程。所以選項(xiàng)B、D不正確。選項(xiàng)C的錯誤在于,System.gc()方法是不接受參數(shù)的。選項(xiàng)E中的方法可以使對象在下次垃圾收集器運(yùn)行時被收集。
例題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
點(diǎn)評:在第12行,16位長的char型變量ch在編譯時會自動轉(zhuǎn)化為一個32位長的int型,并在運(yùn)行時傳給void test(int i)方法。
例題4:
Which of the following lines of code will compile without error?
A. int i=0;
if (i) {
System.out.println(“
Hi”);
}
B.
boolean b=true;
boolean b2=true;
if(b==b2) {
System.out.println(“So true”);
}
C.
int i=1;
int j=2;
if(i==1|| j==2)
System.out.println(“OK”);
D.
int i=1;
int j=2;
if (i==1 &| j==2)
System.out.println(“OK”);
解答:B, C
點(diǎn)評:選項(xiàng)A錯,因?yàn)閕f語句后需要一個boolean類型的表達(dá)式。邏輯操作有^、&、| 和 &&、||,但是“&|”是非法的,所以選項(xiàng)D不正確。
例題5:
Which two demonstrate a "has a" relationship? (Choose two)
A. public interface Person { }
public class Employee extends Person{ }
B. public interface Shape { }
public interface Rectandle extends Shape { }
C. public interface Colorable { }
public class Shape implements Colorable
{ }
D. public class Species{ }
public class Animal{private Species species;}
E. interface Component{ }
class Container implements Component{
private Component[] children;
}
解答:D, E
點(diǎn)評: 在Java中代碼重用有兩種可能的方式,即組合(“has a”關(guān)系)和繼承(“is a”關(guān)系)!癶as a”關(guān)系是通過定義類的屬性的方式實(shí)現(xiàn)的;而“is a”關(guān)系是通過類繼承實(shí)現(xiàn)的。本例中選項(xiàng)A、B、C體現(xiàn)了“is a”關(guān)系;選項(xiàng)D、E體現(xiàn)了“has a”關(guān)系。
北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |