Answer is c.
a. Prints: 13
b. Prints: 4
c. Compile-time error at 1
d. Compile-time error at 2
e. Run-time error
f. None of the above
([C0DE][/C0DE] tags added)
[ August 05, 2004: Message edited by: Barry Gaunt ]
If you don't, it might cause a loss of precision and that results in a compile-time error.
hmm... i just reconsidered, after i saw the function at line 2...
Are you sure 'c' is the right answer?
I would say 'd' does not compile, for the reason I stated in my previous answer.
'c' should compile, as it is defined at compile-time as a constant. The compiler implicitly casts constants to the smallest possible primitive.
'd' is also final, however it is not yet defined, so the compiler has to reserve 2 bytes.
Thank you for using my exam.
No�l is correct. The answer provided by the exam is d. The explanation provided with that answer is as follows.
There is a compile-time error at 2. The char type variable c2 is not a compile-time constant, so it can not be assigned to type byte without an explicit cast. The method parameter c2 is declared final, so the value of c2 can not be changed within method m2. The value of method parameter c2 is set at run time to the value of the argument that is provided when m2 is invoked at line 3. For that reason, the method parameter c2 is not a compile-time constant. In method m2, the statement, "return c2;", is a return statement with an expression, c2. 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 method m2 is byte. The return statement attempts to return the value of the char type variable c2. 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 m2, variable c2 is not a compile-time constant, because the value of c2 is not known at compile time. Instead, the value of c2 is assigned at run time to the value of the argument. Since the char type variable c2 is not a compile-time constant, the value of variable c2 is not assignable to the return type of method m2 without an explicit cast. While the declaration of method m2 produces a compile-time error, the declaration of method m1 does not. The local variable c1 is declared final and the value is set at compile time; so c1 is a compile-time constant. The value \u0001 falls within the range of type byte; so the value of the compile-time constant c1 is assignable to the return type of method m1 without an explicit cast.