This question is from danchisholm mock
test.
Question 14
class F {
public static void main (
String[] args) {
Float f1 = new Float('B' - 'A'); // 1
Float f2 = new Float(010); // 2
Float f3 = new Float(0x10); // 3
Float f4 = new Float(.1e1); // 4
Float f5 = new Float(1.0); // 5
Float f6 = new Float(1.0d); // 6
System.out.print(f1+","+f2+","+f3+","+f4+","+f5+","+f6);
}}
What is the result of attempting to compile and run the program?
a. Compile-time error at 1
b. Compile-time error at 2
c. Compile-time error at 3
d. Compile-time error at 4
e. Compile-time error at 5
f. Compile-time error at 6
g. Run-time error at 1
h. Run-time error at 2
i. Run-time error at 3
j. Run-time error at 4
k. Run-time error at 5
l. Run-time error at 6
m. Prints: 1.0,10.0,10.0,1.0,1.0,1.0
n. Prints: 1.0,8.0,16.0,1.0,1.0,1.0
o. None of the above
Answer is n. -- Reason:The Float constructor is overloaded: one version accepts a primitive of type float; one accepts a primitive of type double; one accepts a String representation of a floating-point literal. All numeric values can be promoted to type double; so all numeric values are accepted by the float constructor.
But in K&B
book , P-377, Wrapper Constructors states that :
Float Wrapper Class provides 2 constructor arguments float or String.
B]Then what about double?[/B]