努力不懈,加油

 

第三十六題(拖拉題)

Given:

public class Doubler {
    public static int doubleMe(Holder h) {
        return h.getAmount() * 2;
    }
}
And:

public class Holder {
    int amount = 10;

    public void doubleAmount() {
        amount = Doubler.doubleMe(this);
    }

    public in getAmount() {
        return amount;
    }
    // more code here
}
Place the code framgmets in position to reduce the coupling between Doubler and Holder.

public class Doubler {
    public static int doubleMe( Place here
int     h){
        return Place here
h      * 2;
    }
}

public class Holder {
    int amount = 10;

    public void doubleAmount(){amount = Doubler.doubleMe( Place here
amount   );}

    public in getAmount() {
        return amount;
    }
    // more code here
}
Code Fragments

void

Holder

int

Double

h.getAmount()

h

this

amount

 

答案:題目反白 (已經修正於2012/02/27 感謝訪客熱心提醒)

執行結果:無

題目範圍:程式設計,基本觀念

解析:

這題目是要簡化程式碼的運算,很容易
本來是把整個物件傳過去運算,後來是先取出要運算的值再傳

 

第三十七題:

Given:

abstract class C1 {
    public C1() {
        System.out.print(1);
    }
}

class C2 extends C1 {
    public C2() {
        System.out.print(2);
    }
}

class C3 extends C2 {
    public C3() {
        System.out.print(3);
    }
}

public class Ctest {
    public static void main(String[] a) {
        new C3();
    }
}

What is the result?

A. 3
B. 23
C. 32
D. 123
E. 321
F. Compilation fails.
G. An exception is thrown at runtime.

答案:

執行結果:答案中

題目範圍:物件觀念

解析:

類別實體化時,建構子呼叫的順序

請參閱:第十六題

 

第三十八題:

Given:

class One {
    public One foo() {
        return this;
    }
}

class Two extends One {
    public One foo() {
        return this;
    }
}

class Three extends Two {
    // insert method here
}

Which two methods, inserted individually, correctly complete the Three class? (Choose two.)

A. public void foo(){}
B. public int foo(){return 3;}
C. public Two foo(){return this;}
D. public One foo(){return this;}
E. public Object foo(){return this;}

答案:CD

執行結果:無

題目範圍:物件觀念

解析:

繼承類別時,方法使用上的限制

public Two foo(){return this;}這個方法雖然回傳了Two類別,但Two是繼承One
Two屬於One,因此回傳了Two也就能解釋為回傳了One,不違反限制規定

例如:我要一台車子,你丟一台巴士給我,不違反我的要求。如果是反過來的話就不行了

 

第三十九題:

Given:

public class ItemTest {
    private final int id;

    public ItemTest(int id) {
        this.id = id;
    }

    public void updateId(int newId) {
        id = newId;
    }

    public static void main(String[] args) {
        ItemTest fa = new ItemTest(42);
        fa.updateId(69);
        System.out.println(fa.id);
    }
}

Which four statments are true?

A. Compilation fails.
B. An exception is thrown at runtime.
C. The attribute id in the ItemTest object remains unchanged.
D. The attribute id in the ItemTest object is modified to the new value.
E. A new ItemTest object is created with the preferred value in the id attribute.

答案:

執行結果:無

題目範圍:基本觀念

解析:

final關鍵字的用法,已經訂為final的變數或屬性如果有被修改的可能(放在等號左邊) 編譯器就會報錯

 

第四十題:

Given:

class Foo {
    private int x;

    public Foo(int x) {
        this.x = x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getX() {
        return x;
    }
}

public class Gamma {
    static Foo fooBar(Foo foo) {
        foo = new Foo(100);
        return foo;
    }

    public static void main(String[] args) {
        Foo foo = new Foo(300);
        System.out.print(foo.getX() + "-");

        Foo fooFoo = fooBar(foo);

        System.out.print(foo.getX() + "-");
        System.out.print(fooFoo.getX() + "-");

        foo = fooBar(fooFoo);

        System.out.print(foo.getX() + "-");
        System.out.print(fooFoo.getX());
    }
}

What is the output?

A. 300-100-100-100-100
B. 300-300-100-100-100
C. 300-300-300-100-100
D. 300-300-300-300-100

答案:

執行結果:答案中

題目範圍:基本觀念

解析:

做這題頭腦不夠清楚可能會被搞混喔
這題是考JAVA 隱藏的指標觀念,其實規則很容易:
1.
方法的呼叫永遠都是傳值,方法在接到值之後,會另外產生一個空間來存這個值,也就是有兩份值,兩個位址
2.
等號的運算有分為基本資料型態(int, float等等) 和物件兩種
    2.1. 等號處理基本資料型態時是傳值
    2.2.
等號處理物件時是傳位址

依照題目的情況來模擬一下:

Foo foo = new Foo(300);
Foo fooFoo = fooBar(foo);

生成一個物件,並且呼叫下面方法,在呼叫之前,記憶體的情況如下

指標

main: foo

內容

Foo物件

X=300

 

static Foo fooBar(Foo foo) {
    foo = new Foo(100);
    return foo;
}

方法第一行接收了物件之後就另外產生空間複製一份,記憶體如下

指標

main: foo

fooBar: foo

內容

Foo物件

X=300

Foo物件

X=300


方法第二行new 了一個物件,也就是生成了新的位址,把這個物件位指指派給foo,記憶體如下

指標

main: foo

 

fooBar: foo

內容

Foo物件

X=300

Foo物件

X=300

Foo物件

X=100

你會發現,有一個空間沒有指標指到它了,沒錯,這樣一來這個空間隨時會被空間回收程序收走

方法第三行回傳了fooBar: foo指標

後來在主方法執行了這一行

foo = fooBar(fooFoo);

之後記憶體狀況會變成

指標

   

main: foo

內容

Foo物件

X=300

Foo物件

X=300

Foo物件

X=100


只要記住前述兩大規則,你就可以掌握所有JAVA 中所有的指標規則

arrow
arrow

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