JAVA GA(3)

第1題

題目:
Which one statement is true?  

(A) Has-a relationships should never be encapsulated.(封装) 

(B) An array or a collection can be used to implement a one-to-many has-a relationship. 

(C) Has-a relationships can be implemented using instance variables. (实体)

(D) Is-a relationships can be implemented using the extends keyword.

你的答案: D

正確答案: A      (这题好像答案有误??)

錯誤

[詳解] 
is-a:指類別間的繼承關係、has-a:指類別間的包含關係



第2題

題目:
class Yoo{ 
public void addFive(){ 
System.out.println("Y");} 

class Boo extends Yoo{ 
public void addFive(){   
System.out.println("B");} 

class Woo extends Yoo{ 
public void addFive(){  
System.out.println("W");} 

public class OO { 
public static void main(String[] args) { 
(A)Yoo y=new Boo(); 
   y.addFive(); 
(B) Boo b=(Boo)y;   
   b.addFive(); 
(C) Woo w=(Woo)y; 
   w.addFive();  
(D)Boo by=(Boo)new Yoo(); 
   by.addFive(); 



下列結果何者正確? 
(A) Y 
(B) Y 
(C) 編譯時發生錯誤 
(D) 執行時發生錯誤

你的答案: B

正確答案: D

錯誤

[詳解] 
(A) B,因為宣告父代產生子代,方法是用子代的方法 
(B) B,因為y物件的方法是B即使轉型為Boo,本質仍不變,仍為B 
(C) 因為兄弟(Boo、Woo)間不能互相轉型,即使編譯會過,但執行時會發生錯誤 
(D) 因為宣告子代產生父代,即使轉型,編譯會過,但執行時會發生錯誤



第3題

題目:
11.class Hi { 
12.    String title; 
13.    int num=3; 
14.    public Hi() { 
15.        title += " My HEART "+num*52; 
16.    } 
17.    public Hi(int num) { 
18.        this.num = num; 
19.        title = "Hi"; 
20.        Hi(); 
21.    } 
22. public void Hi() { 
23. title +=" Baby "+num*2; 
24. } 
25.} 
26.public class Hey { 
27. public static void main(String[] args) { 
28. Hi h = new Hi(10); 
29. System.out.print(h.title); 
30. } 
31.} 

下列結果何者正確? 
(A) Hi Baby 20 
(B) Hi My HEART 520 
(C) Hi Baby 6 
(D) Hi My HEART 156

你的答案: B

正確答案: A

錯誤

[詳解] 
◎ 建構子:建構子不能直接用Hi()呼叫,只有在類別實體化<hi h = new hi(10);>時,建構子才會被呼叫。所以< Hi()>這是方法的呼叫 



第7題

題目:
Given: 
31. class A{ 
32.     public int i = 2; 
33.     public void subOne(){ i -= 1; System.out.print("A "); } 
34. } 
35. class B extends A{ 
36.     public int i = 5; 
37.     public void subOne(){i -= 1; System.out.print("B ");} 
38. } 
Invoked with: 
    A a = new B(); 
    a.subOne(); 
    System.out.println(a.i); 
編譯結果為何? 
(A)B 4 
(B)B 2 
(C)A 1 
(D)A 1   

你的答案: A

正確答案: B

錯誤

[詳解] 
宣告父代產生子代,屬性是父代,方法為子代。 
子類別繼承父類別,但是改寫父類別既有的函式,子代覆寫父代方法,執行時當然是執行子代的方法。 



第9題

題目:
Given: 

1. class Father{ 
2.     private int h; 
3.     protected Father(int h){this.h = h;} 
4. } 
... 
11. class Son extends Father{ 
12.     public Son(int h){super(h);} 
13.     public Son(){this.h = 170;} 
14. } 
在Son類別中出了一些問題,導致程式無法編譯,軟工二的學生們分別提出了幾個改進的方法,請問以下哪些學生的方法,能使Son類別成功編譯? 

(小白) 
將第2的private改成public 
(小黑) 
將第2行的private改成protected 
(小天) 
將第13行的this.h = 170改成this(170) 
(小花) 
將第13行的this.h = 170改成super(170) 
(小明) 
將第13行的this.h = 170改成super(h) 
(A)小白、小花 
(B)小黑、小天 
(C)小明、小花 
(D)小花、小天 

[概念比率:建構子80% 繼承20%] 


你的答案: A

正確答案: D

錯誤

[詳解] 
1.建構子的第一行如果沒有寫super 或是this 的話,就會給定預設super() 在建構子的第一行 
2.呼叫Super(父代)的Constructor 使用 super(...);,呼叫同一個class其他的Constructor使用this(...); 



第12題

題目:
class A { 
    String name = "A"; 
    String getName() { 
        return name; 
    } 
    String greeting() { 
        return "class A"; 
    } 

class B extends A { 
    String name = "B"; 
    String greeting() { 
        return "class B"; 
    } 

public class Client { 
    public static void main(String[] args) { 
        A a = new A(); 
        A b = new B(); 
        System.out.println(a.greeting() + "has name" + a.getName()); 
        System.out.println(b.greeting() + "has name" + b.getName()); 
    } 

Which option is correct? 

(A) class B has name A 
   class A has name A 
(B) class A has name A 
   class B has name A 
(C) class A has name A 
   class B has name B 
(D) class B has name B 
   class A has name A

你的答案: C

正確答案: B

錯誤

[詳解] 
◎ 宣告父代,產生父代,方法用父代的方法,屬性用父代的 
   宣告父代,產生子代,方法用子代的方法,屬性用父代的 
◎ 但getName()是用A的getName()方法,因為B類別沒有getName()方法,所以A的getName()即回傳A的name。

posted on 2016-12-20 21:10  xcshehe  阅读(164)  评论(0编辑  收藏  举报

导航