Java Exam Conversions
Questions
Q001
class A { public static void main(String[] args) { char a = 'a', // 'a' = 97, b = 'b'; // '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
|
Q002
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
|
Q003
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
|
Q004
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
|
Q005
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
|
Q006
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
|
Q007
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
|
Q008
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
|
Q009
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
|
Q010
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
|
Q011
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
|
Q012
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
|
Q013
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
|
Q014
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
|
Q015
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
|
Q016
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
|
Q017
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
|
Q018
class UltraViolet { public static void main (String[] args) { char a = '\u002a', // a = Asterisk * b = '\u0024'; // 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
|
Q019
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
|
Answers
A001
Answer: Q001: C. Prints: 195ab
Remark:
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 result of the first addition operator is converted to type String, and is 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.
|
A002
Answer: Q002: C, D, Line 3 and 4
Remark:
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 the left and if the variable on the left is of type byte, short, or char. In 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.
|
A003
Answer: Q003: D, Prints: 127 -128 -1 0
Remark:
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 significant bit of the remaining byte becomes the new sign bit.
byte a = (byte)127; // 01111111.
byte b = (byte)128; // 10000000.
byte c = (byte)255; // 11111111.
byte d = (byte)256; // 00000000.
|
A004
Answer: Q004: E, None of the above
Remark:
Line 4 does not generate a compile-time error. The reference named base actually refers to an instance of type Sub, so the reference may be cast to type Sub.
|
A005
Answer: Q005: G, None of the above
Remark:
The null literal is converted to an int array type with the value null. All array types implement the Cloneable interface, so any array reference can be assigned to a reference of type Cloneable. The int array object referenced by the Cloneable reference, c, can be assigned to a reference of the int array type, int[].
|
A006
Answer: Q006: C, 3
Remark:
Short is signed and char is not signed so an explicit cast is necessary when a short is assigned to a char and vice versa.
|
A007
Answer: Q007: D, Run-time error at line 2
Remark:
The compiler accepts the explicit cast at line 2, but an error is generated at run-time. Type Base is the super class of type Sub, so an instance of type Base can not be converted to the type of the subclass, Sub.
|
A008
Answer: Q008: E, Compile-time error at line 3
Remark
Although the referenced object is indeed an array of type int, an explicit cast is necessary to cast the obj reference to an int array.
|
A009
Answer: Q009: D, Line 4
Remark:
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.
|
A010
Answer: Q010: E, None of the above
Remark:
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.
|
A011
Answer: Q011: G, None of the above
Remark:
All array types implement the Serializable interface and may be assigned to a reference of type Serializable.
|
A012
Answer: Q012: D, None of the above
Remark:
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.
|
== A013 --
Answer: Q013: D, line 4
Remark:
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.
|
A014
Answer: Q014: C, Line 3
Remark:
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
|
A015
Answer: Q015: A
Remark
The literal, 'a', is promoted to type int; and is then multiplied by the value of the left operand, 4.
If one of the two operands of a 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.
|
A016
Answer: Q016: F, Run-time error at line 3
Remark:
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 reference to an array instance of type Base[] can not be cast to type Sub[].
The type of the reference, obj, is Object.
Type Sub[] is a subclass of Object.
The compiler accepts the cast, because the actual 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.
|
A017
Answer: Q017: D, Line 4
Remark:
An array of primitive type can not be cast to an array of a different primitive type.
|
A018
Answer: Q018: A
Prints: 78 ABC*$
Remark:
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 operand of the expression, " ABC" + a + b, is of type String.
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*$"
|
A019
Answer: Q019: F, None of the above
Remark:
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 of int primitives is converted to a single dimensional array with
components of type Object where each Object is a single 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.
|
See also
Reference