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

答案:B

执行 try 语句块,抛出异常,执行 catch 语句块,最后执行 finally 语句块,输出 'try catch1 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

答案:C

执行 try 语句块,抛出异常,执行 catch 语句块,最后执行 finally 语句块,输出 '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

正确的调用方式是使用 try-catch 语句块捕获异常。

4. 在Java中,尝试对null对象操作时,会产生( )类型的异常

A. ArithmeticException B. NullPointerException C. IOException D. ClassNotFoundException

答案:B

对 null 对象操作会产生 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 方法时抛出异常,被捕获后输出 'add出现异常',然后执行 finally 语句块,最后输出 '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. 编译错误

答案:B

数组 nums 的第一个元素默认值为 0,第二个元素为 2,第三个元素为 4,输出 '246'。

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 的第一个元素为 null,从第二个元素开始输出 'mary,keirly,'。

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 中添加了一个整数和一个字符串,然后移除了索引为 1 的元素,再输出索引为 0 的元素,由于移除了一个元素,所以输出时会引发 IndexOutOfBoundsException 异常。

9. java中异常的顶层父类是 ( )

A. Throwable B. Exception C. Error D. Throws

答案:A

Throwable 是所有 Java 异常的父类。

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

答案:C

执行 aMethod 方法时抛出异常,被捕获后输出 'FinallyException',最后输出 'Finished'。

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

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

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