Which code fragment causes a compilation error? 10/29/2018 – by Mod_GuideK 6 Which code fragment causes a compilation error? A. Option A B. Option B C. Option C D. Option D E. Option E SHOW ANSWERSCorrect Answer: D Explanation/Reference: Download Printable PDF. VALID exam to help you PASS.
D: # javac Test33.java Test33.java:10: error: possible loss of precision float flt = y1; ^ required: float found: double 1 error ———————– to compile without error its needed casting: float flt = (float) y1 Reply
Answer D: By default, any decimal point number is of type “double”. Therefore, whenever we assign a decimal number to a float variable, we either have to use “f” postfix, or to cast that number to (float). 7 Reply
public class Test { public static void main(String[] args) { float flt = 100F; float flt1 = (float) 1_11.00; float flt2 = 100; double y1 = 203.00; float flt3 = y1; //Type mismatch: cannot convert from double to float int y2 = 100; float flt4 = y2; } } 2 Reply
Option D
//Type mismatch: cannot convert from double to float
The old “narrowing cast without explicitly casting” trick
D:
# javac Test33.java
Test33.java:10: error: possible loss of precision
float flt = y1;
^
required: float
found: double
1 error
———————–
to compile without error its needed casting: float flt = (float) y1
Answer D:
By default, any decimal point number is of type “double”. Therefore, whenever we assign a decimal number to a float variable, we either have to use “f” postfix, or to cast that number to (float).
public class Test {
public static void main(String[] args) {
float flt = 100F;
float flt1 = (float) 1_11.00;
float flt2 = 100;
double y1 = 203.00;
float flt3 = y1; //Type mismatch: cannot convert from double to float
int y2 = 100;
float flt4 = y2;
}
}
D