面向对象程序设计(Java) chapter12

1.A Java exception is an instance of __________.
单选题 (2 分)
E
A.Exception

B.RuntimeException

C.Error

D.NumberFormatException

E.Throwable
3.
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 = 0;

  int y = 2 / i;

}

catch (Exception ex) {

  System.out.println("NumberFormatException");

}

catch (RuntimeException ex) {

  System.out.println("RuntimeException");

}

}

}
单选题 (2 分)
D
A.The program displays NumberFormatException followed by RuntimeException.

B.The program displays NumberFormatException.

C.The program displays RuntimeException.

D.The program has a compilation error.
4.Which class do you use to read data from a text file?
单选题 (2 分)
C
A.PrintWriter

B.System

C.Scanner

D.File
6.
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");

}

}
单选题 (2 分)
D
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.The program displays Welcome to Java and End of the block, and then terminates(终止) because of an unhandled(未处理的) exception(异常).
7.
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");

}

}

}
单选题 (2 分)
E
A.The program has a compilation error.

B.The program displays Exception followed by RuntimeException.

C.The program displays RuntimeException twice.

D.The program displays Exception twice.

E.The program displays RuntimeException followed by After the method call.
8.Which of the following statements creates an instance of File on Window for the file c:\temp.txt?
单选题 (2 分)
A
A.new File(“c:\temp.txt”)

B.new File(“c://temp.txt”)

C.new File(“c:\temp.txt”)

D.new File(“c:/temp.txt”)
9.
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;

}

}
单选题 (2 分)
D
A.ArithmeticException
(算术异常)
B.ArrayIndexOutOfBoundsException
(数组越界)
C.StringIndexOutOfBoundsException
(字符串越界)
D.ClassCastException(类型转换异常)

E.No exception
13.
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);

}

}

}
单选题 (2 分)
A
A.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.

B.The program has a compilation error because Exception appears before RuntimeException.

C.None of the above.

D.A try-catch block cannot be embedded inside another try-catch block.
14.Which method can be used to create an input object for file temp.txt?
单选题 (2 分)
B
A.new Scanner(temp.txt)

B.new Scanner(new File(“temp.txt”))

C.new Scanner(“temp.txt”)

D.new Scanner(File(“temp.txt”))
15.
Which of the following statements are correct?

I:

File file = new File(“input.txt”);

try (Scanner input = new Scanner(file)) {

String line = input.nextLine();

}

II:

try (File file = new File(“input.txt”);

 Scanner input = new Scanner(file);) {

String line = input.nextLine();

}

III:

File file;

try (file = new File(“input.txt”);

 Scanner input = new Scanner(file);) {

String line = input.nextLine();

}

IV:

File file;

Scanner input;

try (file = new File(“input.txt”);

 input = new Scanner(file);) {

String line = input.nextLine();

}
单选题 (2 分)
A
A.I

B.III

C.II

D.IV
18._________ is the root class of exception classes.
单选题 (2 分)
D
A.IOException

B.Exception

C.RuntimeException

D.Throwable
19.Which of the following returns the path separator character?
单选题 (2 分)
A
A.File.pathSeparatorChar

B.File.separator

C.None of the above.

D.File.separatorChar

E.File.pathSeparator
20.
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);

}

}

}
单选题 (2 分)
B
A.The program has a compilation error.

B.An exception is raised due to Integer.parseInt(s);

C.An exception is raised due to 2 / i;

D.The program compiles and runs without exceptions.
23.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.
单选题 (2 分)
B
A.Exception

B.Error

C.Throwable

D.RuntimeException

E.NumberFormatException
25.If no exception occurs in a try-catch block, the code in the finally clause _________.
单选题 (2 分)
D
A.is not executed

B.may be executed

C.is ignored.

D.is executed(执行)
26.An instance of _________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.
单选题 (2 分)
A
A.Exception

B.Throwable

C.RuntimeException

D.NumberFormatException

E.Error
27.Which class do you use to write data into a text file?
单选题 (2 分)
A
A.PrintWriter

B.Scanner

C.System

D.File
29.
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");

}

}

}
单选题 (2 分)
D
A.None of the above.

B.Welcome to Java followed by The finally clause is executed in the next line.

C.Welcome to Java.

D.The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.
30.Which class contains the method for checking whether a file exists?
单选题 (2 分)
A
A.File

B.Scanner

C.System

D.PrintWriter
31.An exception is always an instance of ___________.
单选题 (2 分)
C
A.Exception

B.IOException

C.Throwable

D.RuntimeException
33.
Analyze the following code:

class Test {

public static void main(String[] args)

throws MyException {

System.out.println("Welcome to Java");

}

}

class MyException extends Error {

}
单选题 (2 分)
B
A.You cannot declare an exception in the main method.

