Skip to content
Sharp

Language Upgrades · interview question

Given a sealed interface PaymentMethod with classes CreditCard and PayPal implementing it, what will happen if a new class BankTransfer also implements PaymentMethod without updating the switch statement handling PaymentMethod?

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

Correct answer

B. A compile-time error due to non-exhaustive switch.

Why

Sealed classes ensure exhaustiveness in switch statements. If a new subclass is added and not handled in the switch, a compile-time error occurs, prompting developers to update the switch statement to handle all possible subtypes.

Test yourself on Language Upgrades

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

Start free test
All Language Upgrades questions →