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