异常处理

1)What is displayed on the console when running the following program?

class Test {
public static void main (String[ ] args) {
try {
System.out.println("Welcome to Java");
}
finally {
System.out.println("The finally clause is executed");
}
}
}

A)Welcome to Java followed by The finally clause is executed in the next line
B)Welcome to Java
C)The finally clause is executed
D)None of the above

无论异常是否发生,finally子句总会被执行

2)The following code causes Java to throw ________.
int number = Integer.MAX_VALUE + 1; 2) _______
A)Exception B)Error C)Throwable D)RuntimeException E)no exceptions

3)What is displayed on the console when running the following program?

class Test {
public static void main (String[ ] args) {
try {
System.out.println("Welcome to Java");
return;
}
finally {
System.out.println("The finally clause is executed");
}
}
}

A)The finally clause is executed
B)Welcome to Java followed by The finally clause is executed in the next line
C)Welcome to Java
D)None of the above

在任何情况下finally块中的代码都会执行,即使finally子句中有一个return语句,finally块还是会被执行。

4)Which of the following is not an advantage of Java exception handling? 4) _______
A)Exception handling simplifies programming because the error-reporting and error-handling code can be placed at the catch block.
B)Java separates exception handling from normal processing tasks.
C)Exception handling makes it possible for the caller's caller to handle the exception.
D)Exception handling improves performance.

A,B,C都是异常处理的优点,但异常处理不能提升程序的性能

处理简化了编程,因为错误报告和错误处理代码可以放在catch块中。
Java将异常处理与普通的处理任务分开。
异常处理使调用方的调用方能够处理异常。

5)An instance of ________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.. 5) _______
A)Error B)NumberFormatException C)Throwable D)RuntimeException E)Exception

Exception描述由你的程序和外部环境所引起的错误,这些错误能被程序捕获和处理。

6)Analyze the following code:

class Test {
public static void main(String[ ] args)
throws MyException {
System.out.println("Welcome to Java");
}
}
class MyException extends Error {
}

A)You should not declare a class that extends Error, because Error raises a fatal error that terminates the program.
B)The program has a compilation error.
C)You cannot declare an exception in the main method.
D)You declared an exception in the main method, but you did not throw it.

Error类不能被继承,Error描述的是内部系统错误,会引发致命错误,从而终止程序。

7)An instance of ________ are unchecked exceptions. (Choose all that apply.) 7) _______
A)NumberFormatException B)Throwable C)Error D)RuntimeException E)Exception

RuntimeException、Error以及它们的子类都称为unchecked exceptions 未经检查的异常

8)A method must declare to throw ________. 8) _______
A)unchecked exceptions B) Error C)RuntimeException D) checked exceptions

checked exceptions检查性异常,编译器会强制程序员检查并通过try-catch块来处理它们,或者在方法头进行声明

9)What is displayed on the console when running the following program?

class Test {
public static void main(String[ ] args) {
try {
System.out.println("Welcome to Java");
int i = 0;
int y = 2/i;
System.out.println("Welcome to Java");
}
finally {
System.out.println("End of the block");
} System.out.println("End of the block");
}
}

A)The program displays Welcome to Java and End of the block, and then terminates because of an unhandled exception.
B)The program displays Welcome to Java two times followed by End of the block two times.
C)The program displays Welcome to Java two times followed by End of the block.
D)The program displays Welcome to Java three times followed by End of the block.

存在ArithmeticException算数异常没有处理unhandled exception

10)What exception type does the following program throw?

public class Test {
public static void main(String[ ] args) {
String s = "abc";
System.out.println(s.charAt(3));
}
}

A)No exception
B)ArithmeticException
C)StringIndexOutOfBoundsException
D)ArrayIndexOutOfBoundsException
E)ClassCastException

注意这里是String类型

11)An instance of ________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully. 11) ______
A)Throwable B)Error C)RuntimeException D)Exception E)NumberFormatException

Error描述的是内部系统错误,会引发致命错误,从而终止程序。

