Java Reference
In-Depth Information
row = 3 and col = 1 : Line 8 is false so count gets incremented to 2 .
row = 3 and col = 2 : Line 8 is true , the continue executes, and the outer loop is done.
Therefore, the output is 2 and the answer is B.
10. D. You need to tackle these types of questions by analyzing one iteration through the loop
at a time. Let's analyze each step:
m = 9 and n = 1 : m>n is true , m is decremented to 8 , n is incremented to 3 , and x is
8 +3 = 11 .
m = 8 and n = 3 : m>n is true , m is decremented to 7 , n is incremented to 5 , and x is
11 + 7 + 5 = 23 .
m = 7 and n = 5 : m>n is true , m is decremented to 6 , n is incremented to 7 , and x is
23 + 6 + 7 = 36 .
m = 6 and n = 7 : m>n is false , so the loop terminates.
The fi nal value of x is 36 , so the answer is D.
11. E. This is a tough question. The code does not compile, so A, B, and C are incorrect. Line
3 is fi ne — you can declare an infi nite while loop. The compiler is aware that line 3 is an
infi nite loop and that line 6 is an unreachable statement, so the compiler generates an error
at line 6. Therefore, the answer is E.
12. E. The loop control variable y equals 1 and does not change in this do-while loop.
Because 1 <= 10 is always true, this is an infi nite loop and 1 followed by a space displays
indefi nitely, so the answer is E.
13. A. The variable y is declared within the do statement on line 8, so it is out of scope of line
10. Therefore, line 10 generates a compiler error and the answer is A.
14. C. Line 14 compiles fi ne. A control structure that requires a boolean expression can also
use java.lang.Boolean values. The loop control variable i goes from 10 down to 5, then
the loop stops executing. Shifting result to the left 1 on line 13 is the equivalent of mul-
tiplying by 2, so result takes on the successive values 2, 4, 8, 16, and 32 through the fi ve
iterations. Therefore, the answer is C.
15. C and E. The code compiles, so A is incorrect. The command line does not enable
assertions, so E is true and B is false even though the assertion is false. Because no
AssertionError is thrown, line 5 executes and outputs 7, so C is true and D is false.
Therefore, the answer is C and E.
16. B and E. The code compiles fi ne, so A is false. The command line enables assertions, so
C is false. Line 11 changes x to 5, so the assert on line 5 fails and an AssertionError is
thrown, so B is true. The AssertionError causes line 6 to not execute, so D is false and E
is true. Therefore, the answer is B and E.
17. B, C, and E. A java.io.IOException is thrown by many methods in the java.
io package, but it is always thrown programmatically. The same is true for
NumberFormatException; it is thrown programmatically by the wrapper classes of java
.lang. The other three exceptions are all thrown by the JVM when the corresponding
problem arises; therefore, the answer is B, C, and E.
Search WWH ::




Custom Search