17.8k views
1 vote
A program contains a switch statement that lets you test the value of an expression and, depending on that value, to jump directly to some location within the switch statement. When compiling the program, when will a Java compiler report an error?

Select one:
a. If the value of the expression is a char, or a short.
b. If the value of the expression is a String or a real number.
c. If the value of the expression is an int or byte.
d. If the value of the expression is an enum.

User JagaSrik
by
7.6k points

1 Answer

5 votes

Final answer:

A Java compiler will report an error with a switch statement if the expression value is a real number or any unsupported type. The switch supports byte, short, char, int, their wrapper classes, enums, and Strings, but not real numbers like float or double.

Step-by-step explanation:

When compiling a Java program, the Java compiler will report an error with a switch statement if the value of the expression is a real number or not supported by the switch. The correct answer to when a Java compiler will report an error is: If the value of the expression is a String or a real number.

The switch statement in Java supports several data types for its expression, including byte, short, char, int, their respective wrapper classes, enum types, and as of Java 7, String.

However, switch does not support real numbers (e.g., float or double), because real numbers are not discrete and do not match the requirement of having a limited number of identifiable values precise enough for case labels. So option b is the correct one, and whenever there's an attempt to use a real number or an unsupported type in a switch expression, the Java compiler will report an error.

User Trinh Hieu
by
8.3k points