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。try 块中没有抛出异常,所以不会执行 catch 块;finally 块无论如何都会执行,所以输出 try catch2 finally。

  1. 以下代码的执行结果正确的是 ()
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。num2 为 0,会抛出 ArithmeticException 异常,但是 catch 块并没有捕获该异常,所以执行 finally 块后,程序直接结束,输出 catch,finally。

  1. 在 java 中有如下方法:
1 public int count(int i) throws Exception{
2 if(i<0){
3 throw new Exception('参数不正确');
4 }
5 return i;
6 }

对该方法调用正确的是() A. ```java public void useCount(){ count(8); }

B. ```java
public void useCount() throws ArithmeticException {
count(8);
}

C. ```java public void useCount(){ try { count(8); } catch (Exception e) { e.printStackTrace(); } }

D. ```java
public void useCount(){
try {
count(8);
}
}

答案:C。方法 count() 抛出了 Exception 异常,所以调用它的方法必须要进行异常处理,可以使用 try-catch 块捕获异常,也可以使用 throws 关键字声明抛出异常。

  1. 在 Java 中,尝试对 null 对象操作时,会产生( )类型的异常 A. ArithmeticException B. NullPointerException C. IOException D. ClassNotFoundException

答案:B。当对 null 对象进行操作时,会抛出 NullPointerException 异常。

  1. 在 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 方法会抛出 NullPointerException 异常,但是在 catch 块中没有捕获该异常,所以 finally 块执行后程序结束,输出 add 出现异常。

  1. 在 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. 编译错误

答案:B。循环中对 nums[0] 没有进行赋值,所以输出的结果为 246。

  1. 在 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。循环从下标 1 开始,所以输出的结果为 mary,keirly,null。

  1. 以下代码输出结果为()
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 中添加的是 Integer 类型的数据,但是尝试添加 String 类型的数据会引发 ClassCastException 异常,所以输出结果为运行时引发异常。

  1. java 中异常的顶层父类是 ( ) A. Throwable B. Exception C. Error D. Throws

答案:A。Throwable 是 Java 中所有错误和异常的顶层父类。

  1. 给定如下 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

答案:C。aMethod() 抛出了 Exception 异常,但是在 finally 块中输出了 Finally,所以程序会输出 ExceptionFinallyFinished。

Java 练习题:选择并解释答案

原文地址: https://www.cveoy.top/t/topic/oaAo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录