Skip to content
Sharp

Language Upgrades · interview question

Consider a sealed class hierarchy where you have a base class Order and two subclasses, OnlineOrder and InStoreOrder. If you add a new subclass, PhoneOrder, what change is necessary in a switch expression handling Order?

Multiple choiceHardchapter-12-curatedpremiumlanguage-upgradespattern-matching
Choose one answer
Answer and explanation

Correct answer

D. Update the switch expression to handle PhoneOrder to ensure exhaustiveness.

Why

In a sealed class hierarchy, adding a new subclass requires updating the switch expression to explicitly handle the new subclass, ensuring that the code remains exhaustive and no cases are overlooked.

Test yourself on Language Upgrades

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

Start free test
All Language Upgrades questions →