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.
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.
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.
I think the correct answer is C. The exception for readCard() must be caught or declared to be thrown.
checked exception needs to be thrown
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); } }
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.
The answer is option C.Because checked exception should be declared or handle in main method for readcard method.
C is the correct answer
Vote C: RuntimeException no need trycatch or throws
Answer should be C
Answer - C
C is the correct one, because when method throw checked exception, u have to handle it.
C is correct
C, Unhandled exception: java.lang.Exception ... Runtime is unchecked exception
C is the correct
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".
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); } }
Answer is C Unhandled Exception in line n3
c is ans