What is the result?

Given the code fragment: What is the result?

Given the code fragment: What is the result?

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

B.
Reading Card Checking Card

C.
Compilation fails only at line n1.

D.
Compilation fails only at line n2.

E.
Compilation fails only at line n3.



Leave a Reply 13

Your email address will not be published. Required fields are marked *


IO

IO

D.
Compilation fails only at line n2.

Kanke

Kanke

Answer is Option E.
Error:(18, 20) java: unreported exception java.lang.Exception; must be caught or declared to be thrown

Exceptions must either handled or declared.

TC

TC

Answer is option E

Javier Miranda

Javier Miranda

D salta solo en la primera ocurrencia

vg

vg

D.
Compilation fails only at line n2.

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor’s throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

vg

vg

It is
E.
Compilation fails only at line n3.
Because ex.readCard() throws exception that must be handled

Tomas

Tomas

Answer: E
Tested

Arend

Arend

When running in the compiler i get the following output.

public class Test {

void checkCard(int cardNo) throws Exception {
System.out.println(“Reading Card”);
}
void readCard(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.checkCard(cardNo);
ex.readCard(cardNo);
}
}

Reading Card
Checking Card

Compiles fine

Arend

Arend

Sorry. i shouldn’t have added the “throws Exception” to the main method.

When removing that a Compile error at line /n2

Arend

Arend

actually, line /n3 “Unhandled exception type Exception”.

IF the main method would also have “throws Exception” then compiles fine.

MiT

MiT

Decide man! ;)))
RE are thrown by the JVM.
Only the checked ones must be handled at the compile time.