Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the correct string for the number using a switch expression.
C Sharp (C#)
string result = number switch [1]; 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 underscore (_) as the default case.
✗ Incorrect
The switch expression in C# uses curly braces with pattern cases inside.
2fill in blank
mediumComplete the switch expression to return "Positive" for numbers greater than zero.
C Sharp (C#)
string result = number switch {
> 0 => "Positive",
0 => "Zero",
[1] => "Negative"
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>= 0' which overlaps with zero and positive cases.
Using '== 0' which duplicates the zero case.
✗ Incorrect
The pattern '< 0' matches negative numbers in the switch expression.
3fill in blank
hardFix the error in the switch expression to correctly handle strings starting with 'A'.
C Sharp (C#)
string result = input switch {
[1] => "Starts with A",
_ => "Other"
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=>' instead of 'when' for conditions.
Using 'if' or 'where' which are invalid in this context.
✗ Incorrect
The correct syntax for pattern matching with a condition is 'type variable when condition'.
4fill in blank
hardFill both blanks to return the correct description for the shape using switch expression with type patterns.
C Sharp (C#)
string description = shape switch {
[1] => "Circle with radius " + c.Radius,
[2] => "Rectangle with width " + r.Width + " and height " + r.Height,
_ => "Unknown shape"
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing variable names and types incorrectly.
Using the wrong variable name for the shape type.
✗ Incorrect
Use type patterns with variable names matching the properties used in the expressions.
5fill in blank
hardFill all three blanks to create a switch expression that returns a message based on the object type and value.
C Sharp (C#)
string message = obj switch {
[1] when i > 0 => "Positive integer",
[2] when s.Length == 0 => "Empty string",
[3] => "Other"
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names or types in patterns.
Not using '_' for the default case.
✗ Incorrect
Use type patterns with variable names for conditions and '_' as the default case.