close

1. Encapsulation 封裝    //存取權限
2. Inheritance 繼承       //繼承別人的內容
3. Polymorphism 多型   //一個物件多種型態,目的是要抽象化,修改彈性更大

= P.I.E

教材P121,122

 

1. Which is true?

Given:

5. class Building { }
6. public class Barn extends Building {
7.   public static void main(String[] args) {
8.      Building build1 = new Building();
9.      Barn barn1 = new Barn();
10.    Barn barn2 = (Barn) build1;
11.    Object obj1 = (Object) build1;
12.    String str1 = (String) build1;
13.    Building build2 = (Building) barn1;
14.  }
15.}
 
A. If line 10 is removed, the compilation succeeds.
B. If line 11 is removed, the compilation succeeds.
C. If line 12 is removed, the compilation succeeds.
D. If line 13 is removed, the compilation succeeds.
E. More than one line must be removed for compilation to succeed.

答案: E

第10行,build1升級至barn

第11行,build1升級至Object是OK的,執行會失敗但編譯會成功。

第12行building和String沒有親子關係

 

 

2. When is the Float object created in line 3, eligible for garbage collection ?

Given:
1. public class X {
2.  public Object m (){
3.   Object o = new Float (3.14F);
4.   Object [] oa = new Object [1];
5.   oa[0] = o;
6.   o = null;
7.   oa[0] = null;
8.  return o;
9.  }
10.}


A. Just after line 5
B. Just after line 6
C. Just after line 7
D. Just after line 8 (that is, as the method returns)
E. Never in this method


答案: C

oa[0] = null;這之後o的值就是0了,主程式也會被銷毀。

 

 

3. When is the Float object created in line 3, eligible for garbage collection ?

Given:
1. public class X {
2. public Object m(){
3.  Object o = new Float (3.14F);
4.   Object [] oa = new Object [1];
5.   oa[0]= o;
6.   o = null;
7.  return oa[0];
8.  }
9. }

object 物件
created 建立
line 行
eligible 有資格的, 合適的
garbage 垃圾
collection 收集


A. Just after line 5
B. Just after line 6
C. Just after line 7 (that is, as the method returns)
D. Never in this method

 

這一題 考的是對物件存取參照的了解。當Stack區的主副程式執行完後,會自動被系統清掉。但Heap區的物件會被留著,直到沒有其他物件再參照他時,Java的gc();就會在系統不忙時出來將這些物件清掉。

所以這一題首先需要寫一個main(); main的內容會影響答案

publich void static main(String[] args){
 X a = new X();  //先new一個X的實體,把它存到一個名為a 型態為X的變數
 Object b = a.m();     //再透過a呼叫裡面的method m();,把它的回傳值存到一個名為Object型態的變數b

}

接著,Stack區生成一個Float物件,內容是3.14F的數值;Stack區再

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 鈴木保齡球 的頭像
    鈴木保齡球

    Java程式學習手札

    鈴木保齡球 發表在 痞客邦 留言(0) 人氣()