ch. 12

Ace your homework & exams now with Quizwiz!

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); } catch(Exception e) { } } catch(RuntimeException e) { System.out.println(e); } } }

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.

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

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

What exception type does the following program throw? public class Test { public static void main(String[] args) { System.out.println(1 / 0); } }

ArithmeticException

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]); } }

ArrayIndexOutOfBoundsException

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; } }

ClassCastException

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.

Error

An instance of _________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.

Exception

Which of the following is not an advantage of Java exception handling?

Exception handling improves performance.

What exception type does the following program throw? public class Test { public static void main(String[] args) { Object o = null; System.out.println(o); } }

No exception

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()); } }

NullPointerException

An instance of _________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. A. RuntimeException

RuntimeException

Instances of _________ are unchecked exceptions.

RuntimeException Error NumberFormatException

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)); } }

StringIndexOutOfBoundsException

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); 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"); } } }

The program displays NumberFormatException followed by RuntimeException.

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); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } }

The program displays NumberFormatException.

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); 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"); } } }

The program displays RuntimeException followed by After the method call.

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"); } }

The program displays Welcome to Java and End of the block, and then terminates because of an unhandled exception.

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"); } }

The program displays Welcome to Java two times followed by End of the block two times.

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"); } } }

The program displays Welcome to Java two times followed by End of the block.

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"); } } }

The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.

Analyze the following code: public class Test { public static void main(String[] args) { try { String s = "5.6"; Integer.parseInt(s); int i = 0; int y = 2 / i; } catch (Exception ex) { System.out.println("NumberFormatException"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } } }

The program has a compile error.

A Java exception is an instance of __________.

Throwable

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"); } } }

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

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"); } } }

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

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"); } } }

Welcome to Java followed by The finally clause is executed in the next line, then an error message.

What is wrong in the following program? public class Test { public static void main (String[] args) { try { System.out.println("Welcome to Java"); } } }

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

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 { }

You should not declare a class that extends Error, because Error raises a fatal error that terminates the program.

Which of the following statements are true?

You use the keyword throws to declare exceptions in the method heading. A method may declare to throw multiple exceptions. To throw an exception, use the key word throw. If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method.

A method must declare to throw ________.

checked exceptions

The following code causes Java to throw _________. int number = Integer.MAX_VALUE + 1;

no exceptions


Related study sets

Social studies Midterms, Chapter 15 Focus Questions, ME 1.III, ME I.V, TnT1, History: World [12] The Making of Europe, History: World [13] The Byzantine Empire, Crisis, and Recovery in the West

View Set

Code Standards and Practices 2 - Level 2 Study Guide #1 and 2

View Set

People of the American Revolution

View Set

flower stuff lol eheh eheheh ehehhehehe :(

View Set