Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Switch expression (modern C#) - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return the string "One" when number is 1 using a switch expression.

C Sharp (C#)
string result = number switch [1];
// number is an int variable
Drag options to blanks, or click blank then click option'
A{ 1 => "One", _ => "Other" }
B( 1 => "One", _ => "Other" )
C[ 1 => "One", _ => "Other" ]
D< 1 => "One", _ => "Other" >
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or brackets instead of curly braces for switch expression.
Forgetting the default case with underscore (_).
2fill in blank
medium

Complete the switch expression to return "Even" if the number is divisible by 2, otherwise "Odd".

C Sharp (C#)
string parity = number switch
{
    [1] => "Even",
    _ => "Odd"
};
Drag options to blanks, or click blank then click option'
Avar n when n % 2 == 0
Bvar n if n % 2 == 0
Cvar n where n % 2 == 0
Dvar n in n % 2 == 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'where' or 'if' instead of 'when' in the pattern.
Incorrect syntax causing compile errors.
3fill in blank
hard

Fix the error in the switch expression to correctly return the day type for a given day string.

C Sharp (C#)
string dayType = day switch
{
    "Saturday" or [1] => "Weekend",
    _ => "Weekday"
};
Drag options to blanks, or click blank then click option'
Asunday
BSunday
C'Sunday'
D"Sunday"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes for strings.
Using unquoted identifiers instead of string literals.
4fill in blank
hard

Fill both blanks to complete the switch expression that returns a message based on the score.

C Sharp (C#)
string message = score switch
{
    [1] when score >= 90 => "Excellent",
    [2] when score >= 60 => "Pass",
    _ => "Fail"
};
Drag options to blanks, or click blank then click option'
Avar s
Bvar x
Cvar score
Dvar result
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for both patterns causing confusion.
Using invalid variable names or missing 'var'.
5fill in blank
hard

Fill all three blanks to create a switch expression that returns a grade letter based on the numeric score.

C Sharp (C#)
char grade = score switch
{
    [1] when score >= 90 => 'A',
    [2] when score >= 80 => 'B',
    [3] when score >= 70 => 'C',
    _ => 'F'
};
Drag options to blanks, or click blank then click option'
Avar s
Bvar x
Cvar y
Dvar z
Attempts:
3 left
💡 Hint
Common Mistakes
Reusing the same variable name in multiple patterns.
Omitting 'var' keyword.

Practice

(1/5)
1. What is the purpose of the _ symbol in a modern C# switch expression?
easy
A. It declares a new variable inside the switch.
B. It marks the end of the switch expression.
C. It acts as a default case to catch all unmatched values.
D. It is used to separate multiple cases.

Solution

  1. Step 1: Understand the role of _ in switch expressions

    The underscore _ is used as a discard pattern that matches any value not matched by previous cases.
  2. Step 2: Recognize it as the default case

    This means it acts like a default or fallback case to handle all other inputs.
  3. Final Answer:

    It acts as a default case to catch all unmatched values. -> Option C
  4. Quick Check:

    _ = default case [OK]
Hint: Remember '_' means 'anything else' or default [OK]
Common Mistakes:
  • Thinking '_' declares a variable
  • Confusing '_' with case separator
  • Assuming '_' ends the switch
2. Which of the following is the correct syntax for a simple switch expression in C# that returns "Even" for 2 and 4, and "Odd" for 1 and 3?
easy
A. var result = switch(number) { 2: "Even", 4: "Even", 1 => "Odd", 3 => "Odd" };
B. var result = number switch { 2 => "Even", 4 => "Even", 1 => "Odd", 3 => "Odd" };
C. var result = number switch (2 => "Even", 4 => "Even", 1 => "Odd", 3 => "Odd");
D. var result = number switch { case 2: "Even"; case 4: "Even"; case 1: "Odd"; case 3: "Odd"; };

Solution

  1. Step 1: Identify correct switch expression syntax

    In modern C#, switch expressions use the syntax: variable switch { pattern => result, ... }.
  2. Step 2: Compare options to syntax

    var result = number switch { 2 => "Even", 4 => "Even", 1 => "Odd", 3 => "Odd" }; matches this syntax exactly. Options B, C, and D use incorrect syntax like parentheses or keywords not used in switch expressions.
  3. Final Answer:

    var result = number switch { 2 => "Even", 4 => "Even", 1 => "Odd", 3 => "Odd" }; -> Option B
  4. Quick Check:

    Correct switch expression syntax = var result = number switch { 2 => "Even", 4 => "Even", 1 => "Odd", 3 => "Odd" }; [OK]
Hint: Switch expressions use curly braces and => for cases [OK]
Common Mistakes:
  • Using parentheses instead of braces
  • Using 'case' keyword inside switch expression
  • Using colons instead of =>
3. What is the output of the following C# code?
int day = 3;
string dayName = day switch
{
  1 => "Monday",
  2 => "Tuesday",
  3 => "Wednesday",
  _ => "Unknown"
};
Console.WriteLine(dayName);
medium
A. Wednesday
B. Monday
C. Unknown
D. Tuesday

Solution

  1. Step 1: Match the value of day in the switch expression

    The variable day is 3, so the switch expression looks for the case matching 3.
  2. Step 2: Find the corresponding string for case 3

    The case 3 returns "Wednesday". Since it matches, the default case _ is ignored.
  3. Final Answer:

    Wednesday -> Option A
  4. Quick Check:

    day=3 matches 3 => "Wednesday" [OK]
Hint: Match the variable value to the case pattern [OK]
Common Mistakes:
  • Choosing default case instead of matching case
  • Confusing case numbers with strings
  • Ignoring the switch expression syntax
4. Identify the error in this switch expression code snippet:
string result = value switch
{
  1 => "One",
  2 => "Two",
  3 => "Three",
  default => "Other"
};
medium
A. Using 'default' keyword instead of '_' for default case.
B. Switch expression must use parentheses, not braces.
C. Missing comma after case 3.
D. The variable 'value' is not declared.

Solution

  1. Step 1: Check syntax for default case in switch expressions

    Modern C# switch expressions use _ as the default case, not the keyword default.
  2. Step 2: Identify the incorrect use of 'default'

    The code uses default => "Other", which is invalid syntax in switch expressions.
  3. Final Answer:

    Using 'default' keyword instead of '_' for default case. -> Option A
  4. Quick Check:

    Default case = '_' not 'default' [OK]
Hint: Use '_' for default, not 'default' keyword [OK]
Common Mistakes:
  • Using 'default' instead of '_'
  • Forgetting commas between cases
  • Confusing switch statement syntax with switch expression
5. You want to write a switch expression that returns "Small" for numbers less than 10, "Medium" for numbers between 10 and 20 inclusive, and "Large" for numbers greater than 20. Which of the following switch expressions correctly implements this?
hard
A. var size = number switch { < 10 => "Small", 10 <= number <= 20 => "Medium", _ => "Large" };
B. var size = number switch { < 10 => "Small", > 10 => "Medium", _ => "Large" };
C. var size = number switch { < 10 => "Small", > 10 and < 20 => "Medium", _ => "Large" };
D. var size = number switch { < 10 => "Small", >= 10 and <= 20 => "Medium", _ => "Large" };

Solution

  1. Step 1: Understand pattern matching syntax in switch expressions

    Modern C# supports relational patterns like < 10 and logical patterns like and to combine conditions.
  2. Step 2: Check each option for correct range coverage

    var size = number switch { < 10 => "Small", >= 10 and <= 20 => "Medium", _ => "Large" }; uses < 10 for "Small", and >= 10 and <= 20 for "Medium", which correctly covers 10 to 20 inclusive. The default _ covers "Large".
  3. Final Answer:

    var size = number switch { < 10 => "Small", >= 10 and <= 20 => "Medium", _ => "Large" }; -> Option D
  4. Quick Check:

    Use relational and logical patterns for ranges [OK]
Hint: Use 'and' to combine conditions in switch patterns [OK]
Common Mistakes:
  • Using invalid syntax like '10 <= number <= 20'
  • Missing inclusive range boundaries
  • Using overlapping or incorrect ranges
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard