面试题错题集04(异常)

6、现有代码: D

public class Example {

 public static void main(String[] args) {

  try {

   System.out.print(Integer.parseInt("forty"));   

  } catch (RuntimeException e) {

   System.out.println("Runtime");

  }catch (NumberFormatException e) {

   System.out.println("Number");

  }

 }

}

执行结果是什么?(单项选择题)

A、输出Number

B、输出Runtime

C、输出40

D、编译失败

// 子类异常应该在父类异常之前 

 

 

7、现有代码如下:

 

public class Example {

 

 

 

 void topGo() {

 

  try {

 

   middleGo();

 

  } catch (Exception e) {

 

   System.out.println("catch");

 

  }

 

 }

 

 

 

 void middleGo() throws Exception {

 

  go();

 

  System.out.println("late middle");

 

 }

 

 

 

 void go() throws Exception {

 

  throw new Exception();

 

 }

 

 

 

 public static void main(String[] args) {

 

  Example example = new Example();

 

  example.topGo();

 

 }

 

}

 

该代码的执行结果是?(单项选择题)

 

A、输出late middle

 

B、输出catch

 

C、输出late middle catch

 

D、输出catch late middle

// throws 向上抛异常 谁调用谁解决, 当go()发生异常时,直接抛出异常,不执行middleGo()后续代码

 

 

9、现有如下代码: B  

public class Example extends Utils{

 public static void main(String[] args) {

  try {

   System.out.println(new Example().getInt("42"));

  } catch (NumberFormatException e) {

   System.out.println("NFExc");

  }

 }

 int getInt(String arg) throws NumberFormatException{

  return Integer.parseInt(arg);

 }

}

 

class Utils {

 int getInt(String arg) {

  return 42;

 }

}

该代码执行的结果是?(单项选择题)

ANFExc

B42

C42NFExc

D、编译失败

 

// 如果父类的方法没有抛出异常,那么子类中的方法可以抛出异常,但如果父类中的方法有抛出异常,那么子类中的方法的异常不能大于父类中的异常

//Utils中的getInt方法没有抛出异常,而子类Example中的getInt抛出了运行时异常,这是符合方法覆盖的抛出异常特性规范的,因为运行时异常并不会强制要求方法调用代码捕获处理

 

 

10、现有如下代码:

public class Example extends Utils{

 public static void main(String[] args) {

  try {

   System.out.println(new Example().getInt("42"));

  } catch (NumberFormatException e) {

   System.out.println("NFExc");

  }

 }

 int getInt(String arg) throws Exception{

  return Integer.parseInt(arg);

 }

}

 

class Utils {

 int getInt(String arg) {

  return 42;

 }

}

该代码执行的结果是?(单项选择题)

ANFExc

B42

C42NFExc

D、编译失败

  // 子类抛出的异常不符合方法覆盖的异常列表要求,因此编译失败

 

 

16、请问以下哪些关于trycatchfinally结构中的finally语句的描述是正确的?(单项选择题) C

A、只有当一个catch语句获得执行后,finally语句才获得执行

B、只有当catch语句未获得执行时,finally语句才获得执行

C、如果有finally语句,return语句将在finally语句执行完毕后才会返回

D、只有当异常抛出时,finally语句才获得执行

// 执行到return时,会先去执行finally块,执行完后才会去执行return。

 

 

17、请问以下代码的直接执行结果是? A

 

class Example{

 

 public static void main(String[] args) {

 

  try {

 

   System.out.println(args[0]);

 

   System.out.println("I'm nomal");

 

   if (true)

 

    return;

 

  } catch (Exception ex) {

 

   System.out.println("I'm exception");

 

   if (true)

 

    return;

 

  } finally {

 

   System.out.println("I'm finally.");

 

  }

 

 

 

  System.out.println("Out of try.");

 

 }

 

(单项选择题)

 

AI'm exception

 

I'm finally.

 

 

 

B、代码不能编译通过,因为最后一条语句位于return后,不可到达

 

C、代码编译通过,但运行时输出异常信息

 

DI'm nomal

 

I'm finally.

// args默认是空的 所以输出args[0]会抛出数组越界异常。

 

19、关于以下代码,说法正确的是?  A

class Example {

 public static void main(String[] args) throws IOException {

  System.out.println("Before Try");

  try {

  } catch (java.io.IOException e) {

   System.out.println("Inside Catch");

  }

  System.out.println("At the End");

 }

}(单项选择题)

A、代码编译失败,因为无异常抛出

B、代码编译失败,因为未导入IOException异常类

C、输出Before Try

At the End

D、输出Inside Catch

At the End

// 如果try没有代码,catch中又获取

IOException

异常,那么就会因为没有可能会抛出异常的代码而出错,导致编译失败。

 

 

20、关于以下代码,说法正确的是? C   // 解析看上题

 

class Example {

 

