Chapter 8
12: What is the output of the following application? package g; public class BasketBall { public static void main(String[] dribble) { try { System.out.print(1); throw new ClassCastException(); } catch (ArrayIndexOutOfBoundsException ex) { System.out.print(2); } catch (Throwable ex) { System.out.print(3); } finally { System.out.print(4); } System.out.print(5); } } A: 1345 B: 1235 C: The code does not compile. D: The code does compile, but throws an exception at runtime.
A: 1345
33: Given the following code snippet, which specific exception will be thrown? final Object exception = new Exception(); final Exception data = (RuntimeException)exception; System.out.print(data); A: ClassCastException B: RuntimeException C: NullPointerException D: None of the above.
A: ClassCastException
04: Which of the following Throwable types is it recommended not to catch in a Java application. A: Error B: CheckedException C: Exception D: RuntimeException
A: Error
28: What is the output of the following application? package peculiar; public class Stranger { public static String getFullName(String firstName, String lastName) { try { return firstName.toString() + " " + firstName.toString() } finally { System.out.print("Finished!"); } catch (NullPointerException e) { System.out.print("Problem?"); } return count; } public static void main(String[] roster) { System.out.print(getFullName("Joyce","Hopper")); } } A: Joyce Hooper B: Finished! Joyce Hooper C: Problem?Finished!null D: None of the above.
A: Joyce Hooper
36: If a try statement has catch blocks for both ClassCastException and RuntimeException, then which of the following statements is correct? A: The catch block for ClassCastException must appear before the catch block for RuntimeException. B: The catch block for RuntimeException must appear before the catch block for ClassCastException. C: The catch blocks for these two exceptions types can be declared in any order. D: A try statement cannot be declared with these two catch block types because they are incompatible.
A: The catch block for ClassCastException must appear before the catch block for RuntimeException.
32: What is the output of the following application? package zoo; class BigCat { void roar(int level) throw RuntimeException { // m1 if(level<3) throw new IllegalArgumentException("Incomplete"); System.out.print("Roar!"); } } public class Lion extends BigCat { public void roar() { // m2 System.out.print("Roar!!!"); } public static void main(String[] cubs) { final BigCat kitty = new Lion(); // m3 kitty.roar(2); } } A: The code does not compile because of line m1. B: The code does not compile because of line m2. C: The code does not compile because of line m3. D: The code compiles but a stack trace is printed at runtime.
A: The code does not compile because of line m1.
23: If an exception matches two or more catch blocks, which catch block is executed? A: The first one that matches is executed. B: The last one that matches is executed. C: All matched blocks are executed. D: It is not possible to write code like this.
A: The first one that matches is executed.
07: Fill in the blanks: The ______________ keyword is used in method declarations, while the ____________ keyword is used to throw an exception to the surrounding place. A: throws, throw B: catch, throw C: throw, throws D: throws, catch
A: throws, throw
08: If a try statement has catch blocks for both Exception and IOException, then which of the following statements is correct? A: The catch block for Exception must appear before the catch block for IOException. B: The catch block for IOException must appear before the catch block for Exception. C: The catch blocks for these two exception types can be declared in any order. D: A try statement cannot be declared with these two catch block types because they are incompatible.
B
30: What is the output of the following application? package pond; abstract class Duck { protected int count; public abstract int getDuckies(); } public class Ducklings extends Duck { private int age; public Ducklings(int age) {this.age=age;} public int getDuckies() {return this.age/count;} public static void main(String[] pondInfo) { Duck itQuacks = new Ducklings(5); System.out.print(itQuacks.getDuckies()); } } A: 0 B: 5 C: The code does not compile. D: The code does compile, but throws an exception at runtime.
B: 5
16: Which statement about the role of exceptions in Java is incorrect? A: Exceptions are often used when things "go wrong" or deviate from the expected path. B: An application that throws an exception will terminate. C: Some exceptions can be avoided pro-grammatically. D: An application that can properly handle its exception may recover from unexpected problems.
B: An application that throws an exception will terminate.
11: Which of the following exception types must be handled or declared by the method in which the are thrown? A: NullPointerException B: Exception C: RuntimeException D: ArithmeticException
B: Exception
35: In the following application , the values of street and city have been omitted. Which one of the following is a possible output of executing this class? I: 350 5th Ave - New York II: Posted: 350 5th Ave - New York package registrationn; public class Address { public String getAAddress(String street, String city){ try { return street.toString() + "-" + city.toString(); } finally { System.out.print("Posted:"); } } public static void main(String) { String street = // value omitted String city = // value omitted System.out.print(new Address().getAddress(street,city)); } } A: I only B: II only C: I and II D: None of the above.
B: II only
06: Which of the following is a checked exception ? A: ClassCastException B: IOException C: ArrayIndexOutoOfBoundsException D: IllegalArgumentException
B: IOException
22: What is the result of compiling and running the following application? package castles; class CastleUnderSiegeException extends Exception {} class KnightAttackingException extends CastleUnderSiegeException {} public class CItadel { public void OpenDrawBridge() throws RuntimeException { //q1 try { throw new KnightAttackException(); } catch (Exception e) { throw new ClassCastException(); } finally { throw new CastleUnderSiegeException(); //q2 } } public static void main(String) new Citadel().openDrawing(); //q3 } } A: The code does not compile because of line q1. B: The code does not compile because of line q2. C: The code does not compile because of line q3. D: None of the above.
B: The code does not compile because of line q2.
50: What is the output of the following application? package bed; public class Sleep { public static void snore(){ try { String sheep[] = new String[3]; System.out.print(sheep[3]); } catch (RuntimeException e) { System.out.print("Awake!"); } finally { throw new Exception(); //x1 } } public static void main(String... sheep) { //x2 new Sleep().snore(); //x3 } } A: Awake!, followed by a stack trace. B: The code does not compile because of line x1. C: The code does not compile because of line x2. D: The code does not compile because of line x3.
B: The code does not compile because of line x1.
31: Given a try statement, if both the catch block and the finally each throw an exception, what does the caller see? A: The exception from the catch block B: The exception from the finally block C: Both the exception from the catch block and the exception from the finally block D: None of the above.
B: The exception from the finally block
21: Fill in the blanks: A program must handle or declare _________________ but should never handle _________________. A: java.lang.Error, unchecked exceptions B: checked exceptions, java.lang.Error C: java.lang.Throwable, java.lang.Error D: unchecked exceptions, java.lang.Exception
B: checked exceptions, java.lang.Error
18: Which of the following method signatures would not be allowed in a class implementing the Printer interface? class PrintException extends Exception {} class PaperPrintException extends PrintException {} public interface Printer { abstract int printData() throws PrintException; } A: public int printData() throws PrintException; B: public int printData() throws Exception; C: public int printData() D: None of the above.
B: public int printData() throws Exception;
02: Choose the answer that lists the keyword in the order that they would be used together. A: catch, try, finally B: try, catch, finally C: finally, catch, try D: try, finally, catch
B: try, catch, finally
49: Given that FileNotFoundException is a subclass of IOException, what is the output of the following application? package storage; import java.io.*; public class Backup { public void performBackup(){ try { throw new IOException("Disk not found"); } catch (Exception e) { try { throw new FileNotFoundException("File not Found"); } catch (FileNotFoundException e) { //z1 System.out.print("Failed"); } } } public static void main(String... files) { new Backup().performBackup(); //z2 } } A: Failed B: The application compiles but a stack trace is printed at runtime C: The code does not compile because of line z1. D: The code does not compile because of line z2.
C
37: Which of the following is the best scenario to use an exception? A: The computer caught fire. B: The code does not compile. C: A caller passes invalid data to a method. D: A method finishes sooner than expected.
C: A caller passes invalid data to a method.
09: What is the output of the following application? package game; public class FootBall { public static void main(String officials[]) { try { System.out.print('A'); throw new RuntimeException("Out of bounds!"); } catch (ArrayIndexOutOfBoundsException aioobe) { System.out.print('B'); throw aioobe; } finally { System.out.print('C'); } } } A: ABC B: ABC, followed by a stack trace for a RuntimeException C: AC, followed by a stack trace for a RuntimeException D: None of the above.
C: AC, followed by a stack trace for a RuntimeException
26: Fill in the blanks: A _________________ occurs when a program recurses too deeply into an infinite loop, while a(n) _________________ occurs when a reference to a nonexistent object is acted upon. A: NoClassDefFoundError, StackOverflowError B: StackOverflowError, NullPointerException C: ClassCastException, IllegalArgumentException D: StackOverflowError, IllegalArgumentException
C: ClassCastException, IllegalArgumentException
15: Which keywords are required with a try statement ? I: catch II: finalize III: finally A: I only B: II only C: I or III, or both D: None of these statements are required with a try statement.
C: I or III, or both
41: Given the following application, which type of exception will printed in the stack trace at runtime? package carnival; public class WhackAnException { public static void main(String... hammer) { try { throw new ClassCastException(); } catch (RuntimeException e) { throw new NullPointerException(); } finally { throw new RuntimeException(); } } } A: IllegalArgumentException B: NullPointerException C: RuntimeException D: The code does not compile
C: RuntimeException
45: If a try statement has catch blocks for both IllegalArgumentException and ClassCaastException, then which of the following statements is correct? A: The catch block for IllegalArgumentException must appear before the catch block for ClassCastException. B: The catch block for ClassCastException must appear before the catch block for IllegalArgumentException. C: The catch block for these two exception types can be declared in any order. D: A try statement cannot be declared with these two catch block types because they are incompatible.
C: The catch block for these two exception types can be declared in any order.
20: What statements about the following class is correct? class GasException extends Exception {} class Element { public int getSymbol() throws GasException {return -1;} //g1 } public class Oxygen extends Element { public int getSymbol() {return 8; } ; //g2 public void printData() { try { System.out.print(getSymbol()); } catch { //g3 System.out.print("Unable to read data"); } } } A: The code does not compile because of line g1. B: The code does not compile because of line g2. C: The code does not compile because of line g3. D: None of the above.
C: The code does not compile because of line g3.
10: What is the result of compiling and running the following application? package castle; public class Fortress { public void openDrawBridge() throws Exception { //p1 try { throw new Exception("Circle"); } catch (Exception e) { System.out.print("Opening!"); } finally { System.out.print("Walls"); //p2 } } public static void main(String) { new Fortress().openDrawing(); /p3 } } A: The code does not compile because of line p1. B: The code does not compile because of line p2. C: The code does not compile because of line p3. D: The code compiles, but a stack trace is printed at runtime.
C: The code does not compile because of line p3.
14: Given that FileNotFoundException is a subclass of IOException, what is the output of the following application? package office; import java.io.*; public class Printer { public void print(){ try { throw new FileNotFoundException(); } catch (IOException exception) { System.out.print("Z"); } catch (FileNotFoundException enfe) { System.out.print("X"); } finally { System.out.print("Y"); } } public static void main(String... ink) { new Printer().print(); } } A: XY B: ZY C: The code does not compile. D: The code compiles but a stack trace is printed at runtime.
C: The code does not compile.
24: What is the output of the following application? package system; public class Computer { public void compute() throws Exception { throw new RuntimeException("Error processing request"); } public static void main(String{} bits) { try { new Computer().compute(); System.out.print("Ping"); } catch (NullPointerException e) { System.out.print("Pong"); throw e; } } } A: Ping B: Pong C: The code does not compile. D: The code does compile, but throws an exception at runtime.
C: The code does not compile.
38: What is the output of the following application? package body; class Organ { public void operate() throws RuntimeException { throw new RuntimeException ("Not supported"); } } public class Heart extends Organ { public void operate() throws Exception { System.out.print("beat"); } public static void main(String... cholesterol) throws Exception { try { new Heart().operate(); } finally { } } } A: beat B: Not supported C: The code does not compile. D: The code does compile, but throws an exception at runtime.
C: The code does not compile.
13: Which of the following statements about a finally block is true? A: Every line of the finally block is guaranteed to be executed. B: The finally block is executed only if the related catch block is also executed. C: The finally statements requires brackets {}. D: The finally block cannot throw an exception.
C: The finally statements requires brackets {}.
34: Which of the following classes will handle all types in a catch block? A: Exception B: Error C: Throwable D: RuntimeException
C: Throwable
05: What is the output of the following application? package game; public class Baseball { public static void main(String... teams){ try { int score = 1; System.out.print(score++); } catch (Throwable t) { System.out.print(score++); } finally { System.out.print(score++); } System.out.print(score++); } System.out.print(score++); } } A: 123 B: 124 C: 12 D: None of the above.
D: None of the above.
19: Which import statement is required to be declared in order to use the Exception, RuntimeException, and Throwable classes in an application? A: import java.exception.*; B: import java.util.exception.*; C: import java.lang.*; D: None of the above.
D: None of the above.
25: In the following application, the value of list has been omitted. Assuming the code compiles without issue, which one of the following is not a possible output of executing this class? package checkboard; public class Attendance { private Boolean[] list = // value omitted public int printTodayCount() { int count=0; for (int i=0; i<10; i++) { if(list[i]) ++count; } return count; } public static void main(String[] roster) { new Attendance().printTodayCount(); } } A: A stack trace for NullPointerException is printed. B: A stack trace for ArrayIndexOutOfBoundsException is printed. C: A stack trace for ClassCastException is printed. D: None of the above.
D: None of the above.
42: Which of these method signatures is allowed in a class implementing the Outfielder interface? class OutOfBoundsException extends BadCatchException {} class BadCatchException extends Exception {} public interface Outfielder { public void catchBall() throws OutOFBoundsException; } A: public int catchBall() throws OutOFBoundsException B: public int catchBall() throws BadCatchException C: public innt catchBall() throws Exception D: None of the above.
D: None of the above.
48: Given application that hosts a website, which of the following would most likely result in a Java.lang.Error being thrown ? A: Two users try to register an account at the same time. B: The application temporarily loses connection to the network. C: A user enters their password incorrectly. D: The application runs out of memory.
D: The application runs out of memory.
44: What is the output of the following application? package castles; class DragonException extends Exception { public class Lair { public void openDrawBridge() throws Exception { //r1 try { throw new Exception("This Exception"); } catch (RuntimeException e) { throw new DragonException(); //r2 } finally { throw new RuntimeException("Or maybe this one"); } } public static void main(String[] moat) throws Exception { new Lair().openDrawbridge(); // r3 } } A: The code does not compile because of line r1. B: The code does not compile because of line r2. C: The code does not compile because of line r3. D: The code compiles, but a stack trace is printed at runtime.
D: The code compiles, but a stack trace is printed at runtime.
01: What is the result of compiling and executing the following application ? package mind; public class Remember { public static void think() throws Exception { //k1 try { throw new Exception(): } } public static void main(String... ideas) throws Exception { think(); } } A: The code compiles and runs without printing anything. B: The code compiles but a stack trace is printed at runtime. C: The code does not compile because of line k1. D: The code does not compile for another reason.
D: The code does not compile for another reason.
17: What is the output of the following application? package harbor; class CapsizedException extends Exception {} class Transport { public int travel() throws CapsizedException {return 2; } ; } public class Boat { public int travel() throws Exception {return 4; } ; //j1 public static void main(String... distance) throws Exception { try { System.out.print(new Boat().travel()); } catch (Exception e) { System.out.print(8); } } } A: 4 B: 8 C: The code does not compile due to line j1. D: The code does not compile for another reason.
D: The code does not compile for another reason.
47: What is the output of the following application? package lighting; interface Source { void flipSwitch() throws Exception; } public class LightBulb implements Source { public void flipSwitch() { try { throws new RuntimeException("Circuit Break!"); } finally { System.out.print("Flipped!"); } } public static void main(String... electricity) throws Throwable { final Source bulb = new LightBulb(); bulb.flipswitch(); } } A: A stack trace for a RuntimeException B: Flipped!, followed by a stack trace for a RuntimeException C: The code does not compile because flipSwitch() is an invalid method override. D: The code does not compile for another reason.
D: The code does not compile for another reason.
40: What is the output of the following application? package clothing; public class Coat { public long zipper() throws Exception { try { String checkZipper = (String)new Object(); } catch (Exception e) { throw RuntimeException("Broken!"); } return null; } public static void main(String... warmth) { try { new Coat().zipper(); System.out.print("Finished!"); } catch (Throwable t) {} } } A: Finished! B: Finished!, followed by a stack trace C: The application does not produce any output at runtime. D: The code does not compile.
D: The code does not compile.
43: What is the output of the following application? package city; public class Street { public static void dancing() throws RuntimeException { try { throw new IllegalArgumentException { } catch (Error) { System.out.print("Unable!"); } } public static void main(String... count) throws RuntimeException { dancing(); } } A: Unable! B: The application does not produce any output. C: The application compiles but produces a stack trace at runtime. D: The code does not compile.
D: The code does not compile.
46: What is the output of the following application? package broken; class Problem implements RuntimeException {} public class BiggerProblem extends Problem(){ public static void main(String unOh[]) { try { throw new BiggerProblems(); } catch (BiggerProblem re) { System.out.print("Problem?"); } catch (Problem e) { System.out.print("Handled"); } finally { System.out.print("Fixed!"); } } } A: Problem?Fixed! B: Handled.Fixed! C: Problem?Handled.Fixed! D: The code does not compile.
D: The code does not compile.
39: Which statement about the following exception statement is correct ? throw new NullPointerException(); A: The code where this is called must include a try-catch block that handles this exception. B: The method where this is called must declare a compatible exception. C: The exception cannot be handled. D: This exception can be handled with a try-catch block or ignored altogether by the surrounding method.
D: This exception can be handled with a try-catch block or ignored altogether by the surrounding method.
27: Which of the following is not a reason to add checked exceptions to a method signature ? A: To force a caller to handle or declare its exception B: To notify the caller of potential types of problems C: To ensure that exceptions never cause the application to terminate D: To give the caller a chance to recover from a problem.
D: To give the caller a chance to recover from a problem.
29: Fill in the blanks: A try statement has ______________ finally block(s) and ____________ catch blocks. A: zero or one, zero or more B: one, one or more C: zero or one, zero or one D: one or more, zero or one
D: one or more, zero or one
03: Diagram here. A: B: C: D:
Diagram here.