• 沒有找到結果。

Car is an Object

在文檔中 試題試題試題試題 1. (頁 37-51)

F null

1. 2. 3. 4. 5. 6. 7.

1. 2. 3. 4. 5. 6. 7.

C F E B D A F

Place the Relations on their corresponding implementation Structures.

Note:Not all implementation Structures will be used.

Implementation Structures Relations

Car has Wheels

class A{

B b; C c;

}

A.

Car is a Vehicle And

Car is a Collectible

Car is an Object Car has a

SteeringWheel B. Car has a

SteeringWheel

Mini is a Car

Car is a Vehicle And

Car is a Collectible

C. Car has Wheels

class A

extends B, C{ } D. Mini is a Car

E. Car is an Object

【參考章節】2-2 繼承、Is-A、Has-A

【試題解析】

class Car

implements Vehicle, Collectable{ } class Car {

SteeringWheel sw; } class Car {

List<Wheel> wheels; } class Mini

extends Car{ }

class Car {} //任何類別都會自動繼承 Object。

Given the following Java code:

10. interface Jumper { public void jump(); }

20. class Animal {}

30. class Dog extends Animal { 31. Tail tail;

32. }

40. class Beagle extends Dog implements Jumper{

41. public void jump() { } 42. }

50. class Cat implements Jumper{

51. public void jump() { } 52. }

Which three are true? (Choose three.) A. Cat is-a Animal.

B. Cat is-a Jumper.

C. Dog is-a Animal.

D. Dog is-a Jumper.

E. Cat has-a Animal.

F. Beagle has-a Tail.

G. Beagle has-a Jumper.

【答案】BCF

【參考章節】2-2 繼承、Is-A、Has-A

【試題解析】

選項A:錯誤。Cat 並沒有 extends Animal。

選項B:正確。

選項C:正確。

選項D:錯誤。Dog 並沒有 implements Jumper。

選項E:錯誤。Cat 內沒宣告(或透過繼承擁有)Animal型別的參考。

選項F:正確。

選項G:錯誤。理由同選項E。

Given the following Java code:

10. class A{

11. public void process(){ System.out.println(“A, ”); }}

12. class B{

13. public void process() throws IOException{

14. super.process();

15. System.out.print(“B, ”);

16. throw new IOException();

17. }

18. public static void main(String[] args){

19. try{ new B().process(); }

20. catch(IOException e){ System.out.println(”Exception”); }}}

What is the result?

A. Exception.

B. A, B, Exception

C. Compilation fails because of an error in line 20.

D. Compilation fails because of an error in line 14.

E. A NullPointerException is thrown at runtime.

【答案】D

【參考章節】2-2 繼承、Is-A、Has-A

【試題解析】

第14行會造成編譯錯誤,因為B的隱含父類別Object並不提供process()函式。有興趣 的可以上java.sun.com上去查Object的API有提供哪些函式。

Which Man class properly represents the relationship “Man has a best friend who is a Dog”?

A. class Man extends Dog{}

B. class Man implements Dog{}

C. class Man { private BestFriend dog ;}

D. class Man { private Dog bestFriend;}

E. class Man { private Dog;}

F. class Man { private BestFriend;}

【答案】D

【參考章節】2-2-2 HAS-A

【試題解析】

選項A:人類是狗 選項B:人類是狗

選項C:人類有個叫”狗”的好朋友。

選項D:人類有個好朋友就是狗 選項E:錯誤的宣告。

選項F:錯誤的宣告。

Given the following Java code:

10. interface A { public int getValue(); } 11. class B implements A {

12. public int getValue() { return 1; } 13. }

