16~20題考的都是JAVA重要的觀念

務必弄懂才繼續做下去

第十六題:

Given:

class Atom {
    Atom() {
        System.out.print("atom ");
    }
}

class Rock extends Atom {
    Rock(String type) {
        System.out.print(type);
    }
}

public class Mountain extends Rock {
    Mountain() {
        super("granite ");
        new Rock("granite ");
    }

    public static void main(String[] a) {
        new Mountain();
    }
}

What is the result?

A. Compilation fails.
B. atom granite
C. granite granite
D. atom granite granite
E. An exception is thrown at runtime.
F. atom granite atom granite

答案:

執行結果:答案中

題目範圍:物件觀念

解析:

這題的觀念非常的核心,如果不太瞭解一定要想辦法弄懂
類別被宣告為物件時,會呼叫建構子
建構子的第一行如果沒有寫super 或是this 的話,就會給定預設super() 在建構子的第一行
也就是說,子代的建構子在運作之前,必定會先運行父代的建構子,父代的建構子又會運行父代的父代的建構子直到最上層Object 代為止
從子代一直呼叫到最上層父代;再從最父代運行回到子代

這題中,Atom類別沒有明講繼承的對象,那麼預設就會是繼承了Object 類別
Rock
繼承了Atom
Mountain
繼承了Rock
因此當main 中宣告了Mountain 時,會從Object 建構子開始運行,然後運行Atom 的建構子,然後是Rock,最後是Mountain
Mountain
建構子中的第二行又宣告了Rock 物件,從Object 建構子開始運行,然後運行Atom 的建構子,最後是Rock

類題:十八題、二十八題、三十七題

 

第十七題:

Given:

1. public class Blip{
2.     protected int blipvert(int x){return 0;}
3. }
4. class Vert extends Blip{
5.     //insert code here
6. }

Which five methods, inserted independently at line 5, will compile? (Choose five.)

A. public int blipvert(int x){return 0;}

B. private int blipvert(int x){return 0;}

C. private int blipvert(long x){return 0;}

D. protected long blipvert(int x){return 0;}

E. protected int blipvert(long x){return 0;}

F. protected long blipvert(long x){return 0;}

G. protected long blipvert(int x, int y){return 0;}

答案:ACEFG

執行結果:無

題目範圍:物件觀念

解析:

繼承方法的限制,第十二題拖拉題就有出現過
同方法的可見度只能增大不可縮小
可以多載,但是不能覆蓋成不同的回傳型態
另外,多載的方法可以視為不同的方法,因此可見度不受限制

請參閱:第十二題

 

第十八題:

Given:

1. class Super{
2.     private int a;
3.     protected Super(int a){this.a = a;}
4. }
...
11. class Sub extends Super{
12.     public Sub(int a){super(a);}
13.     public Sub(){this.a = 5;}
14. }

Which two, independently, will allow Sub to compile? (Choose two.)

A. Change line 2 to:
 public int a;

B. Change line 2 to:
 protected int a;

C. Change line 13 to:
 public Sub(){this(5);}

D. Change line 13 to:
 public Sub(){super(5);}

E. Change line 13 to:
 public Sub(){super(a);}

答案:CD

執行結果:無

題目範圍:物件觀念

解析:

選項B看似是正確的,不過還差了一點,因為這樣的話第13行的建構子會因為缺少父代的無引數建構子而產生編譯錯誤
如果要套用B選項的話,還得把
Super 類別加上一個無引數的建構子才行

 

第十九題:

Which Man class properly represents the relationship "Man has a best friend who is a Dog"?

A. class Man extends Dog{}

B. class Man implements Dog{}

C. class Man{private BestFriend dog;}

D. class Man{private Dog bestFriend;}

E. class Man{private Dog<bestFriend>;}

F. class Man{private BestFriend<dog>;}

答案:

執行結果:無

題目範圍:物件觀念

解析:

A. class Man extends Dog{} 這麼說的話就是"人是狗的一種",太可怕了@@

B. class Man implements Dog{} 人用了狗的功能,不是吧 =_=

C. class Man{private BestFriend dog;} 好朋友是狗,不對

D. class Man{private Dog bestFriend;} 狗是好朋友,正確

E. class Man{private Dog<bestFriend>;} 狗是好朋友組成的集合,錯

F. class Man{private BestFriend<dog>;} 好朋友是狗組成的集合,錯

 

第二十題:

A team of programmers is reviewing a proposed API for a new utility class. After some discussion, they realize that they can reduce the number of methods in the API without losing any functionality. If they implement the new design, which two OO principles will they be promoting?

A. Looser coupling

B. Tighter coupling

C. Lower cohesion

D. Higher cohesion

E. Weaker encapsulation

答案:AD

執行結果:無

題目範圍:物件觀念

解析:

如果你是初識JAVA的人,你看到OO一定會想:O_O什麼是OO
OO: Object Oriented
,物件導向
物件導向並不是說寫幾個class,弄一些物件讓他運作就這麼簡單
OO
的精隨,也就是其他非OO的語言無法達成的效果,就在於能夠有效的讓工程師們更輕鬆地完成他們的工作
物件導向運作上很簡單,不過要運作的流暢,有效的提高工程師效率,還是需要許多經驗

 

請參閱:
Coupling and Cohesion: The Two Cornerstones of OO Programming
Cohesion and Coupling

 

 

cohesion指的是內聚力、凝聚性的意思。通常指的是程式中各個類別之間的分工上
coupling
指的是對外部的接合、耦合的意思。當類別之間互相需要交流的資訊越多,coupling就越高
一個好的組織,程式的組織或是社群的組織,要有效的工作,必定是分工十分的恰當,不會做重複的工作,也不會漏掉工作。這就是高度的cohesion
除了分工恰當之外,個體之間所需的交流越少,就可以降低資訊交換時可能出現的麻煩,或是某個個體被做替換時,需要重新調適的工作。這就是鬆散的coupling

一般設計程式時,都會希望程式內部可以有高的cohesion與低的coupling
以增加程式的可讀性與維護性

arrow
arrow
    文章標籤
    JAVA SCJP 考試 證照
    全站熱搜

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