Pattern Matching with Switch Quiz Quiz

Explore core ideas and practical scenarios related to pattern matching with the switch statement. Enhance your understanding of syntax, matching rules, ordering, types, and guard clauses for efficient code branching.

  1. Pattern Matching Syntax

    Which of the following is a valid way to use a type pattern in a switch statement to check if a variable 'obj' is an integer?

    1. case int i:
    2. case int = i:
    3. case typeof int:
    4. case integer:

    Explanation: The correct pattern syntax for type matching binds 'obj' to a new variable 'i' if it is of type int, using 'case int i:'. The option 'case integer:' is incorrect because the keyword is 'int', not 'integer'. 'case typeof int:' is invalid, as 'typeof' is not a valid pattern matching keyword here. 'case int = i:' is not valid syntax for a switch pattern match.

  2. Switch Pattern Matching Order

    Why is the order of case labels important when using pattern matching in a switch statement with multiple patterns?

    1. Because the first matching case is executed, and later specific cases may never be reached if a broader pattern comes first.
    2. Because the order determines variable naming in each case.
    3. Because switch statements require cases to be sorted alphabetically for pattern matching.
    4. Because the pattern matching order affects the types the switch can handle at all.

    Explanation: In switch pattern matching, the first case that matches is executed, so placing a more general pattern before specific ones can make the specific patterns unreachable. The order does not influence how variables are named (option B), nor does it affect the types handled (option C). Alphabetical sorting (option D) is neither required nor relevant for pattern matching cases.

  3. Type and Value Patterns

    Given a variable 'x', which switch case pattern correctly matches x only if it is an integer with a value of 10?

    1. case 10:
    2. case int x when x == 10:
    3. case x == 10:
    4. case integer 10:

    Explanation: The pattern 'case 10:' directly matches against the constant value 10, which works if x is an integer with the value 10. 'case int x when x == 10:' uses a type pattern combined with a guard clause, but it is redundant if only 10 is to be matched. 'case x == 10:' is not valid switch syntax. 'case integer 10:' uses an invalid type name and pattern form.

  4. Use of Guard Clauses

    What is the role of a 'when' clause in a switch case using pattern matching?

    1. It changes the matching type at runtime.
    2. It adds an additional boolean condition that must be true for the case to match.
    3. It specifies a default case if no patterns match.
    4. It renames the case variable for clarity.

    Explanation: A 'when' clause appends a guard condition, so the case matches only if both the pattern and the 'when' expression are true. Renaming a variable (option B) is not what 'when' does. The default case (option C) is specified differently, usually with 'default'. The 'when' clause has no effect on matching type at runtime (option D).

  5. Handling Null Values

    When using pattern matching in a switch statement, how can you specifically handle a scenario where the matched variable is null?

    1. By omitting a case, since switch handles null automatically.
    2. By using 'case null:' as one of the case labels.
    3. By adding 'case default:' to cover null values.
    4. By using 'case None:' to match null.

    Explanation: To specifically match null, you use 'case null:' in your switch statement. 'case default:' may act as a catch-all but does not specifically check for null before other patterns. 'case None:' is not valid syntax for matching null in most mainstream languages. Omitting a case does not guarantee that null will be handled distinctly; unhandled null values may cause an exception or fall through.