第四十六題:

Given:

public class Pizza {
    ArrayList toppings;

    public final void addTopping(String topping) {
        toppings.add(topping);
    }

    public void removeTopping(String topping) {
        toppings.remove(topping);
    }
}

And:

class PepperoniPizza extends Pizza {
    public void addTopping(String topping) {
        System.out.println("Cannot add Toppings");
    }

    public void removeTopping(String topping) {
        System.out.println("Cannot remove pepperoni");
    }
}
And:

    Pizza pizza = new PepperoniPizza();
    pizza.addTopping("Mushrooms");
    pizza.removeTopping("Pepperoni");

What is the result?

A. Compilation fails.
B. Cannot add Toppings
C. The code runs with no output.
D. A NullPointerException is thrown in Line 4.

答案:以更正於2012/01/19,感謝范棛祺提醒 

執行結果:答案中

題目範圍:物件觀念

解析

英文解釋:Pepperoni 義大利辣味香腸(好吃)Mushroom 蘑菇

如果方法被定為final,那麼就表示被繼承之後就不可以覆寫,但還是可以多載

請注意變數與方法被標記為final時限制的不同點
變數的final 在繼承時可以被覆寫,如果父代的變數被標記final 時,不可以修改該變數內容
但是如果子代覆寫該變數,則以子代設定為準

 

第四十七題(拖拉題)

Place the Types in one of the Type columns, and the Relationships in the Relationship column, to define

appropriate has-a and is-a relationships:

Type

Relationship

Type

  Dog

 is-a

Animal

Forest

has-a

 Tree

Rectangle

has-a

 Side

Java Book

 is-a

Programming Book

 

Relationship

is-a

has-a

Types

Dog

Side

Tail

Square

Tree

Book

Java Book

Pen

 

答案:題目反白

執行結果:無

題目範圍:物件觀念

解析:

以生活實例來表現物件觀念的題目

有些人可能會填入Book  has-a  Programming Book,不過這樣是不對的,書本裡面並沒有另外一種書,而可能是紙、文字、書籤等等
應該是要反過來Programming Book  is-a  Book

has-a is-a 並不是相反的關係,不要混淆

 

第四十八題:

Given:

public class Venus {
    public static void main(String[] args) {
        int[] x = { 1, 2, 3 };
        int y[] = { 4, 5, 6 };
        new Venus().go(x, y);
    }

    void go(int[]... z) {
        for (int[] a : z)
            System.out.print(a[0]);

    }

}

What is the result?

A. 1
B. 12
C. 14
D. 123
E. Compilation fails.
F. An exception is thrown at runtime.

答案:

執行結果:答案中

題目範圍:for迴圈列舉法

解析:

這題只要抓住傳遞參數的型態,就很容易可以解出來

go() 方法中
z
是一個二維的int 陣列
a
是一個一為的int 陣列

 

第四十九題(拖拉題)

Given:

    public int update(int quantity, int adjust) {

        quantity = quantity + adjust;

        return quantity;       _

    }

 

    public void callUpdate() {

        int quant = 100;

        quant = update(quant, 320);

        System.out.println("The quantity is " + quant);

    }

 

Code Fragments

public int

quantity = quantity + adjust;

update(quant, 320);

quant = update(quant, 320);

public void

quantity = quantity + adjust;

return quantity;

 

答案:題目反白

執行結果:無

題目範圍:基本觀念

解析:

方法在傳遞基本資料型態時,傳遞的是值而不是指標

 

第五十題:

Given:

public abstract class Shape {
    private int x;
    private int y;

    public abstract void draw();

    public void setAnchor(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Which two classes use the Shape class correctly? (Choose two.)

A. public class Circle implements Shape{
 
    private int radius;
 }

B. public abstract class Circle extends Shape{
 
    private int radius;
 }

C. public class Circle extends Shape{
 
    private int radius;
 
    public void draw();
 }

D. public abstract class Circle implements Shape{
 
    private int radius;
 
    public void draw();
 }

E. public class Circle extends Shape{
 
    private int radius;
 
    public void draw(){/* code here */}
 }

F. public abstract class Circle implements Shape{
 
    private int radius;
 
    public void draw(){/* code here */}

 }

答案:BE

執行結果:無

題目範圍:abstract class

解析:

Interface 才是用implements 關鍵字
abstract class
是類別的一種,只可以用來繼承(extends)

不論是繼承抽象類別還是一般類別,子代都可以被設定成抽象類別
如果類別中有方法是抽象的,那麼該類別就必須是抽象類別
或是把抽象方法實作起來,這樣就可以是一般類別了

Abstract這個單字的意思是抽象,抽象並不是模糊不清的意思,而是精隨的意思
在設計抽象類別時,應該要把很關鍵的設計概念放在類別中,這就是設計抽象類別的意義所在

arrow
arrow

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