The answer is E because the code fragment contains several syntax errors that prevent it from compiling. Some of the errors are:
The enum declaration is missing a semicolon after the list of constants.
The enum constants are not capitalized, which violates the Java naming convention for enums.
The switch expression is missing parentheses around the variable name.
The case labels are missing colons after the enum constants.
The default label is missing a break statement, which causes a fall-through to the next case.
The println statement is missing a closing parenthesis and a semicolon.
A possible corrected version of the code fragment is:
enum Vehicle { BICYCLE, CAR, MOTORCYCLE, TRUCK; } public class Test { public static void main(String[] args) { Vehicle v = Vehicle.BICYCLE; switch (v) { case BICYCLE: System.out.print(“1”); break; case CAR: System.out.print(“3”); break; case MOTORCYCLE: System.out.print(“1”); break; case TRUCK: System.out.print(“2”); break; default: System.out.print(“0”); break; } System.out.println(); } }
This would print 1 as the output. References:
Oracle Certified Professional: Java SE 17 Developer
Java SE 17 Developer
OCP Oracle Certified Professional Java SE 17 Developer Study Guide
Enum Types
The switch Statement
Submit