04-Flow Control, Exceptions and Assertions PDF
04-Flow Control, Exceptions and Assertions PDF
A. 0 1 2
B. 0 1 2 1 2 2
C. Compilation fails at line 7.
D. Compilation fails at line 8.
E. Compilation fails at line 9.
F. An exception is thrown at runtime.
ANSWER: C.
Case expressions must be constant expressions. Since x is marked final, lines 8 and 9 are legal;
however, y is not a final so the compiler will fail at line 7.
A. 0 1 2
B. 0 1 2 1 2 2
C. 2 1 0 1 0 0
D. 2 1 2 0 1 2
E. Compilation fails at line 8.
F. Compilation fails at line 9.
ANSWER: D
The case expressions are all legal because x is marked final, which means the expressions can
be evaluated at compile time. In the first iteration of the for loop case x-2 matches, so 2 is
printed. In the second iteration, x-1 is matched so 1 and 2 are printed (remember, once a match
is found all remaining statements are executed until a break statement is encountered). In the
third iteration, x is matched so 0 1 and 2 are printed.
A. 41
B. 42
C. 50
D. 51
E. Compiler fails at line 5.
F. Compiler fails at line 6.
ANSWER: D
In Java, boolean instance variables are initialized to false, so the if test on line 5 is true and hand
is incremented. Line 6 is legal syntax, a do nothing statement. The else-if is true so hand has 7
added to it and is then incremented.
A. 0 def 1
B. 2 1 0 def 1
C. 2 1 0 def def
D. 2 1 def 0 def 1
E. 2 1 2 0 def 1 2
F. 2 1 0 def 1 def 1
ANSWER: F
When z == 0 , case x-2 is matched. When z == 1, case x-1 is matched and then the break occurs.
When z == 2, case x, then default, then x-1 are all matched. When z == 3, default, then x-1 are
matched. The rules for default are that it will fall through from above like any other case (for
instance when z == 2), and that it will match when no other cases match (for instance when z
== 3).
A. 0
B. 1
C. 101
D. 111
E. 1001
F. 1101
ANSWER: C
As instance variables, b1 and b2 are initialized to false. The if tests on lines 5 and 6 are
successful so b1 is set to true and x is incremented. The next if test to succeed is on line 13
(note that the code is not testing to see if b2 is true, it is setting b2 to be true). Since line 13 was
successful, subsequent else-if’s (line 14) will be skipped.
ANSWER: D.
Using the integer 1 in the while statement, or any other looping or conditional construct for that
matter, will result in a compiler error. This is old C syntax, not valid Java.
A, B, C, E, and F are incorrect because line 1 is valid (Java is case sensitive so While is a valid
class name). Line 5 is also valid because an equation may be placed in a String operation as
shown.
1. class For {
2. public void test() {
3.
4. System.out.println("x = "+ x);
5. }
6. }
7. }
x=0x=1
Which two lines of code (inserted independently) will cause this output? (Choose two.)
ANSWER: D and E
It doesn’t matter whether you preincrement or postincrement the variable in a for loop; it is
always incremented after the loop executes and before the iteration expression is evaluated.
A and B are incorrect because the first iteration of the loop must be zero. C is incorrect because
the test will fail immediately and the for loop will not be entered.
A. I is 1
B. I is 1 I is 1
C. No output is produced.
D. Compilation error
E. I is 1 I is 1 I is 1 in an infinite loop.
ANSWER: C
There are two different looping constructs in this problem. The first is a do-while loop and the
second is a while loop, nested inside the do-while. The body of the do-while is only a single
statement—brackets are not needed. You are assured that the while expression will be
evaluated at least once, followed by an evaluation of the do-while expression. Both expressions
are false and no output is produced.
11. int I = 0;
12. outer:
13. while (true) {
14. I++;
15. inner:
16. for (int j = 0; j < 10; j++) {
17. I += j;
18. if (j == 3)
19. continue inner;
20. break outer;
21. }
22. continue outer;
23. }
24. System.out.println(I);
25.
26.
A. 1
B. 2
C. 3
D. 4
ANSWER: A.
The program flows as follows: I will be incremented after the while loop is entered, then I will
be incremented (by zero) when the for loop is entered. The if statement evaluates to false, and
the continue statement is never reached. The break statement tells the JVM to break out of the
outer loop, at which point I is printed and the fragment is done.
1. int I = 0;
2. label:
3. if (I < 2) {
4. System.out.print("I is " + I);
5. I++;
6. continue label;
7. }
The code will not compile because a continue statement can only occur in a looping construct.
If this syntax were legal, the combination of the continue and the if statements would create a
kludgey kind of loop, but the compiler will force you to write cleaner code than this.
Exceptions
1. System.out.print("Start ");
2. try {
3. System.out.print("Hello world");
4. throw new FileNotFoundException();
5. }
6. System.out.print(" Catch Here ");
7. catch(EOFException e) {
8. System.out.print("End of file exception");
9. }
10. catch(FileNotFoundException e) {
11. System.out.print("File not found");
12. }
and given that EOFException and FileNotFoundException are both subclasses of IOException,
and further assuming this block of code is placed into a class, which statement is most true
concerning this code?
ANSWER: A
Line 6 will cause a compiler error. The only legal statements after try blocks are either catch or
finally statements.
B, C, and D are incorrect based on the program logic described above. If line 6 was removed, the
code would compile and the correct answer would be B.
A. Nothing. The program will not compile because no exceptions are specified.
B. Nothing. The program will not compile because no catch clauses are specified.
C. Hello world.
D. Hello world Finally executing
ANSWER: D
Finally clauses are always executed. The program will first execute the try block, printing Hello
world, and will then execute the finally block, printing Finally executing.
A, B, and C are incorrect based on the program logic described above. Remember that either a
catch or a finally statement must follow a try. Since the finally is present, the catch is not
required.
1. import java.io.*;
2. public class MyProgram {
3. public static void main(String args[]){
4. FileOutputStream out = null;
5. try {
6. out = new FileOutputStream("test.txt");
7. out.write(122);
8. }
9. catch(IOException io) {
10. System.out.println("IO Error.");
11. }
12. finally {
13. out.close();
14. }
15. }
16. }
and given that all methods of class FileOutputStream, including close(), throw an
IOException, which of these is true? (Choose one.)
ANSWER: E
Any method (in this case, the main() method) that throws a checked exception (in this case,
out.close() ) must be called within a try clause, or the method must declare that it throws the
exception. Either main() must declare that it throws an exception, or the call to out.close() in
the finally block must fall inside a (in this case nested)
try-catch block.
ANSWER: D
Once the program throws a RuntimeException (in the throwit() method) that is not caught, the
finally block will be executed and the program will be terminated. If a method does not handle
an exception, the finally block is executed before the exception is propagated.
ANSWER: E
The main() method properly catches and handles the RuntimeException in the catch block,
finally runs (as it always does), and then the code returns to normal.
A, B, C, D, and F are incorrect based on the program logic described above. Remember that
properly handled exceptions do not cause the program to stop executing.
Assertions
A. In an assert statement, the expression after the colon ( : ) can be any Java expression.
B. If a switch block has no default, adding an assert default is considered appropriate.
C. In an assert statement, if the expression after the colon ( : ) does not have a value, the assert’s
error message will be empty.
D. It is appropriate to handle assertion failures using a catch clause.
ANSWER: B.
A is incorrect because only Java expressions that return a value can be used. For instance, a
method that returns void is illegal. C is incorrect because the expression after the colon must
have a value. D is incorrect because assertions throw errors and not exceptions, and assertion
errors do cause program termination and should not be handled.
17. Which two of the following statements are true? (Choose two.)
ANSWER: A and B
A. bar
B. bar done
C. foo done
D. bar foo done
E. Compilation fails
F. An error is thrown at runtime.
ANSWER: E
The foo() method returns void. It is a perfectly acceptable method, but because it returns void
it cannot be used in an assert statement, so line 14 will not compile.
19. Which two of the following statements are true? (Choose two.)
A. If assertions are compiled into a source file, and if no flags are included at runtime, assertions
will execute by default.
B. As of Java version 1.4, assertion statements are compiled by default.
C. With the proper use of runtime arguments, it is possible to instruct the VM to disable
assertions for a certain class, and to enable assertions for a certain package, at the same time.
D. The following are all valid runtime assertion flags:
-ea, -esa, -dsa, -enableassertions,
-disablesystemassertions
E. When evaluating command-line arguments, the VM gives –ea flags precedence over –da
flags.
ANSWER: C and D
C is true because multiple VM flags can be used on a single invocation of a Java program. D is
true, these are all valid flags for the VM.
A. Line 8
B. Line 9
C. Line 11
D. Line 15
E. Line 18
ANSWER: E
Assert statements should not cause side effects. Line 18 changes the value of z if the assert
statement is false.