Question 1
class JSC201 {
static byte m1() {
final char c1 = '\u0001';
return c1; // 1
}
static byte m2(final char c2) {return c2;} // 2
public static void main(String[] args) {
char c3 = '\u0003';
System.out.print(""+m1()+m2(c3)); // 3
}}What is the result of attempting to compile and run the program?
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
Question 2
class JSC202 {
static byte m1() {final short s1 = 2; return s1;} // 1
static byte m2(final short s2) {return s2;} // 2
public static void main(String[] args) {
short s3 = 4;
System.out.print(""+m1()+m2(s3)); // 3
}}What is the result of attempting to compile and run the program?
a. Prints: 24
b. Prints: 6
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Run-time error
f. None of the above
Question 3
class JSC203 {
static int m1(byte b) {return b;} // 1
static int m2(char c) {return c;} // 2
static int m3(long l) {return l;} // 3
public static void main(String[] args) {
byte b = 1; char c = '\u0002'; long l = 4L;
System.out.print(""+m1(b)+m2(c)+m3(l));
}}What is the result of attempting to compile and run the program?
a. Prints: 124
b. Prints: 7
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Run-time error
Question 4
class JSC204 {
static int m1(short s) {return s;} // 1
static int m2(float f) {return f;} // 2
public static void main(String[] args) {
short s = 3; float f = 5.0f;
System.out.print(""+m1(s)+m2(f));
}}What is the result of attempting to compile and run the program?
a. Prints: 35.0
b. Prints: 8.0
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Run-time error
f. None of the above
Question 5
class JSC205 {
static int m1(int i) {return i;} // 1
static void m2(int i) {return i;} // 2
static int m3(int i) {return;} // 3
public static void main(String[] args) {
System.out.print(""+m1(1)+m2(2)+m3(3)); // 4
}}What is the result of attempting to compile and run the program?
a. Prints: 123
b. Prints: 6
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
No.
|
Answer
|
Remark
|
|
1
|
d
|
Compile-time error at 2
|
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.
|
2
|
d
|
Compile-time error at 2.
|
There is a compile-time error at 2. The short type variable s2 is not a compile-time constant, so it
can not be assigned to type byte
without an explicit cast. The method parameter s2
is declared final, so the value of s2
can not be changed within method m2.
The value of method parameter s2 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 s2 is not a
compile-time constant. In method m2,
the statement, "return s2;",
is a return statement with an
expression, s2. 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 short type
variable s2. 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
without an explicit cast. In method m2,
variable s2 is not a compile-time
constant, because the value of s2 is
not known at compile time. Instead, the value of s2 is assigned at run time to the value of the argument.
Since the short type variable s2 is not a compile-time constant, the
value of variable s2 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 s1 is declared
final and the value is set at compile time; so s1
is a compile-time constant. The value 2 falls within the range of type byte; so the value of the compile-time
constant s1 is assignable to the
return type of method m1 without an
explicit cast.
|
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.
|
|