You are on page 1of 13

Conversions

Question 1
class A {
public static void main(String[] args) {
char a = 'a', b = 'b'; // 'a' = 97, 'b' = 98
System.out.print(a + b + "" + a + b);
}}

What is the result of attempting to compile and run the program?

a. Prints: 390
b. Prints: 195195
c. Prints: 195ab
d. Prints: ab195
e. Prints: abab
f. Run-time error
g. Compile-time error
h. None of the above

ANSWER
Both operands of the first addition operator are promoted from type
char to int, and are evaluated as integral numeric values. The right
hand operand of the second addition operator is of type String, so the
Prints:
1 c result of the first addition operator is converted to type String, and is
195ab
concatenated with the right hand operand. As evaluation of the
expression continues from left to right, the remaining operands are also
converted to type String.

Question 2
class Black {
public static void main(String[] args) {
short s1 = 1; //1
char c1 = 1; //2
byte b1 = s1; //3
byte b2 = c1; //4
final short s2 = 1; //5
final char c2 = 1; //6
byte b3 = s2; //7
byte b4 = c2; //8
}}
Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8

ANSWER
This question demonstrates a variety of assignment conversions. The
compiler will implicitly do a narrowing conversion for an assignment
statement if the right hand operand is a compile time constant of type byte,
short, char, or int and the value falls within the range of the variable on
c 3
2 the left and if the variable on the left is of type byte, short, or char. In
d 4
this case, variables s1 and c1 are not compile time constants so the compiler
will not do an implicit narrowing conversion. However, variables s2 and c2
are compile time constants that fall within the range of the left hand operand.
For more information, please see JLS section 5.2.

Question 3
class Magenta {
static byte a = (byte)127, b = (byte)128, c = (byte)255, d =
(byte)256;
public static void main(String args[]) {
System.out.print(a + " " + b + " " + c + " " + d);
}}

What is the result of attempting to compile and run the program?

a. Prints: 127 128 255 256


b. Prints: 127 128 255 0
c. Prints: 127 -1 -127 0
d. Prints: 127 -128 -1 0
e. Run-time error
f. Compile-time error
g. None of the above

ANSWER
3 d Prints: Bytes are stored as 8 bit two's complement signed integers. When an
int primitive is cast to a byte, the three most significant bytes are
discarded and only the least significant byte remains. The most
127 -128
significant bit of the remaining byte becomes the new sign bit. byte a =
-1 0
(byte)127; // 01111111. byte b = (byte)128; // 10000000. byte c =
(byte)255; // 11111111. byte d = (byte)256; // 00000000.

