23題跟24題是設計概念上的問題

雖然不是語法邏輯之類的問題,但是重要性也不能忽視

設計規劃有如藍圖,撰寫程式有如鋼筋水泥

藍圖如果不好,即使用上好的建材也造不出好房子;程式規劃沒做好,也難以寫出好的程式,也會使維護更加困難

 

第二十一題:

Given:

class Batman {
    int squares = 81;

    public static void main(String[] args) {
        new Batman().go();
    }

    void go() {
        incr(++squares);
        System.out.println(squares);
    }

    void incr(int squares) {
        squares += 10;
    }
}

What is the result?

A. 81
B. 82
C. 91
D. 92
E. Compilation fails.
F. An exception is thrown at runtime.

答案:

執行結果:答案中

題目範圍:基本觀念

解析:

變數的區域性要注意,這題其實不難,但是針對物件的概念再加以解說一下
這一題中,
int squares是在Batman類別實體化為物件時,被產生的屬性(Field)
void incr(int squares)中的整數是在方法被呼叫時才會存在記憶體中,稱為變數(variable)
OO的語言中,這個觀念格外的重要,特別要注意取得記憶體的時間點

 

第二十二題:

Given:

class Pass {
    public static void main(String[] args) {
        int x = 5;
        Pass p = new Pass();
        p.doStuff(x);
        System.out.print(" main x = " + x);
    }

    void doStuff(int x) {
        System.out.println(" doStuff x = " + x++);
    }
}

What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuff x = 6 main x = 6
D. doStuff x = 5 main x = 5
E. doStuff x = 5 main x = 6
F. doStuff x = 6 main x = 5

答案:

執行結果:答案中

題目範圍:基本觀念

解析:

JAVA中沒有指標的語法,不過有時還是隱藏了指標的用法
只要知道在方法參數傳遞時,都是傳值就可以了

x++是先執行完指令再累加;++x是先累加在完成指令

 

第二十三題:

A company has a business application that provides its users with many different reports: receivables reports, payables reports, revenue projects, and so on. The company has just purchased some new, state-of-the-art, wireless printers, and a programmer has been assigned the task of enhancing all of the reports to use not only the company's old printers, but the new wireless printers as well.

When the programmer starts looking into the application, the programmer discovers that because of the design of the application, it is necessary to make changes to each report to support the new printers.

Which two design concepts most likely explain the situation?
(Choose two.)

A. Inheritance
B. Low cohesion
C. Tight coupling
D. High cohesion
E. Loose coupling
F. Object immutablility

答案:BC

執行結果:無

題目範圍:物件觀念

解析:

這題直接舉了一個例子:工程師發現程式要更新時,他必須修改所有的報表產生器內容
這就是低度分工與高度耦合的情形

為什麼會導致必須修改所有的報表產生器來維護這個軟體呢?推測起來就是因為所有的報表產生時都是獨立運作的,應收帳款報表、應付帳款報表、收入報表等等都完全是由不同的程式片段來產生,類似的工作,卻是由全然不同的程式片段來執行,這就是低度的分工,Low cohesion

獨立產生報表的同時也會出現一個問題,那就是添購新的印表機時,必須逐一修改程式片段;這也表示程式在運作時,程式片段會有很多資料交換,這就是高度的耦合,Tight coupling

雖然這裡舉出了這樣的例子,不過不要誤會,cohesion coupling 的高低沒有絕對的關係,不是高度的分工就一定會是低度的耦合
低分工和高耦合雖然會使程式維護較不易,不過有時候考量到開發的成本或時間,這樣的程式有時候還是會有存在的價值
有效的提高分工和降低耦合需要有經驗的工程師,和投入規劃的設計時間

請參閱:第二十題

 

第二十四題:

A company that makes Computer Assisted Design(CAD) software has, within its application some utility classes that are used to perform 3D rendering tasks. The company's chief scientist has just improved the performance of one of the utility classes' key rendering algorithms, and has assigned a programmer to replace the old algorithm with the new algorithm.

When the programmer begins researching the utility classes, she is happy to discover that the algorithm to be replaced exists in only one class. The programmer reviews that class's API, and replaces the old algorithm with the new algorithm, being careful that her changes adhere strictly to the class's API.

Once testing has begun, the programmer discovers that other classes that use the class she changed are no longer working properly.

What design flaw is most likely the cause of there new bugs?

A. Inheritance
B. Tight coupling
C. Low cohesion
D. High cohesion
E. Loose coupling
F. Object immutablility

答案: 感謝mk0079提醒,已經修正於2012/02/23

執行結果:無

題目範圍:物件觀念

解析:

這個例子裏面有提到,工程師在修改時只需要針對功能做小部分的修改,這是高度的分工
不過當他正確的修改了程式以後,那些與這個片段相關的程式都無法正常運作了,這裡很可能就是資料交流中出現問題

程式之間的資料交流有太多多餘的部分,就很容易出現這種高度耦合問題


例如:
A程式負責丟整數、小數、字串交給B程式顯示

高耦合:A丟出整數小數字串的其中一種、資料長度、資料型態給B顯示
低耦合:A只丟出資料,交由B來判斷長度與資料型態然後顯示

這樣看起來,低耦合的情況,在新增程式功能時,會出現的問題就會少很多,例如加一個C程式來處理A的資料,或是要把A程式換掉

 

第二十五題:

Given:

class ClassA {
    public int numberOfInstances;

    protected ClassA(int numberOfInstances) {
        this.numberOfInstances = numberOfInstances;
    }
}

class ExtendedA extends ClassA {
    private ExtendedA(int numberOfInstances) {
        super(numberOfInstances);
    }

    public static void main(String[] args) {
        ExtendedA ext = new ExtendedA(420);
        System.out.print(ext.numberOfInstances);

    }

}

What is the result?

A. 420 is the output
B. An exception is thrown at runtime.
C. All constructors must be declared public.
D. Constructors CANNOT use the private modifier.
E. Constructors CANNOT use the protected modifier.

答案:

執行結果:答案中

題目範圍:物件觀念

解析:

建構子也是一種方法,可以使用的標記跟一般方法一樣
不過差別在於建構子並不會被繼承,而是只會被子代呼叫,因此沒有可見度增大縮小的問題

arrow
arrow

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