B.You should not declare a class that extends Error, because Error raises a fatal error that terminates the program.
(你不应该声明一个扩展 Error 的类,因为 Error 会引发一个终止程序的致命错误)
C.You declared an exception in the main method, but you did not throw it.

D.The program has a compilation error.
34.Which of the following statements are true?
单选题 (2 分)
D
A.If a directory (e.g., c:\liang) does not exist, new File(“c:\liang”) creates a new directory named c:\liang.
(如果目录(例如,c:\liang)不存在,new File(“c:\liang”) 会创建一个名为 c:\liang 的新目录。)
名字不对,应该是创建名为liang的新目录
B.If a directory (e.g., c:\liang) does not exist, new File(“c:\liang”) returns null.
(如果目录(例如 c:\liang)不存在,则 new File(“c:\liang”) 返回 null)
C.If a file (e.g., c:\temp.txt) does not exist, new File(“c:\temp.txt”) creates a new file named c:\temp.txt.
(如果文件(例如,c:\temp.txt)不存在,new File(“c:\temp.txt”) 创建一个名为 c:\temp.txt 的新文件)
名字不对,应该是创建名为temp.txt的新文件
D.None of the above.

E.If a file (e.g., c:\temp.txt) does not exist, new File(“c:\temp.txt”) returns null.
(如果文件(例如 c:\temp.txt)不存在,则 new File(“c:\temp.txt”) 返回 null)
35.
Analyze the following code:

public static boolean isCorrect() {

try {

// Perform some tasks

// ...

return true;

}

finally {

return true;

}

}
单选题 (2 分)
A
A.When invoking the isCorrect method, it always returns false.

B.When invoking the isCorrect method, it always returns true.

C.When invoking the isCorrect method, it returns false if any exceptions occur in the method.

D.When invoking the isCorrect method, it returns true if no exceptions occur in the method.
36.
Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.

Scanner input = new Scanner(System.in);

double v1 = input.nextDouble();

double v2 = input.nextDouble();

String line = input.nextLine();
单选题 (2 分)
D
A.After the last statement is executed, line contains characters ‘7’, ‘8’, ‘9’, ‘\n’.

B.After the last statement is executed, line contains characters ‘7’, ‘8’, ‘9’.

C.After the last statement is executed, line contains characters ’ ', ‘7’, ‘8’, ‘9’, ‘\n’.

D.After the last statement is executed, line contains characters ’ ', ‘7’, ‘8’, ‘9’.
39.
What is wrong in the following program?

class Test {

public static void main (String[] args) {

try {

  System.out.println("Welcome to Java");

 }

}

}
单选题 (2 分)
C
A.A method call that does not declare exceptions cannot be placed inside a try block.

B.Nothing is wrong.

C.You cannot have a try block without a catch block or a finally block.

D.You cannot have a try block without a catch block.
40.To create an InputStream to read from a file on a Web server, you use the method __________ in the URL class. 回答错误
单选题 (2 分)
B
A.connectStream();

B.openStream();

C.getInputStream();

D.obtainInputStream();
45.Which of the following is not an advantage of Java exception handling?
单选题 (2 分)
A
A.Exception handling improves performance(性能).

B.Exception handling makes it possible for the caller’s caller to handle the exception.

C.Java separates exception handling from normal processing tasks.

D.Exception handling simplifies programming because the error-reporting and error-handling code can be placed at the catch block.
46.
Which of the following statements are correct?

I:

try (PrintWriter output = new PrintWriter(“output.txt”)) {

output.println(“Welcome to Java”);

}

II:

try (PrintWriter output = new PrintWriter(“output.txt”)😉 {

output.println(“Welcome to Java”);

}

III:

PrintWriter output;

try (output = new PrintWriter(“output.txt”)😉 {

output.println(“Welcome to Java”);

}

IV:

try (PrintWriter output = new PrintWriter(“output.txt”)😉 {

output.println(“Welcome to Java”);

}

finally {

output.close();

}
多选题 (1 分)
BCD
A.IV

B.II

C.I

D.III
48.An instance of _________ are unchecked exceptions.
多选题 (1 分)
ACE
A.Error

B.Exception

C.RuntimeException

D.Throwable

E.NumberFormatException
50.Which of the classes are in the java.lang package?
多选题 (1 分)
ABC
A.RuntimeException

B.Throwable

C.Exception

D.IOException
51.A method cannot throw Exception or a subclass of Exception if it is not claimed in the method declaration.
判断题 (1 分)
B
A.false

B.true
52.You cannot place a method call in a try-catch block unless the method claims an exception.
判断题 (1 分)
A
A.false

B.true
54.When an Error-type exception occurs, the GUI application may continue to run.
判断题 (1 分)
A
A.false

B.true
55.Any number divided by 0 would cause a compilation error.
判断题 (1 分)
B
A.true

B.false

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值