雖然到現在為止也沒有幾題,不過我也是邊寫邊學

如果有發現錯誤的地方,還請留言告知喔 ^^\\

不過我會努力不出現錯誤的

 

第三十一題:

Given:

public class Base {
    public static final String FOO = "foo";

    public static void main(String[] args) {
        Base b = new Base();
        Sub s = new Sub();
        System.out.print(Base.FOO);
        System.out.print(Sub.FOO);
        System.out.print(b.FOO);
        System.out.print(s.FOO);
        System.out.print(((Base) s).FOO);
    }
}

class Sub extends Base {
    public static final String FOO = "bar";
}

What is the result?

A. foofoofoofoofoo
B. foobarfoobarbar
C. foobarfoofoofoo
D. foobarfoobarfoo
E. barbarbarbarbar
F. foofoofoobarbar
G. foofoofoobarfoo

答案:

執行結果:無

題目範圍:物件觀念、變數的標記

解析:

標記為final的屬性雖然不可以修改數值,但是繼承時依然可以被複寫

到現在為止,前面的例題中偶爾會出現foobar來命名的變數或方法
那麼你一定會好奇:這到底是甚麼意思呢?
答案是:沒有意思
正如剛才的回答,foobar或是foobar這些命名都是沒有意思的變數,就像甲乙丙丁這樣的命名一樣
通常遇到舉例說明時,取用的變數如果沒有特別的意思,就會依序使用foo bar baz qux…來表示
下次遇到舉例的情況,別再用abc囉,那你就遜掉啦
歷史背景與更多名詞請自行上網查找,關鍵字:foobar

 

第三十二題:

Given:

class Mammal {
}

class Raccoon extends Mammal {
    Mammal m = new Mammal();
}

class BabyRaccoon extends Mammal {
}

Which four statments are true? (Choose four.)

A. Raccoon is-a Mammal.
B. Raccoon has-a Mammal.
C. BabyRaccoon is-a Mammal.
D. BabyRaccoon is-a Raccoon.
E. BabyRaccoon has-a Mammal.
F. BabyRaccoon is-a BabyRaccoon.

答案:ABCF

執行結果:無

題目範圍:物件觀念

解析:

英文解釋:Raccoon 狸,Mammal 哺乳動物

雖然照理來說BabyRaccoon應該屬於(is-a)Raccoon
不過依照題目來看,BabyRaccoon並沒有繼承Raccoon,只有繼承Mammal

Raccoon has-a Mammal.雖然看起來很怪,但是程式中就是這樣寫的

 

第三十三題:

Given:

2. public class Hi{
3.     void m1(){}
4.     protected void m2(){}
5. }
6. class Lois extends Hi{
7.     //insert code here
8. }

Which four code fragments, inserted independently at line 7, will compile? (Choose four.)

A. public void m1(){}
B. protected void m1(){}
C. private void m1(){}
D. void m2(){}
E. pubic void m2(){}
F. protected void m2(){}
G. private void m2(){}

答案:ABEF

執行結果:答案中

題目範圍:物件觀念

解析:

請參閱:

 

第三十四題:

Which four statements are true? (Choose four.)

A. Has-a relationships should never be encapsulated.

B. Has-a relationships should be implemented using inheritance.

C. Has-a relationships can be implemented using instance variables.

D. Is-a relationships can be implemented using the extends keyword.

E. Is-a relationships can be implemented using the implements keyword.

F. The relationship between Movie and Actress is an example of an is-a relationship.

G. An array or a collection can be used to implement a one-to-many has-a relationship.

答案:CDEG

執行結果:無

題目範圍:物件觀念

解析:

英文解釋:encapsulated 封裝

注意這題的implemented 並不是關鍵字implements 的意思,單純是語意上的"實現"的意思

 

第三十五題:

Given:

public class Hello {
    String title;
    int value;

    public Hello() {
        title += " World";
    }

    public Hello(int value) {
        this.value = value;
        title = "Hello";
        Hello();
    }
}
And:

Hello c = new Hello(5);
System.out.print(c.title);

What is the result?

A. Hello
B. Hello World
C. Compilation fails.
D. Hello World 5
E. The code runs with no output.
F. An exception is thrown at runtime.

答案:

執行結果:答案中

題目範圍:物件觀念

解析:

特別注意這題的Hello()方法是一個建構子,建構子是不能直接呼叫的
只有在類別實體化時,建構子才會被呼叫

建構子可以有public private proceted 標籤
不過如果加上了回傳值標籤那就會被認為是一般方法了

例如:
private void Test() 一般方法
public Test() 建構子

arrow
arrow

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