QuizzesSealed Classes — Concept Check
intermediate
java

Sealed Classes — Concept Check

Questions on Sealed Classes. Sealed classes restrict which classes can extend or implement them, enabling exhaustive pattern matching and closed hierarchies.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on Sealed Classes — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.You have a sealed class 'Vehicle' with permitted subtypes 'Car' and 'Bike'. What happens if you add a new subtype 'Truck' without updating switch expressions handling 'Vehicle'?

    • AThe switch will automatically handle the new subtype.
    • BThe compiler will throw an error for non-exhaustive switch.
    • CThe switch will default to a generic case.
    • DThe code will compile but may throw runtime exceptions.
  2. 2.In Java 17, you have a sealed interface 'Response' with subtypes 'Success' and 'Failure'. What is a potential issue when you add a new subtype 'Pending'?

    • AExisting code will handle 'Pending' automatically.
    • BExisting code will break at runtime without warnings.
    • CThe compiler will enforce handling 'Pending' in all switch statements.
    • D'Pending' cannot be added to a sealed interface.
  3. 3.Given a sealed class 'Animal' with a non-sealed subtype 'Dog', what can be inferred about 'Dog'?

    • A'Dog' can have any number of subtypes.
    • B'Dog' cannot have any subtypes.
    • C'Dog' must be final or sealed.
    • D'Dog' must be in a different package.
  4. 4.You have a sealed class 'Payment' with subtypes 'CreditCard' and 'Cash'. What is the benefit of using a sealed class here?

    • AIt allows dynamic addition of new payment methods.
    • BIt ensures exhaustive handling in switch expressions.
    • CIt prevents any subclassing of 'Payment'.
    • DIt automatically optimizes performance.
  5. 5.In a sealed class hierarchy, what must each permitted subtype declare itself as?

    • AOnly sealed
    • BOnly final
    • CFinal, sealed, or non-sealed
    • DOnly non-sealed
  6. 6.A sealed interface 'Command' has subtypes 'Start' and 'Stop'. What happens if you try to implement 'Command' in another package?

    • AThe compiler will throw an error.
    • BIt will require a special annotation.
    • CIt will compile successfully.
    • DIt will compile but fail at runtime.
  7. 7.Why might you choose to use a sealed class over a traditional class hierarchy?

    • ATo allow unlimited subclassing.
    • BTo improve runtime performance.
    • CTo restrict subclassing and enable exhaustive pattern matching.
    • DTo automatically generate constructors.
  8. 8.What is a key advantage of using sealed classes with records in Java?

    • AThey enhance garbage collection efficiency.
    • BThey simplify modeling algebraic data types.
    • CThey automatically handle exceptions.
    • DThey allow dynamic method dispatch.