Chapter 12 Exception Handling and Text I/O
Section 12.2 Exception-Handling Overview
5. The following code causes Java to throw .
int number = Integer.MAX_VALUE + 1;
a. RuntimeException
b. Exception
c. Error
d. Throwable
e. no exceptions
#
7. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
System.out.println(1 / 0);
}
}
a. ArithmeticException
b. ArrayIndexOutOfBoundsException
c. StringIndexOutOfBoundsException
d. ClassCastException
e. No exception
#
Section 12.3 Exception Types
1. A Java exception is an instance of .
a. RuntimeException
b. Exception
c. Error
d. Throwable
e. NumberFormatException
2. 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.
a. RuntimeException
b. Exception
c. Error
d. Throwable
e. NumberFormatException
#
3. An instance of describes the errors caused by your program and external circumstances. These errors can
be caught and handled by your program.
a. RuntimeException
b. Exception
c. Error
d. Throwable
e. NumberFormatException
6. Instances of are unchecked exceptions.
a. RuntimeException
b. Exception
c. Error
d. Throwable
e. NumberFormatException
#
8. 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. ArithmeticException
b. ArrayIndexOutOfBoundsException
c. StringIndexOutOfBoundsException
d. ClassCastException
e. No exception
#
9. 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. ArithmeticException
b. ArrayIndexOutOfBoundsException
c. StringIndexOutOfBoundsException
d. ClassCastException
e. No exception
#
Section 12.4 Declaring, Throwing, and Catching Exceptions
Section 12.4.1 Declaring Exceptions
13. A method must declare to throw .
a. unchecked exceptions
b. checked exceptions
c. Error
d. RuntimeException
Key:b See th end of Section 12.3.
#
11. 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. ArithmeticException
b. ArrayIndexOutOfBoundsException
c. StringIndexOutOfBoundsException
d. ClassCastException
e. NullPointerException
#
12. 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. ArithmeticException
b. ArrayIndexOutOfBoundsException
c. StringIndexOutOfBoundsException
d. No exception
e. NullPointerException
#
Section 12.4.2 Throwing Exceptions
14. Which of the following statements are true?
a. You use the keyword throws to declare exceptions in the method heading.
b. A method may declare to throw multiple exceptions.
c. To throw an exception, use the key word throw.
#
15. Analyze the following code:
public 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. You cannot declare an exception in the main method.
c. You declared an exception in the main method, but you did not throw it.
d. The program has a compile error.
#
10. 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. StringIndexOutOfBoundsException
d. ClassCastException
e. No exception
#
Section 12.4.3 Catching Exceptions
16. Analyze the following code:
public 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”);
}
}
}
a. The program displays NumberFormatException.
b. The program displays RuntimeException.
c. The program displays NumberFormatException followed by RuntimeException.
d. The program has a compile error.
#
4. An instance of describes programming errors, such as bad casting, accessing an outofbounds array,
and numeric errors.
a. RuntimeException
b. Exception
c. Error
d. Throwable
e. NumberFormatException
#
Section 12.4.4 Getting Information from Exceptions
17. Analyze the following program.
public 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. An exception is raised due to Integer.parseInt(s);
b. An exception is raised due to 2 / i;
c. The program has a compile error.
d. The program compiles and runs without exceptions.
#
18. What is displayed on the console when running the following program?
public class Test {
public static void main(String[] args) {
try {
p();
System.out.println(“After the method call”);
}
catch (NumberFormatException ex) {
System.out.println(“NumberFormatException”);
}
catch (RuntimeException ex) {
System.out.println(“RuntimeException”);
}
}
static void p() {
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.
b. The program displays NumberFormatException followed by After the method call.
c. The program displays NumberFormatException followed by RuntimeException.
d. The program has a compile error.
e. The program displays RuntimeException.
#
Section 12.4.5 Example: Declaring, Throwing, and Catching Exceptions
19. What is displayed on the console when running the following program?
public class Test {
public static void main(String[] args) {
try {
p();
System.out.println(“After the method call”);
}
catch (RuntimeException ex) {
System.out.println(“RuntimeException”);
}
catch (Exception ex) {
System.out.println(“Exception”);
}
}
static void p() 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 RuntimeException twice.
b. The program displays Exception twice.
c. The program displays RuntimeException followed by After the method call.
d. The program displays Exception followed by RuntimeException.
e. The program has a compile error.
© 2020 Pearson Education, Inc., Hoboken, NJ. All rights reserved. This material is protected under all copyright laws as they
System.out.println(“After the method call”) is called.
#
Section 12.5 The finally Clause
20. What is wrong in the following program?
public 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. A method call that does not declare exceptions cannot be placed inside a try block.
d. Nothing is wrong.
#
21. What is displayed on the console when running the following program?
public 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
b. Welcome to Java followed by The finally clause is executed in the next line
c. The finally clause is executed
d. None of the above
#
22. What is displayed on the console when running the following program?
public 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. Welcome to Java
b. Welcome to Java followed by The finally clause is executed in the next line
c. The finally clause is executed
#
23. What is displayed on the console when running the following program?
public 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, then an error message.
b. Welcome to Java followed by The finally clause is executed in the next line, then an error message.
c. The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed, then an
error message.
d. None of the above.
#
24. What is displayed on the console when running the following program?
public 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. Welcome to Java followed by The finally clause is executed in the next line.
c. The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.
d. None of the above.
#
25. What is displayed on the console when running the following program?
public 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 three times followed by End of the block.
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 two times.
#
26. What is displayed on the console when running the following program?
public 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 three times followed by End of the block.
b. The program displays Welcome to Java two times followed by End of the block.
c. The program displays Welcome to Java two times followed by End of the block two times.
d. You cannot catch RuntimeException errors.
#
27. What is displayed on the console when running the following program?
public 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 three times followed by End of the block.
b. The program displays Welcome to Java two times followed by End of the block.
c. The program displays Welcome to Java two times followed by End of the block two times.
d. The program displays Welcome to Java and End of the block, and then terminates because of an unhandled
exception.
#
Section 12.6 When to Use Exceptions
28. Which of the following is not an advantage of Java exception handling?
a. Java separates exception handling from normal processing tasks.
b. Exception handling improves performance.
c. Exception handling makes it possible for the caller’s caller to handle the exception.
d. Exception handling simplifies programming because the errorreporting and errorhandling code can be placed at
the catch block.
#
29. Analyze the following code:
public 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. A try-catch block cannot be embedded inside another try-catch block.
b. 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.
c. The program has a compile error because Exception appears before RuntimeException.
d. None of the above.
© 2020 Pearson Education, Inc., Hoboken, NJ. All rights reserved. This material is protected under all copyright laws as they
program, a RuntimeException would occur and it would be caught be the last catch clause.
#
Section 12.7 Rethrowing Exceptions
30. What is displayed on the console when running the following program?
public 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 displays NumberFormatException twice.
b. The program displays NumberFormatException followed by After the method call.
c. The program displays NumberFormatException followed by RuntimeException.
d. The program has a compile error.
#
Section 12.10 The File Class
31. What are the reasons to create an instance of the File class?
a. To determine whether the file exists.
b. To obtain the properties of the file such as whether the file can be read, written, or is hidden.
c. To rename the file.
d. To delete the file.
e. To read/write data from/to a file
#
32. Which of the following is used to test if a file c:\temp\test.txt exists?
a. new File(“c:\temp\test.txt”).exists()
b. new File(“c:\\temp\\test.txt”).exists()
c. new File(“c:\temp\test.txt”).exist()
d. new File(“c:\\temp\\test.txt”).exist()
#
33. Which of the following statements creates an instance of File on Window for the file c:\temp.txt?
a. new File(“c:\temp.txt”)
b. new File(“c:\\temp.txt“)
c. new File(“c:/temp.txt”)
d. new File(“c://temp.txt”)
#
34. Which of the following statements are true?
a. If a file (e.g., c:\temp.txt) does not exist, new File(“c:\\temp.txt”) returns null.
b. If a directory (e.g., c:\liang) does not exist, new File(“c:\liang”) returns 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.
d. If a directory (e.g., c:\liang) does not exist, new File(“c:\liang”) creates a new directory named c:\liang.
e. None of the above.
#
Section 12.11 Text I/O
Section 12.11.1 Writing Data Using PrintWriter
36. Which class do you use to write data into a text file?
a. File
b. PrintWriter
c. Scanner
d. System
#
38. Which method can be used to write data?
a. close
b. print
c. exist
d. rename
#
47. Which method can be used to create an output object for file temp.txt?
a. new PrintWriter(“temp.txt”)
b. new PrintWriter(temp.txt)
c. new PrintWriter(new File(“temp.txt”))
d. new PrintWriter(File(“temp.txt”))
35. Which class contains the method for checking whether a file exists?
a. File
b. PrintWriter
c. Scanner
d. System
#
Section 12.11.2 Closing Resources Automatically Using trywithresource
40. 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”);
Scanner input = new Scanner(System.in);) {
output.println(input.nextLine());
}
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();
}
a. I
b. II
c. III
d. IV
#
Section 12.11.3 Reading Data Using Scanner
37. Which class do you use to read data from a text file?
a. File
b. PrintWriter
c. Scanner
d. System
#
39. Which method can be used to read a whole line from the file?
b. nextLine
c. nextInt
d. nextDouble
#
41. 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();
}
a. I
b. II
c. III
d. IV
#
42. Which method can be used to create an input object for file temp.txt?
a. new Scanner(“temp.txt”)
b. new Scanner(temp.txt)
c. new Scanner(new File(“temp.txt”))
d. new Scanner(File(“temp.txt“))
#
Section 12.11.4 How Does Scanner Work?
43. Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.
Scanner input = new Scanner(System.in);
int v1 = input.nextInt();
int v2 = input.nextInt();
String line = input.nextLine();
a. After the last statement is executed, v1 is 34.
b. The program has a runtime error because 34.3 is not an integer.
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’.
#
44. 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();
a. After the last statement is executed, line contains characters ‘7’, ‘8’, ‘9’.
b. After the last statement is executed, line contains characters ‘7’, ‘8’, ‘9’, ‘\n’.
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’.
#
45. Suppose you enter 34.3, the ENTER key, 57.8, the ENTER key. Analyze the following code.
Line 1 Scanner input = new Scanner(System.in);
Line 2 double v1 = input.nextDouble();
Line 3 double v2 = input.nextDouble();
Line 4 String line = input.nextLine();
a. After line 2 is executed, v1 is 34.3.
b. After line 3 is executed, v2 is 57.8.
c. After line 4 is executed, line contains an empty string.
d. After line 4 is executed, line is null.
e. After line 4 is executed, line contains character “\n”.
#
46. Suppose you enter 34.3, the ENTER key, 57.8, the ENTER key, abc, the Enter key. Analyze the following code.
Line 1 Scanner input = new Scanner(System.in);
Line 2 double v1 = input.nextDouble();
Line 3 double v2 = input.nextDouble();
Line 4 String line = input.nextLine();
a. After line 2 is executed, v1 is 34.3.
b. After line 3 is executed, v2 is 57.8.
c. After line 4 is executed, line contains an empty string.
d. After line 4 is executed, line is null.
e. After line 4 is executed, line contains character “abc”.
#
Section 12.12 Reading Data from the Web
48. To create an InputStream to read from a file on a Web server, you use the method in the URL
class.
a. getInputStream();
b. obtainInputStream();
c. openStream();
d. connectStream();