PASS - TAKER : Anonymos

Java SE 8 - OCA Flow Control and Exceptions

Correct : 14

87% Complete (success)

87 %

False : 2

12% Complete (success)

12 %



Anonymos 2018-10-26T00:54:58

Correct

1. Given that toLowerCase() is an aptly named String method that returns a String, and given the code: {code}
What is the result?

As of Java 7 it's legal to switch on a String, and remember that switches use "entry point" logic.

Correct

2. Given: {code}
What is the result?

Once s3() throws the exception to s2(), s2() throws it to s1(), and no more of s2()'s code will be executed.

Correct

3. Given: {code}
Which could be used to create an appropriate catch block? (Choose all that apply.)

Integer.parseInt can throw a NumberFormatException, and IllegalArgumentException is its superclass (that is, a broader exception).

Correct

4. Given: {code}
And given the command-line invocation:
Java Flip2 RED Green YeLLow
Which are true? (Choose all that apply.)

As of Java 7 the syntax is legal. The sa[] array receives only three arguments from the command line, so on the last iteration through sa[], a NullPointerException is thrown.

Correct

5. Given: {code}
Which, inserted independently at line 4, compiles? (Choose all that apply.)

A is an example of the enhanced for loop. D and F are examples of the basic for loop. B is incorrect because its operands are swapped. C is incorrect because the enhanced for must declare its first operand. E is incorrect syntax to declare two variables in a for statement.

Correct

6. Given: {code}
What is the result?

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

Not Correct

7. Given: {code}
What is the result? (Choose all that apply.)

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

Correct

8. Given: {code}
What is the result?

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

Correct

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

Because i was not initialized, case 1 will throw a NullPointerException. Case 0 will initiate an endless loop, not a stack overflow. Case 2's downcast will not cause an exception.

Correct

10. Given: {code}
What is the result?

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!

Correct

11. Given: {code}
What is the result?

It's true that the value of String s is 123 at the time that the divide-byzero 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.

Correct

12. Given: {code}
What is the result?

A break breaks out of the current innermost loop and carries on. A labeled break breaks out of and terminates the labeled loops.

Correct

13. Given: {code}
And given the following three code fragments:
I. new Gotcha().go();
II. try { new Gotcha().go(); }
catch (Error e) { System.out.println("ouch"); }
III. try { new Gotcha().go(); }
catch (Exception e) { System.out.println("ouch"); }
When fragments I–III are added, independently, at line 5, which are true? (Choose all that apply.)

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.

Correct

14. Given the code snippet: {code}
And given that the numbered lines will all be tested by uncommenting one switch statement and one case statement together, which line(s) will FAIL to compile? (Choose all that apply.)

A switch's cases must be compile-time constants or enum values.

Correct

15. Given that IOException is in the java.io package and given: {code}
And given the following four code fragments:
I. public static void main(String[] args) {
II. public static void main(String[] args) throws Exception {
III. public static void main(String[] args) throws IOException {
IV. public static void main(String[] args) throws RuntimeException {
If the four fragments are inserted independently at line 2, which are true? (Choose all that apply.)

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. E is incorrect because it's okay both to handle and declare an exception.

Not Correct

16. Given: {code}
And given the following four code fragments:
I. void doStuff() {
II. void doStuff() throws MyException {
III. void doStuff() throws RuntimeException {
IV. void doStuff() throws ArithmeticException {
When fragments I–IV are added, independently, at line 10, which are true? (Choose all that apply.)

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.