QuizzesPattern Matching — Concept Check
intermediate
java

Pattern Matching — Concept Check

Questions on Pattern Matching. Pattern matching eliminates verbose casting and enables expressive type-safe code with instanceof and switch patterns.

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 Pattern Matching — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.In Java 16+, what happens when you use pattern matching with instanceof to check if an object is a String?

    • AThe object is cast to String and a binding variable is declared inline.
    • BThe object is automatically converted to a String without any checks.
    • CPattern matching does not support String objects.
    • DThe object is cast to String only if it is null.
  2. 2.A developer uses pattern matching in a switch expression to handle different shapes. What will happen if a null shape is passed?

    • AThe switch will automatically skip null cases.
    • BNull values are ignored in switch expressions.
    • CThe switch will throw a NullPointerException.
    • DThe switch can handle null explicitly with a case null.
  3. 3.Why might a developer choose pattern matching over traditional instanceof checks in Java?

    • AIt increases the runtime performance significantly.
    • BIt automatically optimizes memory usage.
    • CIt allows pattern matching only for primitive types.
    • DIt reduces boilerplate by combining checks and casting.
  4. 4.In a switch expression using pattern matching, what does a guarded pattern allow you to do?

    • AUse instanceof checks within the switch expression.
    • BAutomatically handle null values without explicit cases.
    • CMatch multiple types in a single case.
    • DAdd a boolean condition to a case to refine the match.
  5. 5.A developer writes a switch expression with pattern matching for a sealed interface Shape. What is a potential issue if a new Shape implementation is added but not handled?

    • AThe switch will default to a null case automatically.
    • BThe switch will throw a runtime exception.
    • CThe code will not compile until all cases are handled.
    • DThe new implementation will be ignored silently.
  6. 6.A developer mistakenly believes that pattern matching for instanceof can be used with primitive types. What is the correct understanding?

    • APattern matching can be used with both primitive and reference types.
    • BPattern matching automatically converts primitives to their wrapper types.
    • CPattern matching for instanceof is only applicable to reference types.
    • DPattern matching requires explicit casting for primitive types.
  7. 7.What is the advantage of using pattern matching in switch expressions over if-instanceof-else chains?

    • AIt automatically optimizes the performance of the code.
    • BIt provides a more concise and readable way to dispatch over type hierarchies.
    • CIt eliminates the need for any type checks.
    • DIt allows handling of exceptions without try-catch blocks.
  8. 8.In Java 21, how can a switch expression handle a null input without throwing an exception?

    • ABy including a case null to handle null values explicitly.
    • BBy using a try-catch block around the switch expression.
    • CBy wrapping the input in an Optional before switching.
    • DBy using a default case that automatically catches null.