1. What is the value of j at line 16 ?

Click the exhibit button:
1. int i=1, j=0;
2.
3. switch(i){
4. case 2:
5.   j+=6;
6.
7.  case 4:
8.   j+=1;
9.
10.  default:
11.   j +=2;
12.
13.  case 0:
14.   j +=4;
15.}
16.   


A. 0
B. 1
C. 2
D. 4
E. 6

My Ans: 2

答案: E, 沒有寫"break;"所以執行完第一個就結束。如果i=2,答案會是13。

 

2. What's the output ?

class Language{
    public static final int java=1;
    public static final int pascal=2;
    public static final int csharp=3;
    public static final String dotNet = "4";
}

public class Mgos  {
    static String lang="4";
    public static void main(String argv[]){
        switch(lang){
        case Language.java:
            System.out.println("java");
            break;
        case Language.dotNet:
            System.out.print("dotNet");
        case Language.pascal:
            System.out.println("pascal");
            break;
        case Language.csharp:
            System.out.println("csharp");
            break;
        }
    }
}


A. java
B. dotNet
C. pascal
D. csharp
E. None of the above

My Ans: E

答案: public static final int java=1;和 static String lang="4"; int和String無法做比較,因此變數需要全部改成int或全部改成String. 如果改好,output會是"dotNetpascal"

 

------------------------------------------------

建構式的功能: 初始化一個屬性。

建構式(Constructor)必須符合以下規定
1. 不能有回傳值, 也不能有void宣告
2. method名稱須與class名稱相同, 包含大小寫
3. 必須是動態
4. 建構式第一行一定要呼叫父類別中的其中一個建構式( super(參數); )或是呼叫同class中的另一個建構式( this(參數); ), 如果沒有做到, 則Java會自動在第一行增加super();
5. 如果class原始碼中沒有任何建構式則編譯器將自動加上預設建構式(不收參數的建構式).

------------------------------------------------

 

3. What is the result ?

Click the exhibit button:
1. class Super {
2. public int i = 0;
3.
4. public Super(String text){
5.   i = 1;
6. }
7. }
8.
9. public class Sub extends Super {
10.  public Sub (String text){
11.   i = 2;
12.  }
13.  public static void main (String args[]){
14.   Sub sub = new Sub ("Hello");
15.      System.out.println(sub.i);
16.     }
17. }


click 按
exhibit 展示,陳列
button 按鈕


A. Compilation will fail.
B. Compilation will succeed and the program will print "0".
C. Compilation will succeed and the program will print "1".
D. Compilation will succeed and the program will print "2".

My ans: B

答案: A, 電腦會自動在10和11行中間加入"super();"這個預設建構式來滿足extends Super覆類別的宣告。但JVM往上找,會找不到這個不帶參數的super();(頂多有public Super(String text)),所以編譯會失敗。參考建構式(Constructor)必須符合以下規定-第5點。要讓程式編譯成功,可以手動加入super(text);或修改10&11中間的

 

4. What is the result ?

Click the exhibit button:
1. class Super {
2.  public int i = 0;
3.
4.  public Super(String text){
5.    i = 1;
6.   }
7. }
8.
9. public class Sub extends Super {
10.  public void Sub (String text){
11.   super(text);
12.  }
13.  public static void main (String args[]){
14.   Sub sub = new Sub ("Hello");
15.  System.out.println(sub.i);
16. }
17. }


A. Compilation will fail.
B. Compilation will succeed and the program will print "0".
C. Compilation will succeed and the program will print "1".
D. Compilation will succeed and the program will print "2".

My ans: C

答案: A, 第10行。一般method不可以呼叫建構式,否則會編譯失敗。

 

5. What will happen or printed out when you attempt to compile and run the class ?

private class Base{
   protected Base(){
      System.out.println("Base");
   }
}
public class Test extends Base{
   public static void main(String[] args){
      Test a=new Test();
   }
}

successfully 成功
runtime exception 執行失敗
none 沒有
above 上面
none of the above 以上皆非


A. compile successfully, and output Base
B. compile error , constructor can't be protected
C. compile error , class Test has no constructor
D. compile successfully, but no output
E. runtime exception
F. none of the above

My Ans: C

答案: F, 第一個字"private",class 不能加private,編譯會失敗。如果去掉private,編譯會成功,最後會是選項A的答案。要考慮看不到的程式碼: JVM會將Base extends Object. 父類別寫protect,子類別、同package類別都可以呼叫引用。

 

 

 

 

 

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

    Java程式學習手札

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