# :-: 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. 控制台查看指定学生的全部成绩信息(从高到低逆序排序)
~~~