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

Given the code fragment:

What is the result?

A.

B. Compilation fails only at line n1.

C. Compilation fails only at line n2.

D. Compilation fails only at line n3.

E. Compilation fails at both line n2 and line n3.

    Correct Answer:

    The correct answer is C. In Java, methods that throw checked exceptions must handle those exceptions using a try-catch block or declare the exception to be thrown using the throws keyword in the method signature where the method is called. In this code, the readCard method throws a checked exception (Exception), but in the main method, this exception is neither caught nor declared to be thrown. Therefore, the compilation will fail at line n2. The checkCard method throws a RuntimeException, which is an unchecked exception, so it does not need to be explicitly handled in the main method, and line n3 will compile successfully.

Discussion
RoxyFoxy

I think the correct answer is C. The exception for readCard() must be caught or declared to be thrown.

z24134

checked exception needs to be thrown

Drift_King

Answer is C. Compilation fails only at line n2. \\Code public class Test { void readCard(int cardNo) throws Exception { System.out.println("Reading Card"); } void checkCard(int cardNo) throws RuntimeException { System.out.println("Checking Card"); } public static void main(String[] args) { Test ex = new Test(); int cardNo = 12344; ex.readCard(cardNo); ex.checkCard(cardNo); } }

a_really_reliable_programmer

Answer is C A, compile error (Does not print) B, The method itself is not wrong, you just have to try/catch C, Did not catch the Exception D, Last line won't fail because it's method is runtime and runtime doesn't happen if it doesn't even compile.

Vicky_65

The answer is option C.Because checked exception should be declared or handle in main method for readcard method.

Ankit1010

C is the correct answer

haisaco

Vote C: RuntimeException no need trycatch or throws

yefiw

Answer should be C

arjunrawatirissoftware

Answer - C

duydn

C is the correct one, because when method throw checked exception, u have to handle it.

Sreeni_A

C is correct

jlicini

C, Unhandled exception: java.lang.Exception ... Runtime is unchecked exception

dsms

C is the correct

billy_the_kid

Answer is C. Checked Exceptions should be handled or declared, in this case the problem is that we missed to declare the Exception in main method. So in order your code to pass the compilation, you should add at the main method "throws Exception".

Shad657

The answer should be C. The compilation error can be removed by doing this - class Test { void readCard (int cardNo) throws Exception { System.out.println("Reading Card"); } void checkCard (int cardNo) throws RuntimeException { System.out.println("Checking Card"); } public static void main (String[] args) throws Exception { Test ex = new Test(); int cardNo = 12344; ex. readCard (cardNo) ; ex. checkCard (cardNo); } }

willokans

Answer is C Unhandled Exception in line n3

anmoldev2java

c is ans