CS 2114 VT - Exceptions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What code would you use to test if the sum() method of the MyCalculator class (below) is throwing the correct exception? ----------------------------------------------------------- public int sum(String num1String, String num2String) { int sum = 0; try { int num1 = Integer.parseInt(num1String); int num2 = Integer.parseInt(num2String); sum = num1 + num2; } catch (NumberFormatException nfe) { throw new NumberFormatException(); } return (sum); } ----------------------------------------------------------- a) ------------------------------------------------------ NumberFormatException myNFE = null; try { calc.sum("2hello", "3"); } catch (NumberFormatException nfe) { myNFE = nfe; } assertNotNull(myNFE); b) ------------------------------------------------------ NumberFormatException myNFE = null; try { calc.sum("2", "0"); } catch (NumberFormatException nfe) { myNFE = nfe; } assertNotNull(myNFE); c) ------------------------------------------------------ ArithmeticException myAE = null; try { calc.sum("2", "0"); } catch (ArithmeticException ae) { myAE = ae; } assertNotNull(ae); d) ------------------------------------------------------ NumberFormatException myNFE = null; try { calc.sum("2hello", "3"); } catch (NumberFormatException nfe) { nfe = myNFE; } assertNotNull(myNFE);

Ans: a) ------------------------------------------------------ NumberFormatException myNFE = null; try { calc.sum("2hello", "3"); } catch (NumberFormatException nfe) { myNFE = nfe; } assertNotNull(myNFE);

We implement the following code and receive an error message on the riskyCodeThatWantsToDefer() method call indicating "Unhandled exception type IOException". ----------------------------------------------------------- public void riskyCodeThatWantsToDefer ( ) throws IOException { //some code } public void callingMethod() { riskyCodeThatWantsToDefer(); } ----------------------------------------------------------- We realize that the riskyCodeThatWantsToDefer ( ) method has passed exception handling responsibility to callingMethod( ). Which of the modifications listed below would resolve the error? (select all which apply) a) ------------------------------------------------------ public static void callingMethod() { try { riskyCodeThatWantsToDefer(); } catch (IOException e) { e.printStackTrace(); } } b) ------------------------------------------------------ public static void callingMethod() throws IOException { riskyCodeThatWantsToDefer(); }

Ans: a) ------------------------------------------------------ public static void callingMethod() { try { riskyCodeThatWantsToDefer(); } catch (IOException e) { e.printStackTrace(); } } b) ------------------------------------------------------ public static void callingMethod() throws IOException { riskyCodeThatWantsToDefer(); }

Which of the following could result in code throwing a null pointer exception? Check all that apply a) Attempting to invoke a method from a null object b) Attempting to use an object that was declared but never instantiated c) Attempting to use an object that was instantiated but never declared d) Using an object that became null

Ans: a) Attempting to invoke a method from a null object b) Attempting to use an object that was declared but never instantiated d) Using an object that became null

The following is a valid combination of try and finally blocks. try { // statements } finally { // statements } a) True b) False

Ans: a) True

If the following hierarchy of exception is defined by a user, which option is the correct order of catching these exceptions? ----------------------------------------------------------- class firstLevelException extends Exception{} class secondLevelException_1 extends firstLevelException{} class secondLevelException_2 extends firstLevelException{} class thirdLevelException extends secondLevelException_1{} ----------------------------------------------------------- a) ------------------------------------------------------ try{ //code was removed }catch (firstLevelException e){ e.printStackTrace(); } catch (secondLevelException_1 e){ e.printStackTrace(); } catch (secondLevelException_2 e){ e.printStackTrace(); } catch (thirdLevelException e){ e.printStackTrace(); } b) ------------------------------------------------------ try{ //code was removed } catch (thirdLevelException e){ e.printStackTrace(); } catch (secondLevelException_1 e){ e.printStackTrace(); } catch (secondLevelException_2 e){ e.printStackTrace(); } catch (firstLevelException e){ e.printStackTrace(); } c) ------------------------------------------------------ try{ //code was removed } catch (firstLevelException e){ e.printStackTrace(); } catch (secondLevelException_2 e){ e.printStackTrace(); } catch (secondLevelException_1 e){ e.printStackTrace(); } catch (thirdLevelException e){ e.printStackTrace(); } d) ------------------------------------------------------ try{ //code was removed } catch (thirdLevelException e){ e.printStackTrace(); } catch (firstLevelException e){ e.printStackTrace(); } catch (secondLevelException_2 e){ e.printStackTrace(); } catch (secondLevelException_1 e){ e.printStackTrace(); }

Ans: b) ---------------------------------------- try{ //code was removed } catch (thirdLevelException e){ e.printStackTrace(); } catch (secondLevelException_1 e){ e.printStackTrace(); } catch (secondLevelException_2 e){ e.printStackTrace(); } catch (firstLevelException e){ e.printStackTrace(); }

Which of the following statements are true? Check all that apply. a) A try block can have only one catch block b) A catch block must have an associated try block c) A try block can have multiple catch blocks d) Code in a finally block will always execute whether or not an exception occurs in the try block

Ans: b) A catch block must have an associated try block c) A try block can have multiple catch blocks d) Code in a finally block will always execute whether or not an exception occurs in the try block

When an exception occurs within a method, the method creates an Exception object then hands it off to the Java runtime system to take further action, this is referred to as a) instantiating a semaphore b) throwing an exception c) runtime passing d) exception passing

Ans: b) throwing an exception

What output is produced when the test method is invoked? ----------------------------------------------------------- public static void test() { String a = "6we"; String b = "0"; System.out.print("A"); try { int result = Integer.parseInt(a) / Integer.parseInt(b); System.out.println(result); } catch (NumberFormatException nfe) { System.out.print("B"); } catch (ArithmeticException ae) { System.out.print("C"); } finally { System.out.print("D"); } System.out.print("E"); } ----------------------------------------------------------- a) ABCDE b) ABCD c) ABCE d) ABDE e) ACE

Ans: d) ABDE

What type of exception will be thrown when the myMethod() method is invoked? ----------------------------------------------------------- public static void myMethod() { int a = 6; int b = 0; try { int result = a/b; System.out.println(result); } catch (NumberFormatException nfe) { nfe.printStackTrace(); } catch (ArithmeticException ae) { ae.printStackTrace(); } } ----------------------------------------------------------- a) NumberFormatException b) NullPointerException c) ArrayIndexOutOfBoundsException d) ArithmeticException

Ans: d) ArithmeticException

Which pair of code blocks are used to enclose exception-prone code, and to check for and handle exceptional events? a) if and then b) try and finally c) catch and while d) try and catch

Ans: d) try and catch

Which of these Java keywords can be used to intentionally cause an exception? a) exception b) try c) while d) finally e) catch f) throw

Ans: f) throw


Set pelajaran terkait

Unit 4: Sensation and Perception

View Set

PA Envirothon - Soils, PA Envirothon Soils

View Set

Evidence-Based Practice in Nursing

View Set

0x05. C - Pointers, arrays and strings

View Set

Chapter 01 The Core Principles of Economics

View Set

Exam 2: Election of 1800 and its significance

View Set

Chapter 3: Taxes, retirement, etc.

View Set

Unit2 Prokaryotic and Eukaryotic Cells

View Set