寒假比較有時間…繼續吧

 

 

第七十六題:

Given:

class TestA {
    public void start() {
        System.out.println("TestA");
    }
}

public class TestB extends TestA {
    public void start() {
        System.out.println("TestB");
    }

    public static void main(String[] args) {
        ((TestA) new TestB()).start();
    }
}

What is the result?

A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.

答案:

題目範圍:物件繼承觀念

解析:

子代可以轉換成父代

不過本質還是子代,呼叫的方法如果存在於子代

子代的方法就會被呼叫

 

第七十七題:

Given:

    public static void main(String[] args) {
        Object obj = new int[]{1, 2, 3};
        int[] someArray = (int[])obj;
        for (int i: someArray) System.out.print(i + " ");
    }

What is the result?

A. 1 2 3
B. Compilation fails because of an error in line 12.
C. Compilation fails because of an error in line 13.
D. Compilation fails because of an error in line 14.
E. A ClassCastException is thrown at runtime.

答案:

題目範圍:一般觀念

解析:

把整數陣列轉換成物件是可以的,但是整數或是其他基本資料型態不能轉成物件

以指標的觀念來看,陣列本身就是個指標,物件也是個指標

而基本資料型態本身就是值,不可以轉換成物件(指標)

 

第七十八題:

A developer is creating a class Book, that needs to access class Paper. The Paper class is deployed in a JAR

named myLib.jar. Which three, taken independently, will allow the developer to use the Paper class while

compiling the Book class? (Choose Three.)

A. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.

B. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar.

C. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes

/foo/myLib.jar/Paper.class.

D. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes

/foo/myLib.jar.

E. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -cp

/foo/myLib.jar/Paper Book.java

F. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -d /foo/myLib.jar

Book.java

G. The JAR file is located at foo/myLib.jar and the Book class is compiled using javac -classpath

/foo/myLib.jar Book.java

答案:BDG

題目範圍:編譯

解析:

當程式需要用引用jar檔時,你可以把jar檔放在$JAVA_HOME/jre/lib/ext/底下

或者使用classpath把程式跟jar做關聯

 

第七十九題:

Given classes defined in two different files:

package packageA;

public class Message {
    String getText() {
        return "text";
    }
}

And:

package packageB;

public class XMLMessage extends packageA.Message {

    String getText() {

        return "<msg>text</msg>";
    }

    public static void main(String[] args) {
        System.out.println(new XMLMessage().getText());
    }
}

What is the result of executing XMLMessage.main?

A. text
B. Compilation fails.
C. <msg>text</msg>
D. An exception is thrown at runtime.

答案:

題目範圍:物件觀念

解析:

子代覆寫父代方法,執行時當然是執行子代的方法

 

第八十題:

Given:

interface Fish {
}

class Perch implements Fish {
}

class Walleye extends Perch {
}

class Bluegill {
}

public class Fisherman {
    public static void main(String[] args) {
        Fish f = new Walleye();
        Walleye w = new Walleye();
        Bluegill b = new Bluegill();
        if (f instanceof Perch)
            System.out.print("f-p ");
        if (w instanceof Fish)
            System.out.print("w-f ");
        if (b instanceof Fish)
            System.out.print("b-f ");
    }
}

What is the result?

A. w-f

B. f-p w-f

C. w-f b-f

D. f-p w-f b-f

E. Compilation fails.

F. An exception is thrown at runtime.

答案:

題目範圍:物件觀念

解析:

不管物件怎麼被轉換型態,都是同一個物件

f 被轉成fish 物件,但是依然是Walleye

Walleye是屬於Perch

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 yaya741228 的頭像
    yaya741228

    來喝杯JAVA咖啡

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