SCJP MATERIAL
  <<Back To SCJP Material Main Page
Page: 4

5. Exception Handling

17. Given:
import java.io.*;
class Master {
String doFileStuff() throws FileNotFoundException { return "a"; }
}
class Slave extends Master {
public static void main(String[] args) {
String s = null;
try { s = new Slave().doFileStuff();
} catch ( Exception x) {
s = "b"; }
System.out.println(s);
}
// insert code here
}
Which, inserted independently at // insert code here, will compile, and produce the output
b? (Choose all that apply.)
A. String doFileStuff() { return "b"; }
B. String doFileStuff() throws IOException { return "b"; }
C. String doFileStuff(int x) throws IOException { return "b"; }
D. String doFileStuff() throws FileNotFoundException { return "b"; }
E. String doFileStuff() throws NumberFormatException { return "b"; }
F. String doFileStuff() throws NumberFormatException,
FileNotFoundException { return "b"; }

Answer:
->  A , D, E, and F are correct. It’s okay for an overriding method to throw the same
exceptions, narrower exceptions, or no exceptions. And it’s okay for the overriding
method to throw any runtime exceptions.
->   B is incorrect, because the overriding method is trying to throw a broader exception.
       C is incorrect. This method doesn’t override, so the output is a.

18. Given:
class Input {
public static void main(String[] args) {
String s = "-";
try {
doMath(args[0]);
s += "t "; // line 6
}
finally { System.out.println(s += "f "); }
}
public static void doMath(String a) {
int y = 7 / Integer.parseInt(a);
} }
And the command-line invocations:
java Input
java Input 0
Which are true? (Choose all that apply.)
A. Line 6 is executed exactly 0 times.
B. Line 6 is executed exactly 1 time.
C. Line 6 is executed exactly 2 times.
D. The finally block is executed exactly 0 times.
E. The finally block is executed exactly 1 time.
F. The finally block is executed exactly 2 times.
G. Both invocations produce the same exceptions.
H. Each invocation produces a different exception.
Answer:
-> A , F, and H are correct. Since both invocations throw exceptions, line 6 is never reached. Since both exceptions occurred within a try block, the finally block will always execute. The first invocation throws an   ArrayIndexOutOfBoundsException, and the second invocation throws an ArithmeticException for the attempt to divide by zero.
-> B, C, D, E, and G are incorrect based on the above.

19. Given:
class Plane {
static String s = "-";
public static void main(String[] args) {
new Plane().s1();
System.out.println(s);
}
void s1() {
try { s2(); }
catch (Exception e) { s += "c"; }
}
void s2() throws Exception {
s3(); s += "2";
s3(); s += "2b";
}
void s3() throws Exception {
throw new Exception();
} }
What is the result?
A. -
B. -c
C. -c2
D. -2c
E. -c22b
F. -2c2b
G. -2c2bc
H. Compilation fails.

Answer:
-> B is correct. Once s3() throws the exception to s2(), s2() throws it to s1(), and no
more of s2()’s code will be executed.
-> A, C, D, E, F, G, and H are incorrect based on the above. (Objective 2.5)

20. Given:
try { int x = Integer.parseInt("two"); }
Which could be used to create an appropriate catch block? (Choose all that apply.)
A. ClassCastException
B. IllegalStateException
C. NumberFormatException
D. IllegalArgumentException
E. ExceptionInInitializerError
F. ArrayIndexOutOfBoundsException
Answer:
-> C and D are correct. Integer.parseInt can throw a NumberFormatException, and IllegalArgumentException is its superclass (i.e., a broader exception).
-> A, B, E, and F are not in NumberFormatException’s class hierarchy. (Objective 2.6)

21. Given:
1. class Ping extends Utils {
2. public static void main(String [] args) {
3. Utils u = new Ping();
4. System.out.print(u.getInt(args[0]));
5. }
6. int getInt(String arg) {
7. return Integer.parseInt(arg);
8. }
9. }
10. class Utils {
11. int getInt(String x) throws Exception { return 7; }
12. }
And the following three possible changes:
C1. Declare that main() throws an Exception.
C2. Declare that Ping.getInt() throws an Exception.
C3. Wrap the invocation of getInt() in a try / catch block.
Which change(s) allow the code to compile? (Choose all that apply.)
A. Just C1 is sufficient.
B. Just C2 is sufficient.
C. Just C3 is sufficient.
D. Both C1 and C2 are required.
E. Both C1 and C3 are required.
F. Both C2 and C3 are required.
G. All three changes are required.

