第101題:

Given:

package com.sun.scjp;

public class Geodetics {
    public static final double DIAMETER = 12756.32; // kilometers
}

Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.)

A.
import com.sun.scjp.Geodetics;

public class TerraCarta {
    public double halfway() {
        return Geodetics.DIAMETER / 2.0;
}

B.
import static com.sun.scjp.Geodetics;

public class TerraCarta {
    public double halfway() {
        return DIAMETER / 2.0;
    }
}

C.
import static com.sun.scjp.Geodetics.*;

public class TerraCarta {
    public double halfway() {
        return DIAMETER / 2.0;
    }
}

D.
package com.sun.scjp;

public class TerraCarta {
    public double halfway() {
        return DIAMETER / 2.0;
    }
}

答案:AC

題目範圍:import 匯入、import 靜態成員

解析:

A是一般的匯入做法

B和C選項只差了一個".*"而已,因為匯入靜態成員時,必須指定靜態成員或是全部靜態成員因此必須寫

import static com.sun.scjp.Geodetics.*;

或者

import static com.sun.scjp.Geodetics.DIAMETER;

 

第102題:

Given:

10. interface Foo{int bar();}
11. public class Sprite{
12.     public int fubar(Foo foo){return foo.bar();}
13.     public void testFoo(){
14.         fubar(
15.             //insert code here
16.         );

17.     }
18. }

Which code, inserted at line 15, allows the class Sprite to compile?

A. Foo{public int bar(){return 1;}}

B. new Foo{public int bar(){return 1;}}

C. new Foo(){public int bar(){return 1;}}

D. new class Foo{public int bar(){return 1;}}

答案:

題目範圍:innerClass

解析:

testFoo方法中呼叫了fubar方法

fubar方法必須傳入一個Foo物件

C選項在生成物件同時做了一個innerClass

 

第103題:

Given:

11. public enum Title{

12.     MR("Mr."), MRS("Mrs."), MS("Ms.");

13.     private final String title;

14.     private Title(String t){title = t;}

15.     public String format(String last, String first){

16.         return title + " " + first + " " + last;

17.     }

18. }

19. public static void main(String[] args){

20.     System.out.println(Title.MR.format("Doe", "John"));

21. }

What is the result?

A. Mr. John Doe

B. An exception is thrown at runtime.

C. Compilation fails because of an error in line 12.

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

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

答案:

題目範圍:enum 列舉

解析:

如果把列舉看成是一個傳入參數被限制的類別,就會清楚很多

例如這裡的Title列舉,傳入的參數被限制在這三種 "MR", "MRS" "MS"

"MR"會變成"Mr."傳入建構子,"MRS"會變成"Mrs.""MS"會變成"Ms."

如果Title是個類別,要這樣寫

new Title("Mr.").format("Doe", "John");

Title是列舉,就這樣寫

Title.MR.format("Doe", "John")

 

有些人或許對finaltitle有些疑問:為什麼不用初始化,而且還可以賦予值?

其實final的變數可以不用初始化,不過賦予值只限一次

 

第104題:

Given:

10. class Line{
11.     public static class Point{}
12. }
13.
14. class Triangle{
15.     //insert code here
16. }

Which code, inserted at line 15, creates an instance of the Point class defined in Line?

A. Point p = new Point();

B. Line.Point p = new Line.Point();

C. The Point class cannot be instatiated at line 15.

D. Line l = new Line(); l.Point p = new l.Point();

答案:

題目範圍:innerClass

解析:

從外部實體化innerClass的方法

這裡的Point是一個靜態類別,因此只要寫Line.Point p = new Line.Point();就可以

如果Point不是靜態的,那麼就要像下面這樣寫:

Line l = new Line();

Line.Point p = l.new Point();

如果要寫成一行的話,這樣也可以:

Line.Point p = new Line().new Point();

 

第105題:

Given:

1. public class A {
2.
3.     private int counter = 0;
4
5.     public static int getInstanceCount(){
6.         return counter;
7.     }
8.
9.     public A(){
10.         counter++;
11.     }
12.
13. }

And given this code from Class B:

25. A a1 = new A();
26. A a2 = new A();
27. A a3 = new A();
28. System.out.println(A.getInstanceCount());

What is the result?

A. Compilation of class A fails.

B. Line 28 prints the value 3 to System.out.

C. Line 28 prints the value 1 to System.out.

D. A runtime error occurs when line 25 executes.

E. Compilation fails because of an error on line 28.

答案:

題目範圍:靜態元素

解析:

如果你能夠照著題目來思考執行的過程,那麼大概就能夠了解為什麼靜態方法中必須全都是靜態元素了

這裡的getInstanceCount方法是靜態的,但是裡面的counter不是靜態的

這樣運行起來會有很多矛盾,因此是不被允許的

arrow
arrow
    全站熱搜

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