第116題:

Given:

11. public class Commander{
12.     public static void main(String[] args){
13.         String myProp = /* insert code here */
14.         System.out.println(myProp);

15.     }
16. }
and the command line:

java -Dprop.custom=gobstopper Commander

Which two, placed on line 13, will produce the output gobstopper? (Choose two.)

A. System.load("prop.custom");

B. System.getenv("prop.custom");

C. System.property("prop.custom);

D. System.getProperty("prop.custom");

E. System.getProperties().getProperty("prop.custom");

答案:DE

題目範圍:System下的方法

解析:

System.load方法可以傳入一個檔案位址,讓程式將指定檔案的內容加入函式庫中

System.getenv方法會回傳對應輸入的環境變數,例如你可以輸入"JAVA_HOME"

System.property,沒有這個方法

System.getProperty會回傳在執行檔案時所定義的屬性

System.getProperties會回傳一個hashtable裡面包含很多系統資訊,詳細請上網搜尋

 

第117題:

Given:

11. class Converter{
12.     public static void main(String[] args){
13.         Integer i = args[0];
14.         int j = 12;
15.         System.out.println("It is " + (j==i) + " that j==i.");
16.     }
17. }

What is the result when the programmer attempts to compile the code and run it with the command

java Converter 12?

A. It is true that j==i.

B. It is false that j==i.

C. An exception is thrown at runtime.

D. Compilation fails because of an error in line 13.

答案:

題目範圍:基本觀念

解析:

傳入的資料args型態是字串的陣列,必須經過轉換才能變成數字

Integer i = Integer.valueOf(args[0]);

 

第118題:

Given:

10. class Foo{
11.     static void alpha(){/* more code here */}
12.     void beta() {/* more code here */)
13. }

Which two statements are true? (Choose two.)

A. Foo.beta() is a valid invocation of beta().

B. Foo.alpha() is a valid invocation of alpha().

C. Method beta() can directly call method alpha().

D. Method alpha() can directly call method beta().

答案:BC

題目範圍:靜態方法

解析:

靜態方法可以直接指明被呼叫如:Foo.alpha()

一般方法就要建立實體才能呼叫如:new Foo().beta()

靜態方法中不可以呼叫一般方法;一般方法可以呼叫靜態方法

 

第119題:

Given:

11. public static void main(String[] args){
12.     String str = "null";
13.     if (str == null) {
14.         System.out.println("null");
15.     }else (str.length() == 0) {
16.         System.out.println("zero");
17.     }else{
18.         System.out.println("some");
19.     }
20. }

What is the result?

A. null

B. zero

C. some

D. Compilation fails.

E. An exception is thrown at runtime.

答案:

題目範圍:基本觀念

解析:

這題粗心的話可是會被騙…

第二個else後面少了if,所以錯了

如果修正的話,結果會是some

 

第120題:

Given:

1. public class GC{
2.     private Object o;
3.     private void doSomethingElse(Object obj){o = obj;}
4.     public void doSomething(){
5.         Object o = new Object();
6.         doSomethingElse(o);
7.         o = new Object();
8.         doSomethingElse(null);
9.         o = null;
10.     }
11. }

When the doSomething method is called, after which line does the Object created in line 5 become available for garbage collection?

A. Line 5

B. Line 6

C. Line 7

D. Line 8

E. Line 9

F. Line 10

答案:

題目範圍:物件與指標觀念

解析:

這提是指標的觀念

第5行執行後生出了一個物件,下面用*代表這個物件,方法中的o→*

第6行,doSomethingElseobj→*然後類別中的o→*

    doSomethingElse方法執行完之後,obj就不再被使用

第7行,新生出一個物件,下面用⊕代表這個新的物件,方法中的o→⊕

第8行,doSomethingElseobjnull然後類別中的onull

    到目前為止,終於沒有任何一個指標指向*,*被列入回收清單

第9行,方法的onull沒有任何一個指標指向⊕,⊕被列入回收清單

第10行,沒有動作

arrow
arrow
    全站熱搜

    yaya741228 發表在 痞客邦 留言(0) 人氣()