14. class C extends B { 15. // insert code here 16. }

What three code fragments inserted individually at line 15, make used of polymorphism?

(Choose three)

A. public void add(C c) { c.getValue(); } B. public void add(B b) { b.getValue(); } C. public void add(A a) { a.getValue(); } D. public void add(A a, B b) { a.getValue(); } E. public void add(C c1, C c2) { c1.getValue(); }

【答案】BCD

【參考章節】2-3 多型

【試題解析】

選項 BCD 都有多型的特徵,但選項A與E只是單純的在class C的環境下來存取自身 的物件。

例如在C物件中,要呼叫add(B b),可能必須先利用多型建立一個物件B b = new C();,再透過b物件變數(實際上是C的物件實體) 利用pass-by-value的機制傳遞給add(B b),或是B b = new B();(實際上是B的物件實體)利用pass-by-value的機制來傳遞給add(B b)。

就add(C c)而言,先前也許只需要建立C c = new C(),再利用pass-by-value的機制傳 遞給add(C c)。

Place the Output Options in the Actual Output Sequence to indicate the output from this code:

1. class Alpha {

2. public void foo(String… args) { 3. System.out.println(“Alpha:foo”);

4. }

5. public void bar(String a){

6. System.out.println(“Alpha:bar”);

7. } 8. }

9. public class Beta extends Alpha { 10. public void foo(String a){

11. System.out.println(“Beta:foo”);

12. }

13. public void bar(String a){

14. System.out.println(“Beta:bar”);

15. }

16. public static void main(String[] args){

17. Alpha a = new Beta();

18. Beta b = (Beta)a;

19. a.foo(“test”);

20. b.foo(“test”);

21. a.bar(“test”);

22. b.bar(“test”);

23. } 24. }

Actual Output Sequence:

1 2 3 4 Out Options:

A. Alpha:foo B. Alpha:bar C. Beta:foo D. Beta:bar

1 2 3 4

1 2 3 4

A C D D

Alpha:foo Beta:foo Beta:bar Beta:bar

【參考章節】2-3 多型

【試題解析】

在Alpha a觀點只知道以下兩個方法:

public void foo(String… args) public void bar(String a)

在Beta b觀點知道以下四個方法:

public void foo(String a) //自己的方法 public void bar(String a) //覆寫

public void foo(String… args) //在Alpha定義的 public void bar(String a) //在Alpha定義的

程式第20行在這裡可以想成Beta b = new Beta()。因此很自然地就執行到程式第10 行的foo()方法,印出”Beta:foo”。

執行第21行,在a的觀點中,因為要執行bar(“test”)時有public void bar(String a)可以 呼叫,而在new Beta()實作中接到了呼叫public void bar(String a)的需求,因而執行程式第 13~15行並印出”Beta:bar”,這是覆寫機制。

執行第22行,執行到程式第13行的bar(String a)方法因而印出”Beta:bar”。

Given the following Java code:

1. class A{

2. String name = “A”;

3. String getName() { 4. return name;

5. }

6. String greeting() { 7. return “class A”;

8. } 9. }

10. class B extends A { 11. String name = “B”;

12. String greeting() { 13. return “class B”;

14. } 15. }

16. public class Client {

17. public static void main(String[ ] args){

18. A a = new A();

19. A b = new B();

20. System.out.println(a.greeting() + “ has name ” + a.getName());

21. System.out.println(b.greeting() + “ has name ” + b.getName());

22. } 23. }

Place the names “A” and “B” in the following output.

class 1. has name 2.

class 3. has name 4.

Names

A. A B. B

1. 2. 3. 4.

1 2 3 4

A A B A

class A has name A class B has name A

【參考章節】2-3 多型

【試題解析】

創建一個有繼承關係的子類別,將會分別產生子類別實體與父類別的實體。

a.greeting() – 很簡單的就是單純的A型別的參考,存取A實體的方法。

a.getName() – 同上,單純的存取A實體的方法。

b.greeting() – A型別參考b存取B實體,由於B類別覆寫了A類別的greeting方法,所以 此行是執行B類別的greeting方法,印出”B”。

b.getName() - A型別參考b存取B實體,在此行因為B類別並無覆寫A類別的getName 方法,所以會指向A實體中的getName方法,所以印出的name自然而然的就是A實體 的”A”。

Given the following Java code:

10. interface A { 11. void x();

12. }

13. class B implements A { 14. public void x() {}

15. public void y() {}

16. }

17. class C extends B { 18. public void x(){}

19. } And:

20. java.util.List<A> list = new java.util.ArrayList<A>();

21. list.add(new B());

22. list.add(new B());

23. for(A a : list) { 24. a.x();

25. a.y();

26. }

What is the result?

A. The code runs with no output.

B. An exception is thrown at runtime.

C. Compilation fails because of an error in line 20.

D. Compilation fails because of an error in line 21.

E. Compilation fails because of an error in line 23.

F. Compilation fails because of an error in line 25.

【答案】F

【參考章節】2-3 多型

【試題解析】

由於編譯時期編譯器所參考的是參考變數的型別,所以在執行第25行時,會因為介 面A中並沒有宣告y方法,除非將25進行強制向下轉型,不然執行到程式第25行會產生編 譯時期錯誤。

Given:

10. abstract class A { 11. abstract void a1();

12. void a2() {}

13. }

14. class B extends A { 15. void a1() {}

16. void a2() {}

17. }

18. class C extends B { void c1() {} }

And:

A x = new B(); C y = new C(); A z = new C();

What are four valid examples of polymorphic method calls? (Choose four.) A. x.a2();

B. z.a2();

C. z.c1();

D. z.a1();

E. y.c1();

F. x.a1();

【答案】ABDF

【參考章節】2-3 多型

【試題解析】

選項ABDF都成功使用多型來存取方法。

選項C錯誤,因為在編譯時期,編譯器會判斷出參考變數z所指向的A類別並沒有c1 的方法。

選項E錯誤,因為它沒使用多型的方式,只是一般的方法存取。

Given:

11. abstract class Vehicle { public int speed(){ return 0; } } 12. class Car extends Vehicle { public int speed() { return 60; } } 13. class RaceCar extends Car { public int speed() { return 150; } }

21. RaceCar racar = new RaceCar();

22. Car car = new RaceCar ();

23. Vehicle vehicle = new RaceCar ();

24. System.out.println( racar.speed() + “,” + car.speed() 25. + “,” + vehicle.speed());

What is the result?

A. 0,0,0 B. 150,60,0

C. Compilation fails.

D. 150,150,150

E. An exception is thrown at runtime.

【答案】D

【參考章節】2-3 多型

【試題解析】

本題由於執行的實體是RaceCar,而且RaceCar都有覆寫speed(),所以無論呼叫的參 考型別為繼承樹上的任一類別,都將呼叫RaceCar實體中的speed(),所以答案為D。

Given the following Java code:

1. public class Base {

2. public static final String FOO = “foo”;

3. public static void main(String[] args){

4. Base b = new Base();

5. Sub s = new Sub();

6. System.out.println(Base.FOO);

7. System.out.println(Sub.FOO);

8. System.out.println(b.FOO);

9. System.out.println(s.FOO);

10. System.out.println(((Base)s).FOO);

11. } 12. }

13. class Sub extends Base{

14. public static final String FOO = “bar”;

15. }

What is the result?

A. foofoofoofoofoo B. foobarfoobarbar C. foobarfoofoofoo D. foobarfoobarfoo E. barbarbarbarbar F. foofoofoobarbar G. foofoofoobarfoo

【答案】D

【參考章節】2-3 多型

【試題解析】

繼承關係類別中,如果子類別命名了相同名字的實體變數(Instance variable),參考 的型別會影響到存取到哪個類別的實體變數。至於第6.7行沒透過實體就可以直接存取是 因為該實體變數宣告成static。

Add method to the Beta class to make it compile correctly.

class Alpha {

public void bar(int… x) {}

public void bar(int x) {}

}

public class Beta extends Alpha { 1.

2.

3.

}

Methods:

在文檔中 試題試題試題試題 1. (頁 37-51)

相關文件