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
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1Prints: 1,3  Variables of primitive type are passed to methods by value: Only a copy of the value of the variable is passed to the method. While the method works with a local copy of the variable, the original variable remains unchanged by any actions performed on the method parameter. For that reason, method m1 does not change the contents of the variable y in the main method or the class variable x.  
2Prints: true,false,true  The positive infinity of type float is promoted to the positive infinity of type double. NaN is not equal to anything including itself.  
3Prints: 195ab  Both operands of the first addition operator are promoted from type char to int, and are evaluated as integral numeric values. The right hand operand of the second addition operator is of type String, so the result of the first addition operator is converted to type String, and is concatenated with the right hand operand. As evaluation of the expression continues from left to right, the remaining operands are also converted to type String.  
4Prints: true,true  The sign of an integral numeric type is changed by inverting all of the bits and by adding one.  
5Prints: 10  If the left-hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to a 32 bit int and all four bytes are shifted. If the promoted type of the left-hand operand is of type int, then the shift distance is always within the range of 0 to 31, inclusive; and is specified by the least significant 5 bits of the right-hand operand. In this case, the shift distance is 33, and the five least significant bits are 00001; so the shift distance is one bit. Note: If the type of the left hand operand is long, then the least significant six bits of the right hand operand are used.  
6Prints: true  The literal, 'a', is promoted to type int; and is then multiplied by the value of the left operand, 4. If one of the two operands of a numeric expression is of type int, then the other operand will be promoted to type int if it is of type short, char or byte.  
7Run-time error at line 3  Base is the superclass of type Sub, so a reference to an actual instance of type Base can not be cast to type Sub. Therefore, a reference to an array instance of type Base[] can not be cast to type Sub[]. The type of the reference, obj, is Object. Type Sub[] is a subclass of Object. The compiler accepts the cast, because the actual instance referenced at run-time might be of type Sub[]. In this case, the actual type of the instance referenced by obj at run-time is found to be type Base[], so the result is a run-time error.  
8 An array of primitive type can not be cast to an array of a different primitive type.  
9Compile-time error  The type of the argument is null and could be converted to any of the types Object, String or GFC200, by method invocation conversion. All three methods are applicable, but none of the three is more specific than both of the other two. The ambiguity results in a compile-time type error. If type GFC200 were a subclass of type String; then any argument that could be pass to m(GFC200 x) could also be passed to m(String x) without causing a compile-time type error, and we could say that m(GFC200 x) is more specific than m(String x). Since GFC200 is not a subclass of type String, a method invocation conversion is not able to widen an argument of type GFC200 to match a method parameter of type String, so m(GFC200 x) is not more specific than m(String x).  
10Prints: 78 ABC*$  When char variables a and b are converted to String types they are printed as *$. When not converted to String types they are promoted to type int, and are printed as the numeric sum of 0x2a and 0x24. At line 1, the expression, a + b, can be evaluated as follows: 0x2a + 0x24 = 42 + 36 = 78. At line 2, the first operand of the expression, " ABC" + a + b, is of type String. Since one operand of the first addition operator is of type String the other operand must be converted to type String: (" ABC" + "*") + b = " ABC*" + b. The process is repeated for the second addition operation: " ABC*" + b = " ABC*" + "$" = " ABC*$"  
11Prints: GFC203  The type of the argument is null and could be converted to either type GFC202 or GFC203 by method invocation conversion; so both methods are applicable. The more specific of the two, m(GFC203 x), is chosen. Type GFC203 is a subclass of type GFC202; so any argument that can be passed to m(GFC203 x) can also be passed to method m(GFC202 x) without causing a compile-time type error; therefore, we can say that method m(GFC203 x) is more specific than m(GFC202 x).  
12Prints: 3,1  Method m1 is not able to change the value of the local variables that are declared in the main method and serve as the arguments in the method invocation expression. However, method m1 is able to modify the contents of the arrays that are referenced by the method arguments.  
13Prints: 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.  
14Prints: 1,1  Although the reference parameter i1 is reassigned inside of m1, the change has no impact outside of m1. Array references are passed by value: the invoked method gets a copy of the array reference.  
15Prints: 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.  
16None of the above  The program compiles and runs without error and prints 112. It is necessary to remember that arrays are Cloneable objects. Furthermore, a two dimensional array is also a single dimensional array of single dimensional arrays. At line 2, a two dimensional array of int primitives is converted to a single dimensional array with components of type Object where each Object is a single dimensional array. The loop iterates through each element of the Object array. Since each element is actually a reference to a single dimensional array of integer primitives a conversion from type Object to an int array is legal.  
17Prints: 1,3  Although the reference parameters i1 and i2 are reassigned inside of m1, the change has no impact outside of m1. Array references are passed by value: the invoked method gets a copy of the array reference.  
18Prints: 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.  

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