Answer:
-> A and C are correct. Remember that line 4 is making a polymorphic call so the compiler knows that an exception might be thrown. If C1 is implemented the exception has been sufficiently declared, and if C3 is implemented the exception has been sufficiently handled. C2 is not necessary in either case.
-> B, D, E, F, and G are incorrect based on the above. (Objective 2.4)

Page: 4

1 2 3 4 5
  <<Back To SCJP Material Main Page



freshers jobs | fresher jobs | freshers job | fresher job | fresher it jobs | sap fresher jobs | jobs for fresher | jobs for freshers | it jobs for freshers | abap fresher jobs | net jobs for freshers | net fresher jobs | net freshers jobs | freshers job sites | fresher job openings | software jobs for freshers | fresher jobs bangalore | hr jobs for freshers | software testing jobs for freshers | fresher jobs india | fresher jobs chennai | fresher it job | freshers it jobs | mba fresher jobs | hr fresher jobs | fresher jobs in india | networking jobs for freshers | testing jobs for freshers | job for fresher | oracle fresher jobs | mca fresher jobs | sap freshers jobs | jobs for mca freshers | fresher jobs hyd | freshers software jobs | it job for freshers | fresher job sites | jobs for mba freshers | freshers job it | hot jobs for freshers | job for freshers | testing fresher jobs | mainframe fresher jobs | jobs for freshers sap | fresher jobs at | fresher jobs usa | jobs for freshers at | jobs for freshers usa | freshers jobs at | freshers jobs usa | tech freshers jobs | net freshers job | software fresher jobs | job opportunities for freshers | jobs for freshers in usa | job vacancies for freshers | jobs mumbai fresher | it jobs for fresher | freshers job openings | job openings for freshers | it jobs for freshers in | freshers jobs bangalore | freshers job india | freshers jobs india | jobs in pune for freshers | jobs for fresher in | fresher jobs in usa | net fresher jobs in | fresher jobs in | fresher job in usa | jobs for freshers in | qa jobs for freshers | fresher jobs in dubai | freshers jobs in usa | job opening for freshers | hr freshers job | freshers job in usa | job openings for freshers in | fresher it jobs in | freshers it jobs in | job for fresher in | job for freshers in | testing jobs for freshers in | sap jobs for freshers in | sap freshers jobs in | sap fresher jobs in | mba freshers jobs | jobs for freshers in delhi | jobs for freshers in mumbai | freshers jobs in delhi | fresher jobs in mumbai | fresher jobs in bangalore | fresher jobs in delhi | net jobs for freshers in | fresher jobs in hyderabad | mba freshers job | freshers job in bangalore | fresher hr jobs in | fresher job in | jobs freshers in | finance jobs for freshers | hr jobs for freshers in | jobs in dubai for freshers | fresher it jobs in bangalore | freshers job in india | freshers job in | software jobs for freshers in | mba fresher job | fresher software jobs in | software testing jobs for freshers in | freshers jobs in mumbai | fresher jobs in chennai | mba fresher jobs in | mba finance fresher jobs | jobs for mba freshers in | jobs for mba fresher | mba freshers jobs in | fresher jobs hyderabad | hr freshers jobs | fresher jobs in pune | freshers jobs in pune | freshers jobs in chennai | jobs for mba finance fresher | freshers job in chennai | jobs for mba finance freshers | finance fresher jobs | jobs for freshers in hyderabad | freshers jobs in hyderabad | jobs for freshers in bangalore | freshers jobs in india | jobs for freshers in chennai | jobs for freshers in india | instrumentation jobs for freshers | freshers jobs in bangalore | engineering jobs for freshers in | job for mba fresher | mechanical fresher jobs | mca freshers jobs | jobs for bca freshers | b tech freshers jobs | sap jobs for fresher | sap abap fresher jobs | job sites for freshers | fresher | jobs | bpo jobs | freshers | careers freshers | freshers it | it jobs | fresher world | mba jobs | j2ee jobs | fresher recruitment | call center jobs | testing jobs | finance jobs | call centre jobs | networking jobs | wipro jobs | hyderabad jobs | chennai jobs | fresher opening | pune jobs | telecom jobs | fresher openings | banglore jobs | bangalore jobs | mca jobs | tcs jobs | kolkata jobs | it fresher | mba fresher | fresher india | gurgaon jobs | hr jobs | noida jobs | india jobs | mumbai jobs | fresh graduates | delhi jobs | freshers recruitment | consultant jobs | engineer jobs | engineering jobs | sap jobs | parttime job | walkin jobs | internet jobs | data entry jobs | jobs career | local jobs | online jobs | parttime jobs | online part time job | technical support jobs | net jobs | jobs at | software jobs | website jobs

Home   |   About Us   |   Contact Us
Copyright @ durgasoft.com