# :-: Java綜合測試
## 選擇題與填空(共25題,每題2分)
1. 下列代碼中的異常屬于()
~~~
int a = 0;
System.out.println(2 / a);
~~~
~~~
A. 非檢查型異常
B. 檢查型異常
C. Error
D. Exception
~~~
2. ()類及其子類所表示的異常是用戶程序無法處理的。
~~~
A. NumberFormatException
B. Exception
C. Error
D. RuntimeException
~~~
3. 數組下標越界,則發生異常,提示的異常為()
4. 運行下列代碼,當輸入的 num 值為 a 時,系統會輸出()
~~~
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
int num = input.nextInt();
System.out.println("one");
} catch(Excetion e) {
System.out.println("two");
} finally {
System.out.println("three");
}
System.out.println("end");
}
}
~~~
5. 運行下列代碼,輸出結果為()
~~~
public class Test {
public static void main(String[] args) {
try {
int a = 1 - 1;
System.out.println("a = " + a);
int b = 4 / a;
int c[] = {1};
c[10] = 99;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("索引異常");
} catch (ArithmeticException e) {
System.out.println("除數不允許為零");
}
}
}
~~~
6. 下列關于異常的描述,錯誤的是()
~~~
A. printStackTrace() 用來跟蹤異常事件發生時執行堆棧的內容
B. catch 塊中可以出現同類型異常
C. 一個 try 塊可以包含多個 catch 塊
D. 捕獲到異常后將輸出所有 catch 語句塊的內容
~~~
7. 假設要輸入的 id 值為 a101,name 值為 Tom,程序的執行結果為()
~~~
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
int id = input.nextInt();
String name = input.next();
System.out.println("id=" + id + ", name=" + name);
} catch(InputMismatchException e) {
System.out.println("輸入數據不合規范");
System.exit(1);
e.printStackTrace();
} finally {
System.out.println("輸入結束");
}
}
}
~~~
8. 下列代碼的運行結果為()
~~~
public class Test {
public static int test(int b) {
try {
b += 10;
return b;
} catch(Exception e) {
return 1;
} finally {
b += 10;
return b;
}
}
public static void main(String[] args) {
int num = 10;
System.out.println(test(num));
}
}
~~~
9. 下列關于這段代碼的說法正確的是()
~~~
public class Test {
public static void test(String str) throws Exception {
if (null == str || str.length() == 0) {
throw new Exception("參數不能為空");
} else {
System.out.println("str=" + str);
}
}
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
String str = input.nextLine();
test(str);
}
}
~~~
~~~
A. 代碼錯誤,沒有對 test() 方法中拋出的異常進行處理
B. 若產生異常,將由系統進行異常處理
C. 本段代碼對 throw 拋出異常對象的處理方案為自己拋出異常自己處理
D. 若輸入空字符串,代碼運行結果為:
Exception in thread "main" java.lang.Exception: 參數不能為空
~~~
10. 在下列代碼劃線處不可以填入選項中的哪一個異常類型()(選擇一項)
```
public static int test(int a, int b) throws _______ {
if (b == 0) {
throw new AritheticException("算術異常");
} else {
return (a / b);
}
}
```
```
A. Throwable
B. Exception
C. InputMismatchException
D. ArithmeticException
```
11. 關于下列代碼說法正確的是()(選擇一項)
```
class MyException extends Exception {
public MyException() {
super("The price is too low!");
}
}
public class Test {
public static void main(String[] args) {
Scanenr input = new Scanner(System.in);
float price = input.nextFloat();
try {
if (price < 10) {
throw new MyException();
} else {
System.out.println("the price is " + price);
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
```
```
A. 編譯出錯,因為沒有捕獲 Exception 異常
B. 當輸入 price 的值為 10,運行結果為:The price is too low!
C. 當輸入 price 的值為 9時,運行結果為:the price is 9。0
D. 當輸入 price 的值為 0時,運行結果為:The price is too low!
```
12. 若我們想要在位置 1 處拋出異常的同時保留 MyException 中的異常信息,則可以在位置 1 中添加哪句代碼()(選擇一項)
```
public static void methodOne() throws MyException {
throw new MyException();
}
public static void methodTwo() throws Exception {
try {
methodOne();
} catch(MyException e) {
// 位置 1
}
}
```
```
A. throw new Exception("新異常", e);
B. throw new Exception("新異常", ex);
C. throw new Exception("新異常");
D. throw new MyException(e);
```
13. 請寫出所有的包裝類()
14. 下面代碼運行的正確結果是()
~~~
public class BuildStuff {
int test(Boolean b, int i) {
if (b) {
return i / 7;
}
return i / 49;
}
public static void main(String[] args) {
Boolean bool = new Boolean(true);
Integer x = 343;
Integer y = new BuildStuff().test(bool, x);
System.out.println(y);
}
}
~~~
15. 下面代碼運行的正確結果是
~~~
public class Wrap {
Integer i;
int x;
public Wrap(int y) {
x = i + y;
System.out.println(x);
}
public static void main(String[] args) {
new Wrap(new Integer(4));
}
}
~~~
16. 下面代碼運行的正確結果是()
~~~
public static void main(String[] args) {
Integer i = new Integer(1) + new Integer(2);
switch (i) {
case 3 :
System.out.println("hello");
break;
default:
System.out.println("world");
break;
}
}
~~~
17. 給出如下語句,寫出運行結果()
~~~
String str = "hello,world";
str = str.substring(2,5);
char ch = str.charAt(str.length());
System.out.println(ch);
~~~
18. 給出如下語句,寫出運行結果()
~~~
String str = "abcdefg";
char ch = str.substring(3, 6).charAt(1);
System.out.println(ch);
~~~
19. 關于字符串的 equals() 和 compareTo() 方法,選項中描述錯誤的是()
~~~
A. 方法 equals() 比較兩個字符串內容是否相等
B. 方法 compareTo() 比較兩個字符串大小
C. 方法 equals() 返回值是 boolean 類型的值
D. 方法 compareTo() 返回值是 String 類型的值
~~~
20. 已知如下代碼,運行結果為()
~~~
String str1 = new String("hello");
String str2 = "hello";
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
System.out.println(str1);
System.out.println(str1.concat(str2));
System.out.println(str1)
~~~
21. 給出如下語句:請寫出空白處代碼,使得得到輸出結果“123abc 123abc”。
~~~
StringBuilder sb1 = new StringBuilder("123");
String s1 = "123";
// ....
System.out.println(sb1 + " " + s1)
~~~
22. 已知 ArrayList 的對象 list,請寫出判斷 ArrayList 中是否包含“helloworld”的語句。
~~~
~~~
23. 以下關于 Set 對象的創建錯誤的是()
~~~
A. Set set = new Set();
B. Set set = new HashSet();
C. HashSet set = new HashSet();
D. Set set = new HashSet(10);
~~~
24. 關于 Iterator 的描述錯誤的是()
~~~
A. Iterator 可以對集合 Set 中的元素進行遍歷
B. hasNext() 方法用于檢查集合中是否還有下一個元素
C. next() 方法返回集合中的下一個元素
D. next() 方法返回值為 false 時,表示集合中的元素已經遍歷完畢
~~~
25. HashMap 的數據是以 key-value 的形式存儲的,以下關于HashMap的說法正確的是()
~~~
A. HashMap 中的鍵不能為 null
B. HashMap 中的 Entry 對象是有序排列的
C. key 值不允許重復
D. value 值不允許重復
~~~
## 簡答題(共三題,共計30分)
1. 請寫出利用File類實現文件覆蓋的方法
~~~
~~~
2. 請寫出super,this,super(),this()的區別
~~~
~~~
3. 請寫出線程的五種狀態,并標明有哪些原因會導致這五種狀態
~~~
~~~
## 編程題(共1題,共計20分)
編寫代碼完成如下要求:
* 完成學生類 Student 的編寫
~~~
屬性:姓名、年齡、學科、成績等
~~~
* 完成文件操作類 FileOperate 的編寫
~~~
功能:void write(List<T> list) 將集合數據寫入文件
List<T> read(File file) 讀取文件中存儲的集合數據
~~~
* 完成學生管理類 StudentManage 的編寫
~~~
功能:
1. 控制臺錄入學生考試成績(學號、姓名、學科、成績),退出當前菜單時將學生信息保存至文件中
2. 控制臺修改學生考試成績(學號、學科),退出當前菜單時將學生信息保存至文件中
3. 控制臺查看指定學科的全部學生成績(從高到低逆序排序)
4. 控制臺查看指定學生的全部成績信息(從高到低逆序排序)
~~~