Language Upgrades · interview question
Which scenario might lead to a compile-time error when using a sealed class with a switch expression?
Multiple choiceHardchapter-12-curatedpremiumlanguage-upgradespattern-matching
Answer and explanation
Correct answer
C. A new subclass is added but not handled.
Why
A compile-time error occurs if a new subclass is added to a sealed class and is not handled in the switch expression, as Java requires all possible subclasses to be accounted for to ensure exhaustiveness.
Test yourself on Language Upgrades
A free test with instant, explained results. No account needed.
More Language Upgrades questions
- Which of the following is a correct use of a switch expression in Java?
- How can sealed classes help in managing a complex class hierarchy in an e-commerce application?
- When using a switch expression with pattern matching, what is a potential benefit compared to using multiple if-else…
- How does pattern matching for instanceof improve code readability and maintainability?
- How does pattern matching in switch expressions enhance type safety?
- In Java, how does pattern matching for 'instanceof' improve code readability?