Answer:

 

Image E is correct. There is no problem nesting try / catch blocks. As is normal, when an exception is thrown, the code in the catch block runs, then the code in the finally block runs.

 

Image A, B, C, D, andF are incorrect based on the above. (Objective 2.5)

 

7. Given:

Image

 

What is the result? (Choose all that apply.)

A. Compilation succeeds

B. Compilation fails due to an error on line 8

C. Compilation fails due to an error on line 10

D. Compilation fails due to an error on line 12

E. Compilation fails due to an error on line 14

Answer:

 

Image C is correct. An overriding method cannot throw a broader exception than the method it's overriding. Class CC,s method is an overload, not an override.

 

Image A, B, D, and E are incorrect based on the above. (Objectives 1.5, 2.4)

 

8. Given:

Image

 

What is the result?

A. 9 10 d

B. 8 9 10 d

C. 9 10 10 d

D. 9 10 10 d 13

E. 8 9 10 10 d 13

F. 8 9 10 9 10 10 d 13

G. Compilation fails

Answer:

 

Image D is correct. Did you catch the static initializer block? Remember that switches work on "fall-thru" logic, and that fall-thru logic also applies to the default case, which is used when no other case matches.

 

Image A, B, C, E, F, and G are incorrect based on the above. (Objective 2.1)

 

9. Given:

Image

 

And given that line 7 will assign the value 0, 1, or 2 to sw, which are true? (Choose all that apply.)

A. Compilation fails

B. A ClassCastException might be thrown

C. A StackOverflowError might be thrown

D. A NullPointerException might be thrown

E. An IllegalStateException might be thrown

F. The program might hang without ever completing

G. The program will always complete without exception

Answer:

 

Image D and F are correct. Because i was not initialized, case 1 will throw an NPE. Case 0 will initiate an endless loop, not a stack overflow. Case 2's downcast will not cause an exception.

 

Image A, B, C, E, and G are incorrect based on the above. (Objective 2.6)

 

10. Given:

Image

 

What is the result?

A. 1 3 9

B. 5 5 7 7

C. 1 3 3 9 9

D. 1 1 3 3 9 9

E. 1 1 1 3 3 3 9 9 9

F. Compilation fails

Answer:

 

Image D is correct. The basic rule for unlabeled continue statements is that the current iteration stops early and execution jumps to the next iteration. The last two continue statements are redundant!

 

Image A, B, C, E, and F are incorrect based on the above. (Objective 2.2)

 

11. Given:

Image

 

What is the result?

A. 12

B. 13

C. 123

D. 1234

E. Compilation fails

F. 123 followed by an exception

G. 1234 followed by an exception

H. An exception is thrown with no other output

Answer:

 

Image H is correct. It's true that the value of String s is 123 at the time that the divide-by-zero exception is thrown, but finally() is not guaranteed to complete, and in this case finally() never completes, so the System.out.println (S.O.P.) never executes.

 

Image A, B, C, D, E, F, and G are incorrect based on the above. (Objective 2.5)

 

12. Given:

Image

 

What is the result?

A. 0 1 2 3

B. 1 1 1 3 3

C. 0 1 1 1 2 3 3

D. 1 1 1 3 3 4 4 4

E. 0 1 1 1 2 3 3 4 4 4

F. Compilation fails

Answer:

 

Image C is correct. A break breaks out of the current innermost loop and continues. A labeled break breaks out of and terminates the current loops.

 

Image A, B, D, E, and F are incorrect based on the above. (Objective 2.2)

 

13. Given:

Image

 

And given the following three code fragments:

Image

 

When fragments I - III are added, independently, at line 5, which are true? (Choose all that apply.)

A. Some will not compile

B. They will all compile

C. All will complete normally

D. None will complete normally

E. Only one will complete normally

F. Two of them will complete normally

Answer:

 

Image B and E are correct. First off, go() is a badly designed recursive method, guaranteed to cause a StackOverflowError. Since Exception is not a superclass of Error, catching an Exception will not help handle an Error, so fragment III will not complete normally. Only fragment II will catch the Error.

 

Image A, C, D, and F are incorrect based on the above. (Objective 2.5)

 

14. Given:

Image

 

Which are true? (Choose all that apply.)

A. Compilation succeeds

B. Compilation fails due to an error on line 6

C. Compilation fails due to an error on line 7

D. Compilation fails due to an error on line 8

E. Compilation fails due to an error on line 9

F. Compilation fails due to an error on line 10

Answer:

 

Image E is correct. When an assert statement has two expressions, the second expression must return a value. The only two-expression assert statement that doesn't return a value is on line 9.

 

Image A, B, C, D, and F are incorrect based on the above. (Objective 2.3)

 

15. Given:

Image

 

And given the following four code fragments:

Image

 

If the four fragments are inserted independently at line 4, which are true? (Choose all that apply.)

A. All four will compile and execute without exception

B. All four will compile and execute and throw an exception

C. Some, but not all, will compile and execute without exception

D. Some, but not all, will compile and execute and throw an exception

E. When considering fragments II, III, and IV, of those that will compile, adding a try/catch block around line 6 will cause compilation to fail

Answer:

 

Image D is correct. This is kind of sneaky, but remember that we're trying to toughen you up for the real exam. If you're going to throw an IOException, you have to import the java.io package or declare the exception with a fully qualified name.

 

Image E is incorrect because it's okay to both handle and declare an exception. A, B, and C are incorrect based on the above. (Objective 2.4)

 

16. Given:

Image

 

And given the following four code fragments:

Image

 

When fragments I - IV are added, independently, at line 10, which are true? (Choose all that apply.)

A. None will compile

B. They will all compile

C. Some, but not all, will compile

D. All of those that compile will throw an exception at runtime

E. None of those that compile will throw an exception at runtime

F. Only some of those that compile will throw an exception at runtime

Answer:

 

Image C and D are correct. An overriding method cannot throw checked exceptions that are broader than those thrown by the overridden method. However an overriding method can throw RuntimeExceptions not thrown by the overridden method.

 

Image A, B, E, and F are incorrect based on the above. (Objective 2.4)

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset