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.
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.
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);
}
}
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
RoxyFoxy
Highly Voted 2 years, 4 months agoMPignaProTech
Most Recent 2 months, 1 week agojoeMP
3 months agoz24134
1 year, 2 months agoyefiw
1 year, 2 months agoarjunrawatirissoftware
1 year, 3 months agoDriftKing
1 year, 4 months agoduydn
1 year, 4 months agoa_really_reliable_programmer
1 year, 4 months agoSreeni_A
1 year, 4 months agojlicini
1 year, 5 months agodsms
1 year, 5 months agobilly_the_kid
1 year, 8 months agoVicky_65
1 year, 9 months agoShad657
1 year, 11 months agoAnkit1010
1 year, 11 months agohaisaco
2 years ago