SCJP(OCPJP) 考古題解析 第40題

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


答案:


解析:

line 15:
所以 line 16 印出300


line 18:
所以 line 19 & line 20個別印出 300 & 100


line 22:
所以 line 23 & line 24 個別印出 100 & 100



0 意見:

張貼留言