JAVA核心技術I---JAVA基礎知識(回顧)
阿新 • • 發佈:2018-12-28
一:物件例項化問題:
public class Rectangle { public int width = 3; public int height = 4; public int area() { return width * height; } } 則如下程式碼輸出結果為: Rectangle rectangle; rectangle.height = 5; System.out.println(rectangle.area());
A.15 B.有編譯錯誤,程式不能執行 C.12 D.0
D、rectangle沒有被初始化,因此報錯。
Rectangle rectangle=new Rectangle()
二:型別轉換問題
如下賦值語句中,有語法錯誤的是? A.float f1 = 1.2; B.float f1 = 1.2f C.float f1 = 1; D.float f1 = 0xAE;\
注意double型別可以不加字尾,所以小數不加上f字尾,則預設為double型別,若不是強制轉換則編譯不通過。
三:switch語句
對於Java1.7及之後版本,如下不能用於switch的型別是: A.String B.int C.char D.double
double和float是不精確的數值,不用於判斷,但是String型別可以在1.7後使用
四:賦值問題
如下賦值語句,有編譯錯誤的是? A.byte b = -127; B.int i = (byte)512; C.byte b = 129; D.byte b = -0;
byte b = (byte) 129; 需要強制轉,且結果是-2.
byte型別-127~128
五:編譯問題:
Java有“一次編譯,到處執行”的說法,此種說法中編譯的結果是:
A.機器碼
B.符號表
C.位元組碼
D.中間程式碼
class檔案,即為位元組碼(bytecode)檔案。
六:main函式:
下列關於main方法的描述中,錯誤的是? A.main方法是Java程式的入口 B.main方法格式為 public static void main(String[] args) { //Your code here } C.B選項中所描述格式中形參args不能更改,如果將args改為arguments則不能編譯通過 D.main方法可以被過載
形參名字可以隨意更改,形參型別不可以更改,必須是String[].
main方法可以被過載。可以在類中寫一個同名函式,引數等不一致,是過載,符合要求
七:建構函式問題(重點)
Given the following class class MyNumber { private int num = 5; public MyNumber(int num) { this.num = num; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } What is output after the executation of following code? MyNumber obj1 = new MyNumber(); MyNumber obj2 = new MyNumber(10); obj2 = obj1; obj2.setNum(20); System.out.println(obj1.getNum() + “,” + obj2.getNum());
A.5, 20 B.5, 10 C.20,20 D.編譯錯誤
MyNumber有一個帶形參的建構函式,編譯器不會自動新增無參的建構函式。因此在建立obj1的時候,MyNumber obj1 = new MyNumber();,找不到相應的建構函式,報錯。
八:物件賦值問題
Given the following class: class Mixer { Mixer() { } Mixer(Mixer m) { m1 = m; } Mixer m1; public static void main(String[] args) { Mixer m2 = new Mixer(); Mixer m3 = new Mixer(m2); m3.go(); Mixer m4 = m3.m1; m4.go(); Mixer m5 = m2.m1; m5.go(); //m1中m1未被賦值,為null,呼叫方法會報錯 } void go() { System.out.print("hi "); } }
A.Compilation fails
B.hi hi hi
C.hi hi, followed by an exception
D.hi, followed by an exception