| No. | Answer | Remark | |
|---|---|---|---|
| 1 | d | Compile-time error at 2 | There is a compile-time error at 2. The char type variable, c, is not a compile-time constant, so it can not be assigned to type byte without an explicit cast. The statement, "return c;", is a return statement with an expression, c. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of the method, m3, is byte. The return statement attempts to return the value of the char type variable, c. If a char value is a compile-time constant, and if the value falls within the range of type byte, then the char value is assignable to type byte. In method m3, variable c is not a compile-time constant, so the value of variable c is not assignable to type byte. While the declaration of method m3 produces a compile-time error, the declaration of method m1 does not; because the variable is a compile-time constant with a value, \u0001, that is assignable to type byte. |
| 2 | d | Compile-time error at 2. | There is a compile-time error at 2. The short type variable, s, is not a compile-time constant, so it can not be assigned to type byte without an explicit cast. The statement, "return s;", is a return statement with an expression, s. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of the method, m4, is byte. The return statement attempts to return the value of the short type variable, s. If a short value is a compile-time constant, and if the value falls within the range of type byte, then the short value is assignable to type byte. In method m4, variable s is not a compile-time constant, so the value of variable s is not assignable to type byte. While the declaration of method m4 produces a compile-time error, the declaration of method m2 does not; because the variable is a compile-time constant with a value, 2, that is assignable to type byte. |
| 3 | e | Compile-time error at 3. | There is a compile-time error at line 3. The long type variable, l, can not be assigned to type int without an explicit cast. The statement, "return l;", is a return statement with an expression, l. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of the method, m3, is int. The type of the variable, l, is long, so an explicit cast is needed to perform the narrowing primitive conversion, "return (int)l;". The declarations of methods m1 and m2 do not generate compile-time errors, because the types of the expressions contained in the return statements are assignable to type int. Widening conversions from types byte, char, or short to type int do not require an explicit cast. |
| 4 | d | Compile-time error at 2. | There is a compile-time error at 2, because a narrowing primitive conversion from type float to type int requires an explicit cast. There is no compile-time error at 1, because widening primitive conversions from types byte, char, or short to type int do not require an explicit cast. |
| 5 | d e f | Compile-time error at 2. Compile-time error at 3. Compile-time error at 4. | At line 2, the statement, "return i;", contains the expression, i. The enclosing method, m2, is declared void. The return statement generates a compile-time error, because it contains an expression. At line 3, the statement, "return;", does not contain an expression. The enclosing method, m3, is declared with the result type, int. The return statement generates a compile-time error, because it does not contain an expression that produces a value that is assignable to the declared result type. |