【17-06-19】Java進階自測:面向對象基礎知識掌握了嗎?(附答案及我的解析)

描述

題目和答案來自於阿里雲大學 - 知乎專欄java

題目

  1. 如今有以下一段代碼
    java public class Test { public int aMethod() { static int i=0; i++; return i; } public static void main(String args[]) { Test test = new Test(); test.aMethod(); int j = test.aMethod(); System.out.println(j); } }
    將產生哪一種結果:數組

    A. Compilation will failapp

    B. Compilation will succeed and the program will print「0」this

    C. Compilation will succeed and the program will print「1」阿里雲

    D. Compilation will succeed and the program will print「2」code

  2. 如要在字符串s(內容爲「welcome to mldn !! 」),中,發現字符't'的位置,應該使用下面哪一種方法?對象

    A.mid(2,s);字符串

    B. charAt(2);get

    C. s.indexOf('t');it

    D. indexOf(s,'v');

  3. 編譯和運行下面代碼可能會發生什麼?
    java class Base { private void amethod(int iBase) { System.out.println("Base.amethod"); } } class Over extends Base { public static void main(String args[]) { Over o = new Over(); int iBase = 0 ; o.amethod(iBase) ; } public void amethod(int iOver) { System.out.println("Over.amethod"); } }
    A. Compile time error complaining that Base.amethod is private

    B. Runntime error complaining that Base.amethod is private

    C. Output of Base amethod

    D. Output of Over.amethod

  4. 如今有以下一段程序
    java class super { String name ; public super(String name) { this.name = name ; } public void fun1() { System.out.println("this is class super !"+name); } } class sub extends super { public void fun1() { System.out.println("this is class sub !"+name); } } class Test { public static void main(String args[]) { super s = new sub(); } }
    運行上面的程序可能會出現的結果?

    A. this is class super !

    B. this is class sub !

    C. 編譯時出錯

    D. 運行時出錯

  5. 如今有以下一段程序
    java class Happy { public static void main(String args[]) { float [][] f1 = {{1.2f,2.3f},{4.5f,5.6f}} ; Object oo = f1 ; f1[1] = oo ; System.out.println("Best Wishes "+f1[1]); } }
    該程序會出現何種效果?

    A. {4.5,5.6}

    B. 4.5

    C. compilation error in line NO.5

    D. exception

  6. 在一個類文件中,導入包、類和打包是怎樣的排列順序?

    A. package、import、class;

    B. class、import、package

    C. import、package、class

    D. package、class、import

  7. 若是你試圖編譯並運行下列代碼時可能會打印輸出什麼?
    java int i = 9 ; switch(i) { default: System.out.println("default"); case 0 : System.out.println("zero"); break ; case 1 : System.out.println("one"); case 2 : System.out.println("two"); }
    A. default

    B. default , zero

    C. error default clause not defined

    D. no output displayed

  8. 當你編譯下列代碼可能會輸出什麼?
    java class Test { static int i ; public static void main(String args[]) { System.out.println(i); } }
    A. Error Variable i may not have been initialized

    B. null

    C. 1

    D. 0

  9. 下面代碼會存在什麼問題?
    java public class MyClass { public static void main(String arguments[]) { amethod(arguments); } public void amethod(String[] arguments){ System.out.println(arguments); System.out.println(arguments[1]); } }
    A. 錯誤,void amethod()不是static類型

    B. 錯誤,main()方法不正確

    C. 錯誤,數組必須導入參數

    D. 方法amethod()必須用String類型描述

  10. 爲Demo類的一個無形式參數無返回值的方法method書寫方法頭,使得使用類名Demo做爲前綴就能夠調用它,該方法頭的形式爲?

    A. static void method( )

    B. public void method( )

    C. final void method( )

    D. abstract void method( )

答案

ACDCC ABDAA

我的解析

  1. 在方法體內聲明的變量是「局部變量」,而局部變量是不能用static修飾的,private、protected、public也是不能用的。

  2. indexOf是String類的一個方法,做用是查找第一次出現參數的位置,沒有則返回-1。

  3. 不管amethod方法是否是private,結果都是執行子類的amethod方法。區別是,若是不是private,子類的amethod方法是重寫了父類的方法;若是是private,子類的amethod方法並無重寫父類的方法。

  4. Java中,若是類裏沒有寫構造方法,那麼會默認有一個無參的構造方法。可是一旦手動寫了構造方法,那麼默認的無參構造方法就沒有了。這道題是由於父類只有一個有參的構造方法,可是子類卻沒有,因此編譯出錯。

  5. Java中的數組是對象,因此第四行沒有問題。而f1[1]須要的是一個數組而且是一維數組,因此第五行編譯出錯。

  6. 在default中進入,在case 0中由於break跳出。

  7. 基本數據類型都有相應的默認值,其中int是0,char爲‘\u0000’,boolean爲false。

  8. 靜態方法沒法調用非靜態方法。

  9. 靜態方法能夠用類名.方法名直接調用。

相關文章
相關標籤/搜索