Exam 1z0-808 All QuestionsBrowse all questions from this exam
Question 71

Given the following code for the classes MyException and Test:

What is the result?

    Correct Answer: C

    Given the provided code, the output of the program can either be 'A' or 'B'. Here's the reasoning: In the main method, method1() is called within a try block. In method1(), there's a 50% chance that a MyException will be thrown, and a 50% chance that a RuntimeException will be thrown, as determined by Math.random(). Both exceptions are caught by the catch block in method1(), which prints 'B'. Therefore, whenever an exception is thrown in method1(), 'B' is printed. However, if MyException is thrown, it is not rethrown to the main method. Consequently, the catch block in the main method will not be executed, and 'A' will not be printed. The correct answer is that either 'A' (if MyException is thrown and caught) or 'B' (if RuntimeException is thrown and caught). Therefore, the result is either A or B.

Discussion
v323rsOption: B

I agree, the correct answer B B

Chirag3105Option: B

B is only correct answers, as MyException is subclass of RuntimeException, so it will be caught by the catch block in the method1.

admin8Option: C

Answer is C!!!!!

uncopino

wrong. MyException extends RuntimeException

Adel_Kedidi

wrong. for both classes MyException or RuntimeException, we'll have 'B' printed since MyException is a subclass of RuntimeException. Thanks for rectifying...

Nicolas_Cage

yes it is!

JongHwaOption: B

correct answer is B class MyException extends RuntimeException{} public class Test{ public static void main(String[] args) { try { method1(); } catch (MyException e) { System.out.println("A"); } } public static void method1() { try { throw new MyException(); } catch (RuntimeException e) { System.out.println("bb"); } } }

deksero2Option: B

It is B. Runtime catches MyException too.

mete23Option: B

the correct answer B

yanoolthecoolOption: B

exception is caught in method, B.

RipfumeloOption: B

THE ANSWER IS B

carlosworkOption: B

Tested. Answer is B. Source code: class MyException extends RuntimeException{} public class Test { public static void main(String[] args) { try { method1(); } catch (MyException e) { System.out.println("A"); } } public static void method1() { try { throw Math.random() > 0.5 ? new MyException() : new RuntimeException(); } catch (RuntimeException re) { System.out.println("B"); } } }

deksero2Option: B

B is correct beacause MyException extends Runtime exception.

BuhlebesizweMnqobiOption: B

B is Correct

ddpkOption: B

Answer is B. Because MyException is RuntimeException.

Anupam_AnandOption: B

correct answer is B because MyException is RuntimeException itself.

sudar123Option: E

E. compilation error at line n1 "Exception" needs to be declared

uncopino

nope. RuntimeException as any class extenting it is an unchecked exception, plus it is already hadled in a try block so double wrong

lenaOption: B

B , catch (RuntimeException re)” always catches a RuntimeException