Skip to content
Sharp

Modern Java · interview question

When using switch expressions with enums in Java 17, what happens if you omit a case and there is no default?

Multiple choiceMediumchapter-12-curatedpremiummodern-javaswitch-expressions
Choose one answer
Answer and explanation

Correct answer

C. The code fails to compile due to non-exhaustiveness.

Why

Switch expressions with enums must cover all possible enum values, or include a default case. If neither is done, the code will not compile, ensuring all cases are handled explicitly.

Test yourself on Modern Java

A free test with instant, explained results. No account needed.

Start free test
All Modern Java questions →