Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or brackets instead of curly braces for switch expression.
Forgetting the default case with underscore (_).
✗ Incorrect
The switch expression uses curly braces with pattern cases like { 1 => "One", _ => "Other" }.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'where' or 'if' instead of 'when' in the pattern.
Incorrect syntax causing compile errors.
✗ Incorrect
The correct syntax for a pattern with condition is 'var n when n % 2 == 0'.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes for strings.
Using unquoted identifiers instead of string literals.
✗ Incorrect
String literals in C# must be enclosed in double quotes, so "Sunday" is correct.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for both patterns causing confusion.
Using invalid variable names or missing 'var'.
✗ Incorrect
The pattern variable can be any valid identifier; here 'var s' and 'var x' are used for the two cases.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Reusing the same variable name in multiple patterns.
Omitting 'var' keyword.
✗ Incorrect
Each pattern uses 'var' with a unique variable name: 'var z', 'var x', and 'var y' respectively.