Dan Chisholm's
Java Programmer Certification Mock Exam

Please Help Save a Tree!

The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.

Today, you can find my book at amazon.com.

Answers: Java Programmer Certification Mock Exam
No.AnswerRemark
1a  e  -ea  -enableassertions  Two command-line switches used to enable assertions in non-system classes are -ea and -enableassertions.  
2a  e  -da  -disableassertions  Two command-line switches used to disable assertions are -da and -disableassertions. Remember that all of the letters are lower case.  
3Compile-time error  A compile-time error is generated, because the second catch clause attempts to catch an exception that is never thrown in the try block.  
4-disableassertions  Two command-line switches used to disable assertions in non-system classes are -da and -disableassertions. Remember that all of the letters are lower case.  
5c  g  -esa  -enablesystemassertions  Two command-line switches used to enable assertions in system classes are -esa and -enablesystemassertions. Remember that all of the letters are lower case.  
6a  b  d  e  f  By default assertions are disabled at run-time.  Assertions can be selectively enabled for any named class.  Assertions can be selectively enabled for any named package.  If assertions are selectively enabled for any named package then assertions are automatically enabled for the subpackages.  Assertions can be selectively enable for the unnamed package in the current working directory.   
7AssertionError  An AssertionError is thrown to indicate that an assertion has failed. Don't be fooled by the name AssertionException.  
8Prints: ABCBCC  The switch statement does not contain any break statements. The first case falls through to the second. The second case falls through to the third. There is no default switch label, so nothing is printed when variable i is 3.  
9Compile-time error  The loop variable, h, is local to the for statement. The print statement causes a compile-time error, because it refers to the out-of-scope local variable, h. Always check for variables that are out-of-scope!  
10Compile-time error  A throw statement is the first statement in the outer try block. A throw statement appearing in a try block causes control to pass out of the block. Consequently, statements can not be reached if they appear in the block after the throw statement. The switch statement that appears after the throw statement is unreachable and results in a compile-time error.  
11d  e  Compile-time error at line 2  Compile-time error at line 3  A compile-time error occurs at line 2 as a result of the attempt to shadow the method local variable, k, within the for-loop. A variable declared within the for-loop can not shadow a local variable of the enclosing method. At line 3, the declaration of variable j causes a compile-time error. The initialization part of a for statement may use a single local variable declaration statement to declare more than one local variable. Each of the new variables is declared in a comma separated list of variable declarators. In this program, the local variables should have been declared as follows: "int i=0, j=0;". Instead, the type of variable j has been incorrectly added to the declarator: "int i=0, int j=0;". The result is a compile-time error.  
12Prints: 1212013  A method local variable, i, is declared at line 3. At line 4, the initialization part of the for statement initializes the method local variable to the value zero. The for statement does not declare a new variable, i, so the method local variable is not shadowed within the loop. Suppose a local variable, v, is declared within a statement or block. Variable, v, can shadow a class variable or an instance variable of the same name, but a compile-time error is generated if v shadows a method local variable. Please note that a compile-time error is not generated if a local variable is shadowed within the declaration of a class that is nested within the scope of the local variable.  
13Compile-time error at line 4  A method local variable, i, is declared at line 3. At line 4, the initialization part of the for statement attempts to shadow the method local variable with a new variable that is intended to be local to the for statement. The result is a compile-time error. At line 2, a new variable, i, is declared within the for statement. That variable shadows the class variable, but it does not shadow the method local variable declared at line 3. The scope of a method local variable begins with its own initializer--if present--and extends to the end of the method body. At line 2, the method local variable is not in scope, so shadowing does not occur. Suppose a local variable, v, is declared within a statement or block. Variable, v, can shadow a class variable or an instance variable of the same name, but a compile-time error is generated if v shadows a method local variable. Please note that a compile-time error is not generated if a local variable is shadowed within the declaration of a class that is nested within the scope of the local variable.  
14Prints: 012345  This trick question has a do-loop nested inside of a while-loop. For each iteration, the values of i and j are as follows: (1,0)(1,1)(1,2)(1,3)(2,4)(3,5).  
15Prints: i1k1i2j1i3j2  A while loop is nested inside of a do loop. The while loop iterates once during the first iteration of the do loop. The body of the while loop does not execute during the final iteration of the do loop.  
16Prints: A0B0J1C1B1J2C2B2  The initialization statement, labeled A, is processed first. The boolean expression, labeled B, is processed before each iteration of the loop. The body of the loop is processed next followed by the update expression, labeled C.  
17Prints: 6,6  Suppose the print statement, System.out.print("(" + i + "," + j + ")");, is inserted at the top of the loop. The output of the program would be as follows: (0,9)(2,8)(4,7)6,6. The variable, i, is incremented twice with each pass through the loop. The variable, j, is decremented once with each pass. The loop terminates when i reaches six.  
18Prints: 9,4  Suppose the print statement, System.out.print("(" + i + "," + j + ")");, is inserted at the top of the loop. The output of the program would be as follows: (1,8)(3,7)(5,6)(7,5)9,4. The variable, i, is incremented twice with each pass through the loop. The variable, j, is decremented once with each pass. The boolean expression of the while loop, i++ <= j--, is false when i reaches 8 and j reaches 5. The value of i is subsequently incremented and j is decremented yielding 9 and 4 respectively. Those values are printed.  
19Prints: 8,4  Suppose the print statement, System.out.print("(" + i + "," + j + ")");, is inserted at the top of the loop. The output of the program would be as follows: (0,9)(2,8)(4,7)(6,6)(7,5)8,4. The initial value of j is 9. With each pass through the loop, the expression, j-- < 7, decrements j. The initial value of variable i is 0. With each pass through the loop, the expression, i++ < 7, increments variable i. Inside the body of the loop, i is also incremented as long as j is greater than or equal to 7. When j is less than seven, variable i is no longer incremented inside the body of the loop, but it is still incremented by the expression, i++ < 7.  
20Prints: 3,1  Suppose the print statement, System.out.print("("+i+","+j+","+k+")");, is inserted before the switch statement. The output of the program would be as follows: (1,0,1)(2,0,2)(2,1,3)(3,1,4)3,1. On the first iteration, case 1 is processed. The "continue label1;" statement causes control to go directly to the top of the for-loop following the label, label1. The boolean expression of the do-loop is not processed. On the second iteration, case 2 is processed. The break statement causes the switch statement to complete, and control passes to the boolean expression of the do-loop. On the third iteration, case 3 is processed. The break with label statement, "break label2;", completes abruptly; and the do-loop completes abruptly without processing the loop expression, ++j<5, so j is not incremented. On the fourth iteration, case 4 is processed. The break with label statement, "break label1;", completes abruptly, and the for-loop completes abruptly.  
21Prints: 1,3,2  Suppose the print statement, System.out.print("("+h+","+i+","+j+","+k+")");, is inserted before the switch statement. The output of the program would be as follows: (1,1,0,2)(1,2,1,4)(1,3,2,6)1,3,2. Three iterations of the loop are executed. On the first iteration, the switch expression, k, matches the case constant, 2. The subsequent break statement is executed and the switch statement completes normally. On the second iteration, the switch expression matches the case constant, 4; and the subsequent continue statement causes control to pass to the loop-continuation point of the do statement where the expression, ++j < 5, is evaluated and found to be true. On the final iteration, the switch expression does not match any case constant; so the break statement following the default label is processed.  
 
Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 

Copyright © 2002-2004, Dan Chisholm
All rights reserved.