Java SE 7 Programmer I — Question 11
Which code fragment cause a compilation error?
Answer options
- A. flat flt = 100F;
- B. float flt = (float) 1_11.00;
- C. float flt = 100;
- D. double y1 = 203.22; floatflt = y1
- E. int y2 = 100;
Correct answer: B
Explanation
Option B causes a compilation error because the underscore in the number 1_11.00 is not valid syntax for a float literal. The other options are syntactically correct; A defines a variable with an incorrect type name, C assigns an integer to a float without casting, D has a typo in 'floatflt' but does not cause a compilation error.