盡信書,不如無書

 

 

 

第九十六題:

Given a class Repetition:

package utils;

public class Repetition {
    public static String twice(String s) {
        return s + s;
    }
}
and given another class Demo:

// insert code here

public class Demo {
    public static void main(String[] args) {
        System.out.println(twice("pizza"));
    }
}

Which code should be inserted at line 1 of Demo.java to compile and run Demo to print "pizzapizza"?

A. import utils.*;

B. static import utils.*;

C. import utils.Repetition.*;

D. static import utils.Repetition.*;

E. import utils.Repetition.twice();

F. import static utils.Repetition.twice;

G. static import utils.Repetition.twice;

答案:

題目範圍:import 匯入類別

解析:

例子中直接呼叫twice方法,沒有實體化Repetition類別

因此必須用到靜態匯入,讓twice方法可以直接使用

 

第九十七題:

Which statement is true?

A. A class's finalize() method CANNOT be invoked explicitly.

B. super.finalize() is called implicitly by any overriding finalize() method.

C. The finalize() method for a given object is called no more than once by the garbage collector.

D. The order in which finalize() is called on two objects is based on the order in which the two objects became finalizable.

答案:

題目範圍:資源回收(garbage collection)

解析:

A:finalize()可以被明確的呼叫,不過需要擲出例外或以try/catch包圍

B:沒有這個動作

D:沒有一定順序

 

第九十八題:

Given:

public class Yippee {
    public static void main(String[] args) {
        for (int x = 1; x < args.length; x++) {
            System.out.print(args[x] + " ");
        }
    }
}

and two separate command line invocations:

java Yippee

java Yippee 1 2 3 4

What is the result?

答案:

題目範圍:基本觀念

解析:

傳入參數1 2 3 4main方法

然後再從第二個開始印到最後一個,結果當然是2 3 4

 

第九十九題:

Given:

3.     interface Animal{void makeNoise();}
4.     class Horse implements Animal{
5.         Long weight = 1200L;
6.         public void makeNoise(){System.out.println("whinny");}
7.     }
8.     public class lcelandic extends Horse{
9.         public void makeNoise(){System.out.println("vinny");}
10.         public static void main(String[] args) {
11.             Icelandic i1 = new lcelandic();
12.             Icelandic i2 = new lcelandic();
13.             Icelandic i3 = new lcelandic();
14.             i3 = i1; i1 = i2; i2 = null; i3 = i1;
15.         }
16.     }

When line 14 is reached, how many objects are eligible for the garbage collector?

A. 0

B. 1

C. 2

D. 3

E. 4

F. 6

答案:

題目範圍:資源回收

解析:

這題的答案我再三確認,不會有錯

14行的時候會有兩個Icelandic的物件會要被回收,這是肯定的

然而Icelandic物件中的weight是否要被回收呢?

如果weightLong類別(注意L是大寫的,它代表它是longwrapperClass),那麼他也算是一個物件

如果weightlong類別(注意L是小寫的,它代表它是基本資料型態),那麼他就不算是物件

有注意到這點的話,就很容易知道應該是4個物件要被回收

台灣版本的答案跟美國版本的答案不同,而台灣版本的C應該是錯的;美國版本的E應該才是正解

 

第一百題:

Given:

10. public class SuperCalc{
11.     protected static int multiply(int a, int b){return a * b;}
12. }
and:

20. public class SubCalc extends SuperCalc{
21.     public static int multiply(int a, int b){
22.         int c = super.multiply(a, b);
23.         return c;
24.     }
25. }
and:

30.     SubCalc sc = new SubCalc();
31.    System.out.println(sc.multiply(3, 4));
32.    System.out.println(SubCalc.multiply(2, 2));

What is the result?

A. 12

 4

B. The code runs with no output.

C. An exception is thrown at runtime.

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

E. Compilation fails because of an error in line 22.

F. Compilation fails because of an error in line 31.

答案:

題目範圍:物件觀念

解析:

static的方法中不可以用thissuper

因為static的方法是程式開始運作就存在,可以被呼叫的

然而無法確保static的方法所在的物件是否有被實體化

因此在static的方法中使用superthis是不被允許的

 

arrow
arrow
    全站熱搜

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