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

Switch expressions with patterns in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Switch Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of switch expression with type patterns
What is the output of the following C# code using switch expressions with type patterns?
C Sharp (C#)
object value = 42;
string result = value switch
{
    int i => $"Integer: {i}",
    string s => $"String: {s}",
    _ => "Unknown"
};
System.Console.WriteLine(result);
AString: 42
BInteger: 42
CUnknown
DCompilation error
Attempts:
2 left
💡 Hint
Check the type of the variable 'value' and how the switch expression matches patterns.
Predict Output
intermediate
2:00remaining
Switch expression with property pattern output
What will be printed by this C# code using a switch expression with a property pattern?
C Sharp (C#)
record Person(string Name, int Age);

Person p = new("Alice", 30);
string category = p switch
{
    { Age: < 18 } => "Minor",
    { Age: >= 18 and < 65 } => "Adult",
    { Age: >= 65 } => "Senior",
    _ => "Unknown"
};
System.Console.WriteLine(category);
AUnknown
BMinor
CSenior
DAdult
Attempts:
2 left
💡 Hint
Look at the Age property of the Person record and which pattern it matches.
🔧 Debug
advanced
2:00remaining
Identify the error in this switch expression with tuple patterns
This C# code uses a switch expression with tuple patterns. Which option correctly describes the error it produces?
C Sharp (C#)
var point = (x: 3, y: 4);
string quadrant = point switch
{
    ( > 0, > 0 ) => "First",
    ( < 0, > 0 ) => "Second",
    ( < 0, < 0 ) => "Third",
    ( > 0, < 0 ) => "Fourth",
    _ => "Origin or axis"
};
System.Console.WriteLine(quadrant);
ACompilation error due to missing parentheses around relational patterns
BRuntime NullReferenceException
COutput: First
DOutput: Origin or axis
Attempts:
2 left
💡 Hint
Check if the tuple patterns and relational patterns syntax are correct and what the values are.
📝 Syntax
advanced
2:00remaining
Which switch expression syntax is correct for matching a string prefix?
Which option shows the correct C# switch expression syntax to match a string starting with "Hello" using a pattern?
Astring greeting = input switch { string s when s.StartsWith("Hello") => "Hi", _ => "Bye" };
Bstring greeting = input switch { string s if s.StartsWith("Hello") => "Hi", _ => "Bye" };
Cstring greeting = input switch { string s => s.StartsWith("Hello") ? "Hi" : "Bye" };
Dstring greeting = input switch { string s where s.StartsWith("Hello") => "Hi", _ => "Bye" };
Attempts:
2 left
💡 Hint
Recall the correct keyword for adding a condition in a switch expression pattern is 'when'.
🚀 Application
expert
3:00remaining
Determine the number of items in the dictionary created by this switch expression
Consider this C# code that uses a switch expression inside a dictionary initializer. How many key-value pairs will the resulting dictionary contain?
C Sharp (C#)
var inputs = new object[] { 1, "two", 3.0, null };
var dict = new Dictionary<object, string>();
foreach (var item in inputs)
{
    dict[item] = item switch
    {
        int i => $"Int {i}",
        string s => $"Str {s}",
        null => "Null",
        _ => "Other"
    };
}
System.Console.WriteLine(dict.Count);
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
Check how many unique keys are added to the dictionary and if any keys are overwritten.