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.
| No. | Answer | Remark | |
|---|---|---|---|
| 1 | f | Compile-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. |
| 2 | d | Prints: 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. |
| 3 | a d e | 1 4 5 | The minimum value of type byte is -128. The maximum value of type short is 32767. The minimum value of type char is 0. |
| 4 | b | Prints: 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 | d | 4 | 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. |
| 6 | e | None 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. |
| 7 | g | None of the above | All array types implement the Serializable interface and may be assigned to a reference of type Serializable. |
| 8 | e | Compile-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). |
| 9 | a | Prints: -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. |
| 10 | d | None 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 | d | 4 | 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 | c | 3 | 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. |
| 13 | b | Prints: 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). |
| 14 | a | Prints: Dog,Cat | The method m1 is invoked by the method invocation expression m1(pet1,pet2). A copy of the reference argument pet1 is assigned to the method parameter pet1. Inside the body of method m1, the assignment expression pet1 = new GFC303("Fish") assigns a reference to a new instance of GFC303 to the method parameter pet1; but the argument pet1 that appears in the method invocation expression m1(pet1,pet2) and the local variable pet1 that is declared in the main method remain unchanged. The method invocation expression m1(pet1,pet2) has a second argument pet2, and a copy of pet2 is assigned to the method parameter pet2. Inside of method m1, the assignment expression pet2 = null changes the value of the method parameter pet2; but the argument pet2 appearing in the method invocation expression remains unchanged in the main method. |
| 15 | j | Compile-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. |
| 16 | b | Prints: 1,3 | The method m1 is invoked by the method invocation expression m1(i1, i2). The argument i1 denotes a local variable of type int[] that is declared in the main method. The value of the argument is a reference to the array, and the argument value is used to initialize the method parameter i1 of method m1. Inside the body of m1, the expression i1 = i2 sets the value of parameter i1 to the value of parameter i2, but the change in the value of the parameter i1 does not change the original argument value or the local variable i1 of the main method that the argument denotes. Similarly, the assignment expression i2 = i3 in method m1 does not change the value of the local variable i1 declared in the main method. |
| 17 | c | Prints: 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. |
| 18 | b | Prints: 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. |
| 19 | c | Prints: -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. |