12)What is displayed on the console when running the following program?

class Test {
public static void main(String[ ] args) {
try {
System.out.println("Welcome to Java");
int i = 0;
int y = 2/i;
System.out.println("Welcome to HTML");
}
finally {
System.out.println("The finally clause is executed");
}
}
}

A)Welcome to Java followed by The finally clause is executed in the next line.
B)The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.
C)Welcome to Java. D)None of the above.

13)What exception type does the following program throw?

public class Test {
public static void main(String[ ] args) {
Object o = new Object();
String d = (String)o;
}
}

A)ArithmeticException
B)ArrayIndexOutOfBoundsException
C)ClassCastException
D)No exception
E)StringIndexOutOfBoundsException

父类对象o不是子类对象d的一个实例,不能进行转换,否则会出现ClassCastException

14)What exception type does the following program throw?

public class Test {
public static void main(String[ ] args) {
Object o = null;
System.out.println(o.toString());
}
}

A)ArrayIndexOutOfBoundsException
B)ClassCastException
C)NullPointerException
D)ArithmeticException
E)StringIndexOutOfBoundsException

toStrinfg()返回一个描述该对象的字符串,而这里Object对象没有创建,会造成NullPointerException
15)What is wrong in the following program?

class Test {
public static void main (String[ ] args) {
try {
System.out.println("Welcome to Java");
}
}
}

A)You cannot have a try block without a catch block.
B)You cannot have a try block without a catch block or a finally block.
C)Nothing is wrong.
D)A method call that does not declare exceptions cannot be placed inside a try block.

try之后必须要有catch或者finally

16)An instance of ________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program. 16) ______
A)Throwable B)RuntimeException C)Error D)Exception E)NumberFormatException

Exception描述由你的程序和外部环境所引起的错误,这些错误能被程序捕获和处理。

17)Analyze the following code:

class Test {
public static void main(String[ ] args) {
try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
int i = ;
int y = / i;
}
catch (Exception ex) {
System.out.println("NumberFormatException");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
}
}

A)The program displays NumberFormatException.
B)The program displays RuntimeException.
C)The program has a compilation error.
D)The program displays NumberFormatException followed by RuntimeException.

注意:

这里有两个catch子句,并且Exception还是RuntimeException的父类,这样RutimeException是多余的,会造成编译错误的。

Unreachable catch block for RuntimeException. It is already handled by the catch block for Exception

18)What exception type does the following program throw?

public class Test {
public static void main(String[ ] args) {
System.out.println(1 / 0);
}
}

A)StringIndexOutOfBoundsException
B)ArrayIndexOutOfBoundsException
C)ArithmeticException
D)No exception
E)ClassCastException

19)What is displayed on the console when running the following program?

class Test {
public static void main(String[ ] args) {
try {
method();
System.out.println("After the method call");
}
catch (NumberFormatException ex) {
System.out.println("NumberFormatException");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
}
static void method() {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
System.out.println("Welcome to Java");
}
}

A)The program displays NumberFormatException followed by RuntimeException.
B)The program displays RuntimeException.
C)The program has a compilation error.
D)The program displays NumberFormatException followed by After the method call.
E)The program displays NumberFormatException.

抛出算数异常2/0后没有可以捕获的catch,造成程序中断

20)What is displayed on the console when running the following program?

class Test {
public static void main(String[ ] args) {
try {
System.out.println("Welcome to Java");
int i = 0;
double y = 2.0 / i;
System.out.println("Welcome to HTML");
}
finally {
System.out.println("The finally clause is executed");
}
}
}

A)Welcome to Java.
B)The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.
C)Welcome to Java followed by The finally clause is executed in the next line.
D)None of the above.

注意上面是浮点数

21)What exception type does the following program throw?

public class Test {
public static void main(String[ ] args) {
int[ ] list = new int[5];
System.out.println(list[5]);
}
}

