0
0
C Sharp (C#)programming~10 mins

Switch expression (modern C#) - Interactive Code Practice

Choose your learning style9 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.