第八十一題:

Given:

interface DoStuff2 {
    float getRange(int low, int high);
}

interface DoMore {
    float getAvg(int a, int b, int c);
}

abstract class DoAbstract implements DoStuff2, DoMore {
}

class DoStuff implements DoStuff2 {
    public float getRange(int x, int y) {
        return 3.14f;
    }
}

interface DoAll extends DoMore {
    float getAvg(int a, int b, int c, int d);
}

What is the result?

A. The file will compile without error.

B. Compilation fails. Only line 7 contains an error.

C. Compilation fails. Only line 12 contains an error.

D. Compilation fails. Only line 13 contains an error.

E. Compilation fails. Only lines 7 and 12 contain errors.

F. Compilation fails. Only lines 7 and 13 contain errors.

G. Compilation fails. Lines 7, 12, and 13 contain errors.

答案:

題目範圍:物件觀念、抽象類別、介面

解析:

抽象類別DoAbstract不需要實作方法

一般類別DoStuff實作了介面DoStuff2的方法

介面DoAll繼承了介面DoMore,加入新的方法

以上這三個動作都沒有問題

 

第八十二題:

Given:

public interface A111 {
    String s = "yo";

    public void method1();
}

interface B {
}

interface C extends A111, B {
    public void method1();

    public void method1(int x);
}

What is the result?

A. Compilation succeeds.

B. Compilation fails due to multiple errors.

C. Compilation fails due to an error only on line 20.

D. Compilation fails due to an error only on line 21.

E. Compilation fails due to an error only on line 22.

F. Compilation fails due to an error only on line 12.

答案:

題目範圍:介面

解析:

介面C 繼承了兩個介面,雖然類別不能多重繼承,但是介面還是可以的

public void method1();一行把介面A111中的方法又寫了一次,這樣是可以的

不過如果用不同的回傳型態,例如:public int method1();不行

 

第八十三題:

Given:

10. interface Foo {
11.    int bar();
12.}
13.
14.public class Beta {
15.
16.    class A implements Foo {
17.        public int bar() {
18.            return 1;
        }
    }
19.
20.    public int fubar(Foo foo) {
        return foo.bar();
    }
21.
22.    public void testFoo() {
23.
24.        class A implements Foo {
25.            public int bar() {
                return 2;
            }
26.        }
27.
28.        System.out.println(fubar(new A()));
29.    }
30.
31.    public static void main(String[] argv) {
32.        new Beta().testFoo();
33.    }
14.}

Which three statements are true? (Choose three.)

A. Compilation fails.

B. The code compiles and the output is 2.

C. If lines 16, 17 and 18 were removed, compilation would fail.

D. If lines 24, 25 and 26 were removed, compilation would fail.

E. If lines 16, 17 and 18 were removed, the code would compile and the output would be 2.

F. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1.

答案:BEF

題目範圍:物件觀念、介面、innerClass

解析:

這裡出現了兩種innerClass:在class中的、在方法中的

其實不論是哪種innerClass,其效力都只限於它出現的block中,也就是大括弧中

同名的class會被覆寫,默認使用最接近呼叫點的層級的class

原程式的testFoo方法做了fubar(new A())的動作,這裡的A是在testFoo方法中生成的,因此指的便是方法中的innerClass(最接近呼叫點層級)

如果把16.17.18行去掉,不影響原本結果

如果把24.25.26行去掉,原本方法中指的A就會是class中的innerClass A (最接近呼叫點層級)

 

第八十四題:

Given:

class Alpha {
    public void foo() {
        System.out.print("Afoo ");
    }
}

public class Beta extends Alpha {
    public void foo() {
        System.out.print("Bfoo ");
    }

    public static void main(String[] args) {
        Alpha a = new Beta();
        Beta b = (Beta) a;
        a.foo();
        b.foo();
    }
}

What is the result?

A. Afoo Afoo
B. Afoo Bfoo
C. Bfoo Afoo
D. Bfoo Bfoo
E. Compilation fails.
F. An exception is thrown at runtime.

答案:

題目範圍:物件觀念

解析:

不管物件如何轉型,本質都不會改變

 

第八十五題:

Given:

class Animal {
    public String noise() {
        return "peep";
    }
}

class Dog extends Animal {
    public String noise() {
        return "bark";
    }
}

class Cat extends Animal {
    public String noise() {
        return "meow";
    }
}

Animal animal = new Dog();
Cat cat = (Cat)animal;
System.out.println(cat.noise());

What is the result?

A. peep

B. bark

C. meow

D. Compilation fails.

E. An exception is thrown at runtime.

答案:

題目範圍:物件觀念

解析:

不管物件如何轉型,本質都不會改變

但是別忘記,兄弟之間不能互相轉型

雖然可以將子代轉型為父代,再將父代強制轉型為另一個子代

這樣編譯可以通過,不過執行時就會出錯

如果工程師沒有注意父代轉子代的轉型的話,就會發生像這題的情況

arrow
arrow
    全站熱搜

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