因為最近被懶神附身了

注意,不是纜繩,是懶神!

懶了很久沒有繼續發文,今天總算又開工了,希望以後不要再被附身了>"<

 

 

第六十一題:

Given:

Given two files, GrizzlyBear.java and Salmon.java:

1. package animals.mammals;
2.
3. public class GrizzlyBear extends Bear{
4.     void hunt() {
5.         Salmon s = findSalmon();
6.         s.consume();
7.     }
8. }

1. package animals.fish;
2.
3. public class Salmon extends Fish {
4.     public void consume() { /* do stuff */ }
5. }

If both classes are in the correct directories for their packages, and the Mammal class correctly defines the findSalmon() method, which change allows this code to compile?

A. add import animals. mammals.*; at line 2 in Salmon.java

B. add import animals.fish.*; at line 2 in GrizzlyBearjava

C. add import animals.fish.Salmon.*; at line 2 in GrizzlyBear.java

D. add import animals. mammals.GrizzlyBear*; at line 2 in Salmon.java

答案:

題目範圍:程式的import

解析:

因為GrizzlyBear用到了Salmon類別,因此要把它import進來

Import只能指定package或者檔案,不可以指定方法

 

第六十二題:

Given:

31. class Foo{
32.     public int a = 3;
33.     public void addFive(){ a += 5; System.out.print("f "); }
34. }
35. class Bar extends Foo{
36.     public int a = 8;
37.     public void addFive(){this.a += 5; System.out.print("b ");}
38. }
Invoked with:

    Foo f = new Bar();
    f.addFive();
    System.out.println(f.a);

What is the result?

A. b 3
B. b 8
C. b 13
D. f 3
E. f 8
F. f 13
G. Compilation fails,
H. An exception is thrown at runtime.

答案:

題目範圍:物件觀念

解析:

這一題有些複雜,來分析一下

首先是子代物件轉換成父代之後,使用方法…
如果父代沒有該方法,則會出現編譯器錯誤:找不到方法
如果子代沒有該方法,則會直接使用父代的方法
如果兩代都有該方法,則會使用子代的方法

當變數在父子兩代都有宣告,方法在運行時…
子代的方法使用子代的變數(因此這裡
this.a this 寫不寫是不影響結果的)
父代的方法使用父代的變數

當變數在父子兩代都有宣告,取值時…
如果父代沒有該變數,則會出現編譯器錯誤:找不到變數
如果子代沒有該變數,則會使用父代的變數
如果兩代都有該變數,則會使用父代的變數

所以運行過程是:
f.addFive(); 運行子代的方法,並且子代的變數被修改
System.out.println(f.a);
 取得父代的變數

 

第六十三題:

Given:

11. class ClassA{}
12. class ClassB extends ClassA{}
13. class ClassC extends ClassA{}

and:

21. ClassA p0 = new ClassA();
22. ClassB p1 = new ClassB();
23. ClassC p2 = new ClassC();
24. ClassA p3 = new ClassB();
25. ClassA p4 = new ClassC();

Which three are valid? (Choose three.)

A. p0 = p1;
B. p1 = p2,
C. p2 = p4;
D. p2 = (ClassC)p1;
E. p1 = (ClassB)p3;

答案:AEF

題目範圍:物件觀念

解析:

 

第六十四題:

Given:

class A {
    String name = "A";

    String getName() {

        return name;
    }

    String greeting() {

        return "class A";
    }
}

class B extends A {
    String name = "B";

    String greeting() {

        return "class B";
    }
}

public class Client {
    public static void main(String[] args) {
        A a = new A();
        A b = new B();
        System.out.println(a.greeting() + "has name" + a.getName());
        System.out.println(b.greeting() + "has name" + b.getName());
    }
}

 

Place the names "A" and "B" in the following output Names

class has name

class has name

Names
A
B

 

答案:題目反白

題目範圍:物件觀念

解析:

請參閱:第六十三題

 

第六十五題:

Replace two of the Modifiers that appear in the Single class to make the code compile.

Note: Three modifiers will not be used and four modifiers in the code will remain unchanged.

Code

public class Single {

    private static Single instance;

 

    public static Single getInstance() {

        if (instance == null)

            instance = create();

        return instance;

    }

 

    private Single() {

    }

 

    protected Single create() {

        return new Single();

    }

}

 

class SingleSub extends Single {

}

Modifiers

final

protected

private

abstract

static

 

答案:

protected Single()

static Single create()

題目範圍:物件觀念

解析:

由於有類別繼承了SingleSub,因此SingleSub 的建構子就不可以是private
static
方法只能呼叫static方法;一般方法也可以呼叫static方法

arrow
arrow

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