Java 异常处理练习题及答案 - 测试你的编程技能
10道Java异常处理练习题,测测你的编程水平
1. 以下程序的执行结果是()
1 public class Test {
2 public static void main(String [ ] args) {
3 new Test( ).test( );
4 }
5 public void test( ){
6 try {
7 System.out.print ("try");
8 } catch (ArrayIndexOutOfBoundsException e) {
9 System.out.print ("catch1");
10 } catch (Exception e) {
11 System.out.print ("catch2");
12 } finally {
13 System.out.println ("finally");
14 }
15 }
16 }
A. try finally B. try catch1 finally C. try catch2 finally D. finally
答案:C
解析:
- 程序中没有出现
ArrayIndexOutOfBoundsException异常,所以不会执行catch1代码块。 Exception是所有异常的父类,所以会执行catch2代码块。finally代码块无论是否有异常都会执行。
2. 以下代码的执行结果正确的是 ()
1 public class Animal {
2 public static void main(String[] args) {
3 try {
4 int num1 = 8;
5 int num2 = 0;
6 int result = num1 / num2;
7 System.out.println(result);
8 } catch (ArrayIndexOutOfBoundsException e) {
9 System.out.println("catch");
10 } catch (Exception e) {
11 System.out.println("catch2");
12 } finally {
13 System.out.println("finally");
14 }
15 }
16 }
A. 0,finally B. catch,finally C. catch2,finally D. finally
答案:B
解析:
- 代码中出现了
ArithmeticException异常,因为除数为 0。 ArithmeticException是Exception的子类,所以不会执行catch2代码块。finally代码块无论是否有异常都会执行。
3. 在java中有如下方法:
1 public int count(int i) throws Exception{
2 if(i<0){
3 throw new Exception("参数不正确");
4 }
5 return i;
6 }
对该方法调用正确的是() A. public void useCount(){ count(8); } B. public void useCount() throws ArithmeticException { count(8); } C. public void useCount(){ try { count(8); } catch (Exception e) { e.printStackTrace(); } } D. public void useCount(){ try { count(8); } }
答案:C
解析:
count方法声明抛出了Exception异常,所以在调用该方法时需要捕获该异常。- 选项 A 没有捕获异常,编译错误。
- 选项 B 捕获了
ArithmeticException异常,但count方法可能抛出其他类型的异常,所以不能完全捕获异常,编译错误。 - 选项 D 捕获了异常,但没有处理异常,可能导致程序崩溃。
- 选项 C 捕获了异常并打印了异常信息,是正确的调用方法。
4. 在Java中,尝试对null对象操作时,会产生( )类型的异常 A. ArithmeticException B. NullPointerException C. IOException D. ClassNotFoundException
答案:B
解析:
NullPointerException表示空指针异常,当程序试图对一个空对象进行操作时就会抛出该异常。
5. 在Java中,以下程序编译运行后的输出结果是( )。
1 public class Test{
2 public void add(int i) {
3 if(i == 0)
4 throw new NullPointerException();
5 System.out.println("add出现异常");
6 }
7 public static void main(String[] args) {
8 Test t = new Test();
9 try {
10 t.add(0);
11 System.out.println("add方法返回");
12 }catch(Exception e) {
13 System.out.println("捕获异常");
14 }
15 }
16 }
A. add出现异常 B. add出现异常 add方法返回 C. add方法返回 捕获异常 D. 捕获异常
答案:B
解析:
add方法中,当i为 0 时,会抛出NullPointerException异常。try-catch语句捕获了Exception异常,所以会执行catch代码块,打印出 "捕获异常"。try代码块中剩余的代码不会执行,所以不会打印出 "add方法返回"。
6. 在Java中,以下代码段的输出结果为()
public static void main(String[]args){
Int[] nums=new int[3];
for(int i=1;i<nums.length;i++){
nums[i]=i*2;
}
for(int i=0;i<nums.length;i++){
System.out.print(nums[i]);
}
}
A. 024 B. 246 C. 运行时出现异常 D. 编译错误
答案:A
解析:
- 代码中,
nums[0]没有被赋值,所以会输出默认值 0。 - 循环从
i=1开始,所以nums[1]和nums[2]分别被赋值为 2 和 4。
7. 在Java中,以下代码段的输出结果为( )。
public static void main(String[] args) {
String[] girls = new String[4];
girls[0] = "alise";
girls[1] = "mary";
girls[2] = "keirly";
for(int i = 1; i<girls.length ; i++){
System.out.print(girls[i]+",");
}
}
A. alise,mary,keirly B. 运行时出现NullPointerException异常 C. alise,mary,keirly,null, D. mary,keirly,null,
答案:D
解析:
- 代码中,数组
girls的长度为 4,但只赋值了girls[0]、girls[1]和girls[2],girls[3]的值为空。 - 循环从
i=1开始,所以会输出girls[1]、girls[2]和girls[3]的值,分别为 "mary"、"keirly" 和 null。
8. 以下代码输出结果为()
public class Test{
public static void main(String []args){
List<Integer> list = new ArrayList<Integer>();
list.add(123);
list.add("abc");
list.remove(1);
System.out.println(list.get(0));
}
}
A. 编译时发生错误 B. 正确运行,输出:123 C. 正确运行,输出 “abc” D. 运行时引发异常
答案:D
解析:
- 代码中,
list.add("abc");试图将字符串 "abc" 添加到Integer类型的列表中,导致编译错误。
9. java中异常的顶层父类是 ( ) A. Throwable B. Exception C. Error D. Throws
答案:A
解析:
Throwable是 Java 中所有异常的父类,它有两个子类:Exception和Error。Exception代表可恢复的异常,Error代表不可恢复的错误。Throws是一个关键字,用于声明方法可能抛出的异常。
10. 给定如下Java程序,这段程序编译运行后的输出结果是( )。
1 public class Test {
2 public static void aMethod() throws Exception {
3 try{
4 throw new Exception();
5 }finally{
6 System.out.print("Finally");
7 }
8 }
9 public static void main(String[] args) {
10 try{
11 aMethod();
12 }catch(Exception e) {
13 System.out.print("Exception");
14 }
15 System.out.print("Finished");
16 }
17 }
A. FinallyException B. Finally C. ExceptionFinallyFinished D. FinallyExceptionFinished
答案:A
解析:
aMethod方法中,try代码块抛出了Exception异常。finally代码块无论是否有异常都会执行,所以会打印出 "Finally"。main方法中,try代码块捕获了Exception异常,所以会执行catch代码块,打印出 "Exception"。finally代码块执行后,程序继续执行,打印出 "Finished"。- 由于
finally代码块和catch代码块都打印了内容,所以最终的输出结果是 "FinallyException"。
原文地址: https://www.cveoy.top/t/topic/oazE 著作权归作者所有。请勿转载和采集!