單選 3.下面哪個不是Java內建的Annotation?
A. @Override
B. @Deprecated
C. @SuppressWarning
D. @FunctionalInterface
// 該題地方答案是C,正確的忽略警告的註解是@SuppressWarnings
// 考察基礎:對於java常見的基本註解的認識。
// A 註解用於覆寫的標識,防止覆寫出錯。
// B 用於標識該方法或接口已經過時了,請使用最新的方法接口。
// D 函數式功能接口描述,jdk1.8新添加的。用於描述該接口有且僅有一個抽象方法。
- 父類與子類
-
9.編譯和運行下面代碼可能會發生什麼?
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
//答案D ,解析 此題中父類與子類擁有共同簽名的方法,卻有不同的權限修飾符。
// 由於父類的私有方法對於子類不可見,所以方法重名是正確的。但是在實際編碼中這種應該是要避免的,
//以防止不必要的錯誤,和影響代碼的可讀性。
- 靜態變量,成員變量,常量賦值初始化問題
-
13.編譯下列代碼可能會輸出什麼?
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
// 答案 D ,變量沒有被賦值時,初始化時會賦予對應類型的默認值。基本類型 int 0 float 0.0F double 0.0D byte 0 short 0 boolean false char '' String null 引用變量爲null
// 局部變量的使用需要給予初始化值,成員變量會在構造對象時根據成員變量類型給與默認值。
// 常量必須給予值。
// 靜態變量 可以不給值,會使用默認值。
- 靜態變量
-
14.現在有如下一段代碼
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 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”
// 答案是A 編譯出錯。不存在局部靜態變量的概念。靜態表明此變量跟隨類存在,放在方法區。故static 不能修飾方法內不的變量,方法內部的屬於局部變量,線程私有,存在於線程的虛擬機棧中。
- 泛型
-
20.下面關於泛型的描述中錯誤的一項是?
A. “? extends 類”表示設置泛型上限
B. “? super 類”表示設置泛型下限
C. 利用“?”通配符可以接收全部的泛型類型實例,但卻不可修改泛型屬性內容
D. 如果類在定義時使用了泛型,則在實例化類對象時需要設置相應的泛型類型,否則程序將無法編譯通過
//答案 D java 泛型的實現是採用編譯時的類型擦除實現的。如果構造對象時沒有設置泛型,則使用Object 代替泛型。比如常見的集合類 ArrayList<?> 如果構造集合時,未指定泛型類型,則表明該集合可以存在任何對象類型。即Object 及其子類。Object是java裏的頂級父類。即java裏的所有類都繼承Object.
// 這裏也體現了一種設計,公共邏輯的抽象。抽象出現實世界所有一切物品的共性,由此出發來構建了java 面向對象的特性。即一切都是對象。