A)ClassCastException
B)ArithmeticException
C)No exception
D)ArrayIndexOutOfBoundsException
E)StringIndexOutOfBoundsException

数组越界

22)Analyze the following code:

class Test {
public static void main(String[ ] args) {
try {
int zero = 0;
int y = 2/zero;
try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
}
catch(Exception e) {
}
}
catch(RuntimeException e) {
System.out.println(e);
}
}
}

A)The program has a compilation error because Exception appears before RuntimeException.
B)A try-catch block cannot be embedded inside another try-catch block.
C)A good programming practice is to avoid nesting try-catch blocks, because nesting makes programs difficult to read. You can rewrite the program using only one try-catch block.
D)None of the above.

一个好的程序是避免嵌套try-catch块,因为嵌套会使程序难于阅读。可以仅使用一个try-catch块重写程序。

23)Analyze the following program.

class Test {
public static void main(String[ ] args) {
try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
System.out.println("Welcome to Java");
}
catch (Exception ex) {
System.out.println(ex);
}
}
}

A)The program has a compilation error.
B)An exception is raised due to 2 / i;
C)The program compiles and runs without exceptions.
D)An exception is raised due to Integer.parseInt(s);

注意到这里的NumberFormatException实现抛出来的,之后会将正常的执行流程给中断掉,也就是说会跳过try块剩余的语句。

24)What is displayed on the console when running the following program?

class Test {
public static void main(String[ ] args) {
try {
method();
System.out.println("After the method call");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
catch (Exception ex) {
System.out.println("Exception");
}
}
static void method() throws Exception {
try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
System.out.println("Welcome to Java");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
catch (Exception ex) {
System.out.println("Exception");
}
}
}

A)The program displays Exception followed by RuntimeException.
B)The program displays RuntimeException twice.
C)The program has a compilation error.
D)The program displays RuntimeException followed by After the method call.
E)The program displays Exception twice.

注意这道题的说法,是在调用方法结束之后显示异常,不要把这个After the method call当做打印信息的“After the method call”

25)What is displayed on the console when running the following program?

class Test {
public static void main(String[ ] args) {
try {
System.out.println("Welcome to Java");
int i = 0;
int y = 2/i;
System.out.println("Welcome to Java");
}
catch (RuntimeException ex) {
System.out.println("Welcome to Java");
}
finally {
System.out.println("End of the block");
}
}
}

A)The program displays Welcome to Java two times.
B)The program displays Welcome to Java two times followed by End of the block.
C)The program displays Welcome to Java three times.
D)The program displays Welcome to Java three times followed by End of the block.

ArithmeticException是RuntimeException的子类,子类的实例总是父类的实例

26)What is displayed on the console when running the following program?

class Test {
public static void main(String[ ] args) {
try {
System.out.println("Welcome to Java");
int i = 0;
int y = 2/i;
System.out.println("Welcome to Java");
}
catch (RuntimeException ex) {
System.out.println("Welcome to Java");
}
finally {
System.out.println("End of the block");
}
System.out.println("End of the block");
}
}

A)The program displays Welcome to Java two times followed by End of the block.
B)The program displays Welcome to Java two times followed by End of the block two times.
C)The program displays Welcome to Java three times followed by End of the block.
D)You cannot catch RuntimeException errors.

27)What is displayed on the console when running the following program?

class Test {
public static void main(String[ ] args) {
try {
method();
System.out.println("After the method call");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
catch (Exception ex) {
System.out.println("Exception");
}
}
static void method() throws Exception {
try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
System.out.println("Welcome to Java");
}
catch (NumberFormatException ex) {
System.out.println("NumberFormatException");
throw ex;
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
}
}

A)The program has a compilation error.
B)The program displays NumberFormatException followed by After the method call.
C)The program displays NumberFormatException followed by RuntimeException.
D)The program displays NumberFormatException twice.

NumberFormatException
RuntimeException

注意这里先捕获

28)A Java exception is an instance of ________. 28) ______
A)RuntimeException B)Error C)NumberFormatException D)Exception E)Throwable