Question 4
interface I1 {} interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Red {
public static void main(String args[]) {
Sub s1 = new Sub();
I2 i2 = s1; // 1
I1 i1 = s1; // 2
Base base = s1; // 3
Sub s2 = (Sub)base; // 4
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above

ANSWER
Line 4 does not generate a compile-time error. The reference named
None of the
4 e base actually refers to an instance of type Sub, so the reference
above
may be cast to type Sub.

Question 5
class Green {
public static void main (String args[]) {
int[] i = null; // 1
Cloneable c = i; // 2
i = (int [])c; // 3
}}

What is the result of attempting to compile and run the program?


a. Compile-time error at line 1.
b. Run-time error at line 1.
c. Compile-time error at line 2.
d. Run-time error at line 2.
e. Compile-time error at line 3.
f. Run-time error at line 3.
g. None of the above

ANSWER
The null literal is converted to an int array type with the value
None of null. All array types implement the Cloneable interface, so any
5 g the array reference can be assigned to a reference of type Cloneable.
above The int array object referenced by the Cloneable reference, c, can
be assigned to a reference of the int array type, int[].

Question 6
class Primitives {
static void printFloat(float f) {System.out.println(f);}
static void printDouble(double d) {System.out.println(d);}
public static void main(String[] args) {
byte b = 1; // 1
short s = b; // 2
char c = s; // 3
int i = c; // 4
long l = i; // 5
float f = l; // 6
printFloat(i); // 7
printFloat(l); // 8
printDouble(l); // 9
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
i. 9
j. None of the above
ANSWER
Short is signed and char is not signed so an explicit cast is necessary when
6 c 3
a short is assigned to a char and vice versa.

Question 7
interface I1 {} interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Orange {
public static void main(String args[]) {
Base base = new Base();
I1 i1 = base; // 1
Sub sub = (Sub)base; // 2
}}

What is the result of attempting to compile and run the program?

a. Compile-time error at line 1


b. Run-time error at line 1
c. Compile-time error at line 2
d. Run-time error at line 2
e. None of the above

ANSWER
The compiler accepts the explicit cast at line 2, but an error is
Run-time generated at run-time. Type Base is the super class of type Sub, so
7 d error at
an instance of type Base can not be converted to the type of the
line 2
subclass, Sub.

Question 8
class Purple {
public static void main (String []args) {
int[] i = {1,2,3}; // 1
Object obj = i; // 2
i = obj; // 3
}}

What is the result of attempting to compile and run the program?

a. Compile-time error at line 1.


b. Run-time error at line 1.
c. Compile-time error at line 2.
d. Run-time error at line 2.
e. Compile-time error at line 3.
f. Run-time error at line 3.
g. None of the above

ANSWER
Although the referenced object is indeed an array of type int,
Compile-time
8 e an explicit cast is necessary to cast the obj reference to an
error at line 3.
int array.

Question 9
class Maroon {
public static void main (String[] args) {
int a = 1; // 1
short b = 1; // 2
long c = 1; // 3
a = c + a; // 4
c = b + a; // 5
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above

ANSWER
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
9 d 4
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.

Question 10
interface I1 {} interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Yellow {
public static void main(String args[]) {
Base base = new Sub(); // 1
I1 i1 = base; // 2
Sub sub = (Sub)base; // 3
I2 i2 = (Sub)base; // 4
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above

ANSWER
Although the reference named base is of type Base, the instance
None of that it refers to is of type Sub, and the reference can be cast to type
10 e the Sub. Since instances of type Sub implement both interfaces, I1 and
above I2, the Sub type instances can be assigned to references of type I1
and I2 without an explicit cast.

Question 11
import java.io.Serializable;
class Blue {
public static void main (String args[]) {
int[] i = {1,2,3}; // 1
Serializable s = i; // 2
i = (int [])s; // 3
}}

What is the result of attempting to compile and run the program?

a. Compile-time error at line 1.


b. Run-time error at line 1.
c. Compile-time error at line 2.
d. Run-time error at line 2.
e. Compile-time error at line 3.
f. Run-time error at line 3.
g. None of the above

ANSWER
11 g None of the All array types implement the Serializable interface and may
above be assigned to a reference of type Serializable.

Question 12
class Teal {
public static void main (String[] args) {
byte b = 1; // 1
long l = 1000; // 2
b += l; // 3
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. None of the above

ANSWER
The compound assignment operators include an implicit cast to the
None of type of the left hand operand. The expression at line 3, b += l,
12 d
the above does not require an explicit cast to convert the right hand operand
from type long to type byte.

Question 13
interface I1 {} interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Gray {
public static void main(String []args) {
Base[] base = {new Base()}; // 1
Sub sub[] = {new Sub()}; // 2
Object obj = sub; // 3
base = obj; // 4
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above
ANSWER
The Object referenced by obj is of type Sub[], and the reference, base,
13 d 4 is of type Base[]. The assignment expression, base = obj requires an
explicit cast to type Base[] as follows: base = (Base[])obj.

Question 14
class Black {
public static void main(String args[]) {
int[] i = {1,2,3,4,5}; // 1
long[] l = new long[5]; // 2
for (int j = 0; j < l.length(); j++) { // 3
l[j] = i[j]; // 4
}}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above

ANSWER
The length member of the array type is an attribute. A compile-time error
14 c 3 is generated as a result of the attempt to access length as though it were a
method.

Question 15
class Sienna {
static double a; static float b; static int c; static char d;
public static void main(String[] args) {
a = b = c = d = 'a';
System.out.println(a+b+c+d == 4 * 'a');
}}

What is the result of attempting to compile and run the program?

a. Prints: true
b. Prints: false
c. Compile-time error
d. Run-time error
e. None of the above

ANSWER
The literal, 'a', is promoted to type int; and is then multiplied by
Prints: the value of the left operand, 4. If one of the two operands of a
15 a
true numeric expression is of type int, then the other operand will be
promoted to type int if it is of type short, char or byte.

Question 16
interface I1 {} interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Silver {
public static void main(String []args) {
Base[] base = {new Base()};
Sub sub[] = new Sub[1]; // 1
Object obj = base; // 2
sub = (Sub[])obj; // 3
I1 []i1 = (I1[])obj; // 4
}}

What is the result of attempting to compile and run the program?

a. Compile-time error at line 1


b. Run-time error at line 1
c. Compile-time error at line 2
d. Run-time error at line 2
e. Compile-time error at line 3
f. Run-time error at line 3
g. Compile-time error at line 4
h. Run-time error at line 4
i. None of the above

ANSWER
Base is the superclass of type Sub, so a reference to an actual
instance of type Base can not be cast to type Sub. Therefore, a
Run- reference to an array instance of type Base[] can not be cast to type
time Sub[]. The type of the reference, obj, is Object. Type Sub[] is a
16 f
error at subclass of Object. The compiler accepts the cast, because the actual
line 3 instance referenced at run-time might be of type Sub[]. In this case,
the actual type of the instance referenced by obj at run-time is found
to be type Base[], so the result is a run-time error.
Question 17
class White {
public static void main(String args[]) {
int[] i = {1,2,3,4,5}; // 1
long[] l1 = new long[5]; // 2
long []l2 = l1; // 3
long l3[] = (long[])i; // 4
long l4[] = new long[5]; // 5
l4[1] = i[1]; // 6
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. None of the above

ANSWER
An array of primitive type can not be cast to an array of a different primitive
17 d 4
type.

Question 18
class UltraViolet {
public static void main (String[] args) {
char a = '\u002a', b = '\u0024'; // a = Asterisk *; b = Dollar Sign
$
System.out.print(a + b); // 1
System.out.print(" ABC" + a + b); // 2
}}

What is the result of attempting to compile and run the program?

a. Prints: 78 ABC*$
b. Prints: *$ ABC*$
c. Prints: 78 ABC78
d. Compile-time error
e. Run-time error
f. None of the above
ANSWER
When char variables a and b are converted to String types they
are printed as *$. When not converted to String types they are
promoted to type int, and are printed as the numeric sum of 0x2a
and 0x24. At line 1, the expression, a + b, can be evaluated as
follows: 0x2a + 0x24 = 42 + 36 = 78. At line 2, the first
Prints: 78
18 a operand of the expression, " ABC" + a + b, is of type String.
ABC*$
Since one operand of the first addition operator is of type String
the other operand must be converted to type String: (" ABC" +
"*") + b = " ABC*" + b. The process is repeated for the
second addition operation: " ABC*" + b = " ABC*" + "$" =
" ABC*$"

Question 19
class Amber {
public static void main(String[] args) {
int[][] a = {{1,2},{0,1,2},{-1,0,2}}; // 1
Object[] obj = (Object[])a.clone(); // 2
for(int i = 0; i < obj.length; i++) { // 3
int[] ia = (int[])obj[i]; // 4
System.out.print(ia[i]); // 5
}}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above

ANSWER
The program compiles and runs without error and prints 112. It is
necessary to remember that arrays are Cloneable objects.
Furthermore, a two dimensional array is also a single dimensional
array of single dimensional arrays. At line 2, a two dimensional array
None of of int primitives is converted to a single dimensional array with
19 f the
components of type Object where each Object is a single
above
dimensional array. The loop iterates through each element of the
Object array. Since each element is actually a reference to a single
dimensional array of integer primitives a conversion from type
Object to an int array is legal.

You might also like