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
1Compile-time error  Method m1 is an instance method, and must be invoked with reference to an instance of type GFC402. Method m1 can not be invoked from a static context.  
2Prints: false,true,true  The right hand operand of the conditional or operator is evaluated only if the left hand operand is false. The right hand operand of the conditional and operator is only evaluated if the left hand operand is true. In this case, the left hand operand of the conditional or operator is false, so the right hand operand must also be evaluated. The left hand operand of the conditional and operator is the result of the conditional or expression, true, so the right hand operand is evaluated.  
3a  d  e  The minimum value of type byte is -128. The maximum value of type short is 32767. The minimum value of type char is 0.  
4Prints: 2,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 value of the variable y in the main method. However, method m1 does have direct access to the class variable x and the content of the class variable is modified by method m1.  
5 The assignment expression, a = c + a, requires an explicit cast to type int. If one of the two operands of a numeric expression is of type long and if the other operand is of type int, short, char or byte; then it will be promoted to type long, and the result of the expression will be of type long. (Note: The rule does not apply to the shift operator.) The type long result can not be assigned to a variable of type int without an explicit cast.  
6None of the above  Although the reference named base is of type Base, the instance that it refers to is of type Sub, and the reference can be cast to type Sub. Since instances of type Sub implement both interfaces, I1 and I2, the Sub type instances can be assigned to references of type I1 and I2 without an explicit cast.  
7None of the above  All array types implement the Serializable interface and may be assigned to a reference of type Serializable.  
8Compile-time error  The method invocation expression, m(b1), contains an argument of type double. A method invocation conversion will not implicitly narrow the argument to match the parameter type of the method, m(float i). The method invocation expression, m(a1), contains an argument of type long. A method invocation conversion will widen the argument to match the parameter type of the the method, m(float i).  
9Prints: -4  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. When a variable of type int with a value of 127 is shifted two bits to the left, the result is 508. The compound assignment operator includes an implicit cast to the type of the left-hand operand. The expression, E1 op= E2, is equivalent to E1=(T)((E1) op (E2)), where T is the type of the left hand operand. Therefore, when 508 is cast to an eight bit byte, the three most significant bytes (24 bits) are discarded leaving only the least significant byte (8 bits). The result is the binary value, 11111100, which is the two's complement representation of negative four.  
10None of the above  The compound assignment operators include an implicit cast to the type of the left hand operand. The expression at line 3, b += l, does not require an explicit cast to convert the right hand operand from type long to type byte.  
11 The Object referenced by obj is of type Sub[], and the reference, base, is of type Base[]. The assignment expression, base = obj requires an explicit cast to type Base[] as follows: base = (Base[])obj.  
12 The length member of the array type is an attribute. A compile-time error is generated as a result of the attempt to access length as though it were a method.  
13Prints: String  A method invocation conversion can widen an argument of type String to match a method parameter of type Object, so any argument that can be passed to m(String x) without generating a compile-time type error can also be passed to m(Object x). For that reason, we can say that m(String x) is more specific than m(Object x). The argument of the method invocation expression, m(null), is of type null and can be converted to either type String or Object by method invocation conversion, so both methods, m(String x) and m(Object x), are applicable. The more specific of the two, m(String x), is chosen over the less specific, m(Object x).  
14Prints: Dog,Cat  Although the reference parameters pet1 and pet2 are reassigned inside of m1, the change has no impact outside of m1. Object references are passed by value: the invoked method gets a copy of the object reference.  
15Compile-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.  
16Prints: 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.  
17Prints: BFCTAT  The right operand of the conditional or operator is evaluated only if the left hand operand is false. In this case, the left operand of the first conditional or operator is false so the right hand operand is evaluated. No further evaluation of the expression is necessary so the right hand operand of the second conditional or operator is not evaluated.  
18Prints: 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.  
19Prints: -2, 3, 0, 2, 2, 5  The expression can be simplified as follows: -2+3+0+2+2=5. The original expression is as follows: m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1)). Expr 1: ~1 = -1 - 1 = -2. Expr 2: 1|2 = 0001 | 0010 = 0011 = 3. Expr 3: 1&2 = 0001 & 0010 = 0000. Expr 4: 1^3 = 0001 ^ 0011 = 0010 = 2. Expr 5: 1<<1 = 0001 << 1 = 0010 = 2. Note: The bitwise expressions were demonstrated using only four bits of the 32 bit int type values.  

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