异常exception的根类是java.lang.Throwable

29)What exception type does the following program throw?

public class Test {
public static void main(String[ ] args) {
Object o = null;
System.out.println(o);
}
}

A)No exception
B)ArrayIndexOutOfBoundsException
C)StringIndexOutOfBoundsException
D)NullPointerException
E)ArithmeticException

30)Which of the following statements are true? (Choose all that apply.) 30) ______
A)You use the keyword throws to declare exceptions in the method heading.
B)If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method.
C)A method may declare to throw multiple exceptions.
D)To throw an exception, use the key word throw.

文本IO

31)Which class do you use to write data into a text file? 2) _______
A)File B) System C)Scanner D) PrintWriter

32)Which class do you use to read data into a text file? 10) ______
A)Scanner B) System C)PrintWriter D) File

使用Scanner类从文件中读取文本数据,使用 PrintWriter 类向文本文件写入数据

33)Which of the following statements are true? 12) ______
A)If a file (e.g., c:\temp.txt) does not exist, new File("c:\\temp.txt") creates a new file named c:\temp.txt.
B)If a directory (e.g., c:\liang) does not exist, new File("c:\liang") creates a new directory named c:\liang.
C)If a file (e.g., c:\temp.txt) does not exist, new File("c:\\temp.txt") returns null.
D)If a directory (e.g., c:\liang) does not exist, new File("c:\liang") returns null.
E)None of the above.

34)Which class contains the method for checking whether a file exists? 14) ______
A)System B) Scanner C)File D) PrintWriter

15)Which of the following statements creates an instance of File on Window for the file c:\t.txt?
A)new File("c:\txt.txt")  B) new File("c:\\txt.txt")
C)new File("c://txt.txt") D) new File("c:/txt.txt")

在java中,反斜杠是一个特殊的字符,应该写成\\形式

35)Which of the following returns the path separator character? 18) ______
A)File.separatorChar
B)File.pathSeparatorChar
C)File.pathSeparator
D)File.separator
E)None of the above.

36)Which method can be used to write data? 24) ______
A)exist B) print C) close D) rename

37)Which method can be used to create an output object for file temp.txt? (Choose all that apply.)
A)new PrintWriter("temp.txt")
B)new PrintWriter(new File("temp.txt"))
C)new PrintWriter(File("temp.txt"))
D)new PrintWriter(temp.txt)

38)Suppose you enter 34.3, the ENTER key, 57.8, the ENTER key, 789, the ENTER key. Analyze the following code.

Scanner scanner = new Scanner(System.in);
int value = scanner.nextDouble();
int doubleValue = scanner.nextInt();
String line = scanner.nextLine();

A)After the last statement is executed, intValue is 34.
B)After the last statement is executed, line contains characters '7 ', '8 ', '9'.
C)After the last statement is executed, line contains character  '\n '.
D)The program has a runtime error because 34.3 is not an integer.
E)After the last statement is executed, line contains characters '7', '8', '9', '\n'.

39)Which method can be used to create an input object for file temp.txt? 31) ______
A)new Scanner("temp.txt")
B)new Scanner(temp.txt)
C)new Scanner(File("temp.txt"))
D)new Scanner(new File("temp.txt"))

40)What are the reasons to create an instance of the File class? (Choose all that apply.) 37) ______
A)To delete the file.
B)To read/write data from/to a file
C)To rename the file.
D)To obtain the properties of the file such as whether the file can be read, written, or is hidden.
E)To determine whether the file exists.

41)Which method can be used to read a whole line from the file? 43) ______
A)nextDouble B) nextLine C)next D) nextInt

