six Flashcards
Which of the following statements are true? (Choose all that apply)
A. Runtime exceptions are the same thing as checked exceptions.
B. Runtime exceptions are the same thing as unchecked exceptions.
C. You can declare only checked exceptions.
D. You can declare only unchecked exceptions.
E. You can handle only Exception subclasses.
B. Runtime exceptions are also known as unchecked exceptions. They are allowed
to be declared, but they don’t have to be. Checked exceptions must be handled or
declared. Legally, you can handle java.lang.Error subclasses, but it’s not a good idea.
Which of the following pairs fill in the blanks to make this code compile? (Choose all that apply) 7: public void ohNo() \_\_\_\_\_ Exception { 8: \_\_\_\_\_\_\_\_\_\_\_\_\_ Exception(); 9: } A. On line 7, fill in throw B. On line 7, fill in throws C. On line 8, fill in throw D. On line 8, fill in throw new E. On line 8, fill in throws F. On line 8, fill in throws new
B, D. In a method declaration, the keyword throws is used. To actually throw an
exception, the keyword throw is used and a new exception is created.
When are you required to use a finally block in a regular try statement (not a try-with-resources)?
A. Never.
B. When the program code doesn’t terminate on its own.
C. When there are no catch blocks in a try statement.
D. When there is exactly one catch block in a try statement.
E. When there are two or more catch blocks in a try statement.
C. A try statement is required to have a catch clause and/or finally clause. If it goes
the catch route, it is allowed to have multiple catch clauses.
Which exception will the following throw? Object obj = new Integer(3); String str = (String) obj; System.out.println(str); A. ArrayIndexOutOfBoundsException B. ClassCastException C. IllegalArgumentException D. NumberFormatException E. None of the above.
B. The second line tries to cast an Integer to a String. Since String does not extend
Integer, this is not allowed and a ClassCastException is thrown.
Which of the following exceptions are thrown by the JVM? (Choose all that apply) A. ArrayIndexOutOfBoundsException B. ExceptionInInitializerError C. java.io.IOException D. NullPointerException E. NumberFormatException
A, B, D. java.io.IOException is thrown by many methods in the java.io package,
but it is always thrown programmatically. The same is true for NumberFormatExcep-tion; it is thrown programmatically by the wrapper classes of java.lang. The other
three exceptions are all thrown by the JVM when the corresponding problem arises.
What will happen if you add the statement System.out.println(5 / 0); to a working
main() method?
A. It will not compile.
B. It will not run.
C. It will run and throw an ArithmeticException.
D. It will run and throw an IllegalArgumentException.
E. None of the above.
C. The compiler tests the operation for a valid type but not a valid result, so the code
will still compile and run. At runtime, evaluation of the parameter takes place before
passing it to the print() method, so an ArithmeticException object is raised.
What is printed besides the stack trace caused by the NullPointerException from line 16? 1: public class DoSomething { 2: public void go() { 3: System.out.print("A"); 4: try { 5: stop(); 6: } catch (ArithmeticException e) { 7: System.out.print("B"); 8: } finally { 9: System.out.print("C"); 10: } 11: System.out.print("D"); 12: } 13: public void stop() { 14: System.out.print("E"); 15: Object x = null; 16: x.toString(); 17: System.out.print("F"); 18: } 19: public static void main(String[] args) { 20: new DoSomething().go(); 21: } 22: } A. AE B. AEBCD C. AEC D. AECD E. No output appears other than the stack trace.
C. The main() method invokes go and A is printed on line 3. The stop method is
invoked and E is printed on line 14. Line 16 throws a NullPointerException, so stop
immediately ends and line 17 doesn’t execute. The exception isn’t caught in go, so the
go method ends as well, but not before its finally block executes and C is printed on
line 9. Because main() doesn’t catch the exception, the stack trace displays and no fur-ther output occurs, so AEC was the output printed before the stack trace.
What is the output of the following snippet, assuming a and b are both 0? 3: try { 4: return a / b; 5: } catch (RuntimeException e) { 6: return -1; 7: } catch (ArithmeticException e) { 8: return 0; 9: } finally { 10: System.out.print("done"); 11: } A. -1 B. 0 C. done-1 D. done0 E. The code does not compile. F. An uncaught exception is thrown.
E. The order of catch blocks is important because they’re checked in the order they appear after the try block. Because ArithmeticException is a child class of Runtime-Exception, the catch block on line 7 is unreachable. (If an ArithmeticException is thrown in try try block, it will be caught on line 5.) Line 7 generates a compiler error because it is unreachable code.
What is the output of the following program? 1: public class Laptop { 2: public void start() { 3: try { 4: System.out.print("Starting up "); 5: throw new Exception(); 6: } catch (Exception e) { 7: System.out.print("Problem "); 8: System.exit(0); 9: } finally { 10: System.out.print("Shutting down "); 11: } 12: } 13: public static void main(String[] args) { 14: new Laptop().start(); 15: } } A. Starting up B. Starting up Problem C. Starting up Problem Shutting down D. Starting up Shutting down E. The code does not compile. F. An uncaught exception is thrown.
B. The main() method invokes start on a new Laptop object. Line 4 prints Starting
up; then line 5 throws an Exception. Line 6 catches the exception, line 7 prints
Problem, and then line 8 calls System.exit, which terminates the JVM. The finally
block does not execute because the JVM is no longer running.
What is the output of the following program? 1: public class Dog { 2: public String name; 3: public void parseName() { 4: System.out.print("1"); 5: try { 6: System.out.print("2"); 7: int x = Integer.parseInt(name); 8: System.out.print("3"); 9: } catch (NumberFormatException e) { 10: System.out.print("4"); 11: } 12: } 13: public static void main(String[] args) { 14: Dog leroy = new Dog(); 15: leroy.name = "Leroy"; 16: leroy.parseName(); 17: System.out.print("5"); 18: } } A. 12 B. 1234 C. 1235 D. 124 E. 1245 F. The code does not compile. G. An uncaught exception is thrown.
E. The parseName method is invoked within main() on a new Dog object. Line 4 prints
1. The try block executes and 2 is printed. Line 7 throws a NumberFormatException, so
line 8 doesn’t execute. The exception is caught on line 9, and line 10 prints 4. Because the
exception is handled, execution resumes normally. parseName runs to completion, and
line 17 executes, printing 5. That’s the end of the program, so the output is 1245.
What is the output of the following program? 1: public class Cat { 2: public String name; 3: public void parseName() { 4: System.out.print("1"); 5: try { 6: System.out.print("2"); 7: int x = Integer.parseInt(name); 8: System.out.print("3"); 9: } catch (NullPointerException e) { 10: System.out.print("4"); 11: } 12: System.out.print("5"); 13: } 14: public static void main(String[] args) { 15: Cat leo = new Cat(); 16: leo.name = "Leo"; 17: leo.parseName(); 18: System.out.print("6"); 19: } 20: } A. 12, followed by a stack trace for a NumberFormatException B. 124, followed by a stack trace for a NumberFormatException C. 12456 D. 12456 E. 1256, followed by a stack trace for a NumberFormatException F. The code does not compile. G. An uncaught exception is thrown.
A. The parseName method is invoked on a new Cat object. Line 4 prints 1. The try
block is entered, and line 6 prints 2. Line 7 throws a NumberFormatException. It isn’t
caught, so parseName ends. main() doesn’t catch the exception either, so the program
terminates and the stack trace for the NumberFormatException is printed.
What is printed by the following? (Choose all that apply) 1: public class Mouse { 2: public String name; 3: public void run() { 4: System.out.print("1"); 5: try { 6: System.out.print("2"); 7: name.toString(); 8: System.out.print("3"); 9: } catch (NullPointerException e) { 10: System.out.print("4"); 11: throw e; 12: } 13: System.out.print("5"); 14: } 15: public static void main(String[] args) { 16: Mouse jerry = new Mouse(); 17: jerry.run(); 18: System.out.print("6"); 19: } } A. 1 B. 2 C. 3 D. 4 E. 5 F. 6 G. The stack trace for a NullPointerException
A, B, D, G. The main() method invokes run on a new Mouse object. Line 4 prints 1 and line 6 prints 2, so options A and B are correct. Line 7 throws a NullPointerException, which causes line 8 to be skipped, so C is incorrect. The exception is caught on line 9 and line 10 prints 4, so option D is correct. Line 11 throws the exception again, which causes run() to immediately end, so line 13 doesn’t execute and option E is incorrect. The main() method doesn’t catch the exception either, so line 18 doesn’t execute and option F is incorrect. The uncaught NullPointerException causes the stack trace to be printed, so option G is correct.
Which of the following statements are true? (Choose all that apply) A. You can declare a method with Exception as the return type. B. You can declare any subclass of Error in the throws part of a method declaration. C. You can declare any subclass of Exception in the throws part of a method declaration. D. You can declare any subclass of Object in the throws part of a method declaration. E. You can declare any subclass of RuntimeException in the throws part of a method declaration.
A, B, C, E. Classes listed in the throws part of a method declaration must extend
java.lang.Throwable. This includes Error, Exception, and RuntimeException. Arbi-trary classes such as String can’t go there. Any Java type, including Exception, can
be declared as the return type. However, this will simply return the object rather than
throw an exception.
Which of the following can be inserted on line 8 to make this code compile? (Choose all
that apply)
7: public void ohNo() throws IOException {
8: // INSERT CODE HERE
9: }
A. System.out.println(“it’s ok”);
B. throw new Exception();
C. throw new IllegalArgumentException();
D. throw new java.io.IOException();
E. throw new RuntimeException();
A, C, D, E. A method that declares an exception isn’t required to throw one, making
option A correct. Runtime exceptions can be thrown in any method, making options
C and E correct. Option D matches the exception type declared and so is also correct.
Option B is incorrect because a broader exception is not allowed.
Which of the following are unchecked exceptions? (Choose all that apply)
A. ArrayIndexOutOfBoundsException
B. IllegalArgumentException
C. IOException
D. NumberFormatException
E. Any exception that extends RuntimeException
F. Any exception that extends Exception
A, B, D, E. ArrayIndexOutOfBoundsException, IllegalArgumentException, and Num-berFormatException are runtime exceptions. Sorry, you have to memorize them. Any class that extends RuntimeException is a runtime (unchecked) exception. Classes that extend Exception but not RuntimeException are checked exceptions.