Skip to content
Sharp

Modern Java · interview question

How does a switch expression enhance type safety compared to traditional switch statements?

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

Correct answer

C. By enforcing exhaustiveness and allowing expressions to yield values

Why

Switch expressions enhance type safety by requiring all possible cases to be handled (either directly or through a default case) and by allowing cases to yield values directly, enforcing consistency in return types.

Test yourself on Modern Java

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

Start free test
All Modern Java questions →