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

Given:

public class KungFu {
    public static void main(String[] args) {
        Integer x = 400;
        Integer y = x;
        x++;
        StringBuilder sb1 = new StringBuilder("123");
        StringBuilder sb2 = sb1;
        sb1.append("5");
        System.out.println((x == y) + " " + (sb1 == sb2));
    }
}


What is the result?

考古題下載


Download

題庫來源:SCJP 6.0認證教站手冊(Oracle Certified Professional Java Programmer)

作者:黃彬華


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?

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

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 is true?

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

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.)

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

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?

SCJP(OCPJP) 考古題解析 第36題(Drag&Drop)

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 int getAmount() { return amount; }
    // more code here
}

Place the code framgmets in position to reduce the coupling between Doubler and Holder.