Java题库——Chapter12 异常处理和文本IO的更多相关文章

  1. Java题库——Chapter17 二进制I/0

    Introduction to Java Programming 的最后一章,完结撒花!Chapter 17 Binary I/O Section 17.2 How is I/O Handled in ...

  2. JAVA题库01

    说出一些常用的类,包,接口,请各举5个 常用的类:BufferedReader  BufferedWriter  FileReader  FileWirter  String Integer java ...

  3. Java题库——Chapter13抽象类和接口

    )What is the output of running class Test? public class Test { public static void main(String[ ] arg ...

  4. Java题库——Chapter8 对象和类

    1)________ represents an entity(实体) in the real world that can be distinctly identified. 1) _______ ...

  5. Java题库——Chapter16 JavaFX UI组件和多媒体

    Chapter 16 JavaFX UI Controls and Multimedia Section 16.2 Labeled and Label1. To create a label with ...

  6. Java题库——Chapter14 JavaFX基础

    Chapter 14 JavaFX Basics Section 14.2 JavaFX vs Swing and AWT1. Why is JavaFX preferred?a. JavaFX is ...

  7. Java题库——Chapter11 继承和多态

    1)Analyze the following code: public class Test { public static void main(String[ ] args) { B b = ne ...

  8. Java题库——Chapter10 面向对象思考

    1)You can declare two variables with the same name in ________. 1) _______ A)a method one as a forma ...

  9. Java题库——Chapter9 String的用法

    1)Which code fragment would correctly identify the number of arguments passed via the command line t ...

随机推荐

  1. 关于JAVA的Random类的冷知识(转自菜鸟V)

    JAVA的Random类(转) Random类 (java.util) Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基 ...

  2. 2016/09/27 Hadoop Yarn

    1.1 YARN基本架构     YARN是Hadoop2.0中的资源管理系统,它的基本设计思想是将MRv1中的JobTracker拆分成了两个独立的服务:一个全局的资源管理器ResourceMana ...

  3. Linux Bash之通配符

    通配符是我们在shell环境中不知不觉中都会用到的,有时甚至都不会考虑到去探究其实现过程,因为使用得太普遍了.而清晰地理解每一个过程,将有助于我们的分析和调试. 说白了,通配符就是在 shell 环境 ...

  4. Kotlin 编程语言成为其 Android 应用程序开发人员的首选语言

    今年 5 月,谷歌在 I/O 大会上宣布,Kotlin 编程语言成为其 Android 应用程序开发人员的首选语言. Kotlin 是一种面向现代多平台应用程序的编程语言,成为谷歌开发 Android ...

  5. Androi O Automotive 介绍

    最近由于工作需要对android o 中的 automotive源码进行了深入的学习,现总结如下: Android O Vehicle之架构介绍 Android O Vehicle之Car Servi ...

  6. sqlalchemy 执行原生sql语句

    from contextlib import contextmanager from sqlalchemy import create_engine, ForeignKey from sqlalche ...

  7. 一起学SpringMVC之文件上传

    概述 在Web系统开发过程中,文件上传是普遍的功能,本文主要以一个简单的小例子,讲解SpringMVC中文件上传的使用方法,仅供学习分享使用,如有不足之处,还请指正. 文件上传依赖包 如下所示,文件上 ...

  8. vuejs中拖动改变元素宽度实现宽度自适应大小

    需求效果: 原理:拖动效果的实现基本都是dom操作来实现的,通过拖动分隔线,计算分隔线与浏览器边框的距离(left),来实现拖动之后的不同宽度的计算:当拖动分隔线1时,计算元素框left和mid:当拖 ...

  9. css position:sticky的尝试

    前言 sticky这种设计效果是经常出现的,比如陶宝右侧的工具栏,当我们向下滚动到它的位置时,它就会黏住顶部跟随滚动,类似position: fixed的效果,只不过它的触发条件是当我们滚动到所在位置 ...

  10. 邬江兴院士:工业互联网安全&拟态防御

    尊敬的郑院士.曹书记.张秘书长,各位学术界的同仁们,很高兴在第一届工业互联网学术专题论坛上发言.我今天想谈的问题是工业互联网,这个概念很热,前景也很美好,很诱人.但是我认为工业互联网的安全挑战更严峻, ...