 public static void main(String[] args) throws IOException {

 

  System.out.println("Before Try");

 

  try {

 

  } catch (Throwable e) {

 

   System.out.println("Inside Catch");

 

  }

 

  System.out.println("At the End");

 

 }

 

}(单项选择题)

 

A、代码编译失败,因为无异常抛出

 

B、代码编译失败,因为未导入IOException异常类

 

C、输出Before Try

 

At the End

 

D、输出Inside Catch

 

At the End

 

21、给出以下代码: C

class Example {

 public static void main(String[] args) throws IOException {

  try {

   methodA();   

  } catch (IOException e) {

   System.out.println("caught IOException");

  }catch (Exception e) {

   System.out.println("caught Exception");

  }

 }

}

如果methodA()方法抛出一个IOException异常,则该程序的运行结果是什么?(单项选择题)

A、无内容输出

B、代码编译失败

C、输出caught IOException

D、输出caught Exception

// 这里会抛出IO异常 记住

 

 

22、下列代码的运行结果是? B  

 

class Example {

 

 public static void main(String[] args) throws IOException {

 

  try {

 

   return;

 

  } finally{

 

   

 

   System.out.println("Finally");

 

  }

 

 }

 

}(单项选择题)

 

A、无内容输出

 

B、输出Finally

 

C、代码编译失败

 

D、输出异常信息

// 这里没有异常 所以io异常管不了

 

 

30、现有如下代码: A C  

 

public class Example {

 

 public static void main(String[] args) {

 

  try {

 

   int x=Integer.parseInt("42a");

 

   //插入代码处

 

   System.out.println("oops");

 

  }

 

 }

 

}

 

在插入代码处插入哪些语句可以在运行后输出oops(多项选择题)

 

A} catch (IllegalArgumentException e) {

 

B} catch (IllegalStateException c) {

 

C} catch (NumbelFormatException n) {

 

D} catch (ClassCastException c) {

// IllegalArgumentException 是NumbelFormatException 的父类异常

 

 

34、以下有关java.lang.Exception异常类的正确描述有?(多项选择题) A B

 

A、该类是一个公共类

 

B、该类是Throwable类的子类

 

C、该类实现了Throwable接口

 

D、该类可以序列化

// Throwable是类 不是接口  异常和接口不能序列化

 

 

36、以下哪些是catch语句能够捕获处理的异常?(多项选择题) A B C

 

AThrowable

 

BError

 

CException

 

DString

 

// Error也是可以被catch捕获的

 

 

38、下列代码的执行结果是?    B

 

class Example {

 

 

 

 private void method1() throws Exception {

 

  throw new RuntimeException();

 

 }

 

 

 

 public void method2() {

 

  try {

 

   method1();

 

  } catch (RuntimeException e) {

 

   System.out.println("Caught Runtime Exception");

 

  } catch (Exception e) {

 

   System.out.println("Caught Exception");

 

  }

 

 }

 

 

 

 public static void main(String[] args) throws IOException {

 

  Example a = new Example();

 

  a.method2();

 

 }

 

}(多项选择题)

 

A、代码编译失败

 

B、输出Caught Runtime Exception

 

C、输出Caught Exception

 

D、输出Caught Runtime ExceptionCaught Exception

// 抛出什么异常就是什么异常 跟throws没关系

 

 

44、以下代码的执行结果是?

 

public static int fun() {

 

  int result = 5;

 

  try {

 

   result = result / 0;

 

   return result;

 

  } catch (Exception e) {

 

   System.out.println("Exception");

 

   result = -1;

 

   return result;

 

  } finally {

 

   result = 10;

 

   System.out.println("i am in finally");

 

  }

 

 }

 

 

 

 public static void main(String[] args) {

 

  int x=fun();

 

  System.out.println(x);

 

 

 

 }

 

(简答题)

 

Exception

 

i am in finally

 

-1

// return在执行的时候认准本语句块中的result值,

 

45、以下代码的执行结果是?

public class Example {

 public static StringBuffer fun() {

 

  StringBuffer result = new StringBuffer("Hello");

  Integer i = new Integer(5);

  try {

   if (true)

    throw new RuntimeException();

   return result;

  } catch (Exception e) {

   System.out.println("Exception");

   result.append(" World");

   return result;

  } finally {

   result.append(" Java");

   System.out.println("i am in finally");

  }

 }

 

 public static void main(String[] args) {

  StringBuffer x = fun();

  System.out.println(x);

 

 }(简答题)

Exception

i am in finally

Hello World Java

// 引用数据类型 return时看最终结果

 

posted @ 2020-08-06 21:41  大明湖畔的闰土  阅读(492)  评论(0编辑  收藏  举报