Given the application below and the choices available,
which lines must all be removed to allow the code to compile? (Choose three.)
1: package mypkg; 2: public class Main { 3: public static void main(String[] time) { 4: final long winter = 10; 5: final byte season = 2; 6: int fall = 4; 7: final short summer = 3; 8: switch(season) { 9: case 1: 10: case winter: System.out.print("winter"); 11: default: 12: case fall: System.out.print("fall"); 13: case summer: System.out.print("summer"); 14: default: 15: } /* w ww. j a v a 2 s . c o m*/ 16: } 17: }
C, D, E.
The value of a case statement must be a literal expression or a final constant variable, and have a compatible data type.
Lines 10 and 12 do not compile, making Options C and E correct answers.
Line 10 uses a constant value, but long is not compatible with switch statements.
Line 12 uses a variable that is not marked final.
Next, a switch statement may only have one default block.
Line 11 or 14 must be removed.
Since Line 14 is not in the list of options, Option D becomes the last correct answer.
The rest of the lines are fine since removing Lines 10, 11, and 12 allows the code to compile.