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 BookSurge.com.

Are you a university student studying Java programming? Do you agree that my book would serve as a helpful workbook and companion to be used along with the Java fundamentals textbook that is currently being used in your class? If so, then please ask your professor to consider using my book in future classes.

If you have any questions or comments concerning my mock exams or my book, then please send an e-mail to me at scjpexam2000@yahoo.com.

I would also like to read your response to the following questions.

Answers: Java Programmer Certification Mock Exam
No.AnswerRemark
1Prints: 2, 2, -3, 3, 4,  The expression can be simplified as follows: j = 2 + 2 + -3 + 3 = 4. The original expression is as follows: j = ++i + i++ + -i + i++. Simplification step one. Evaluate the unary expressions from left to right: j = 2 + 2 + -3 + 3. Step two. Complete the evaluation of the simplified expression: j = 4.  
2Prints: 1,2,3,4,1  The expression can be simplified as follows: j = 1 + (2 * 3) + 4 = 11. The original expression is as follows: j = m(i++) + m(i++) * m(i++) + m(i++). The method, m, prints and then returns the value of the parameter, so the original expression is equivalent to the following: j = i++ + i++ * i++ + i++. Step one. Work through the expression from left to right to evaluate the unary expressions: j = 1 + 2 * 3 + 4. Step two. Add parentheses to indicate operator precedence: j = 1 + (2 * 3) + 4. Step three. Work through the simplified expression: j = 1 + 6 + 4 = 11. Step four. Evaluate the expression that is the argument of the print method: j % 5 = 11 % 5 = 1.  
3Compile-time error.  The precedence of the cast operator is higher than the precedence of the addition operator, so the cast applies only to variable a and not to the result of the addition. Binary numeric promotion causes the byte variables a and b to be promoted to type int before the addition operation, and the result of the addition is also of type int. The attempt to assign the int result to the byte variable e generates a possible loss of precision error.  
4Prints: 2, 2, -3, -4, 12,  The original expression is as follows: m(m(++i) - m(i++) + m(-i) * m(~i)). The method, m, prints and then returns the value of the parameter, so the original expression is equivalent to the following: ++i - i++ + -i * ~i. We can use a simplification process that evaluates the expression as follows. Step one. Work through the expression from left to right to evaluate the unary expressions: 2 - 2 + -3 * -4. Step two. Add the parentheses to indicate operator precedence: 2 - 2 + (-3 * -4). Step three. Evaluate the inner-most parentheses: 2 - 2 + 12. Step four. Evalute the simplified expression to produce the result, 12. The bitwise complement operator, ~, inverts each bit of the operand. To avoid working in binary the same result can be obtained by changing the sign of the operand and then subtracting 1. The following identity is always true ~x == -x - 1.  
5Prints: 1,0  The expression, i = i++ + m(i), can be reduced to, i = 0 + 0. The left operand of the addition expression is found to be zero, and the value of variable i is then incremented to 1. The right hand operand of the addition operation is evaluated next. The value, 1, is passed to method m. After printing the argument value, method m returns the value zero. The two operands of the addition operation are zero as is the result of the addition. The zero value serves as the right hand operand of the assignment statement, so the value, zero, is assigned to variable i.  
6Prints: 5  The two statements, int a=1 followed by a += ++a + a++, can be rewritten as the single statement, a=(int)((1)+(++a + a++)). Further evaluation produces a=(int)((1)+(2 + 2)). Generally speaking, a compound assignment expression of the form E1 op= E2 can be rewritten as E1=(T)((E1)op(E2)) where T is the type of E1.  
7Prints: 1,2,3,4,5,3  The expression can be simplified as follows: j = 1 + ((2 * 3) % 4) + 5 = 8. The original expression is as follows: j = ++i + ++i * ++i % ++i + ++i. Step one. Evaluate the unary expressions from left to right: j = 1 + 2 * 3 % 4 + 5. Step two. Add parentheses to indicate operator precedence: j = 1 + ((2 * 3) % 4) + 5. Step three. Evaluate the inner most parentheses: j = 1 + (6 % 4) + 5. Repeat step three: j = 1 + 2 + 5. Repeat step three: j = 8. The argument of the print expression is: j%5. The result is: 8 % 5 = 3.  
8Prints: 3  The expression can be simplified as follows: j = 1 + ((2 * 3) % 4) + 5 = 8. The original expression is as follows: j = ++i + ((++i * ++i) % ++i) + ++i;. Step one. Evaluate the unary expressions from left to right: j = 1 + ((2 * 3) % 4) + 5. Step two. Evaluate the inner-most parentheses: j = 1 + (6 % 4) + 5. Step three: Evaluate the inner-most parentheses. j = 1 + 2 + 5. Step four: Work through the expression from left to right. j = 8. The argument of the print expression is: j%5. The result is: 8 % 5 = 3.  
 
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.