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

Relational patterns in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Relational Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of relational pattern matching with switch
What is the output of this C# code using relational patterns in a switch expression?
C Sharp (C#)
int number = 15;
string result = number switch
{
    < 10 => "Less than 10",
    >= 10 and <= 20 => "Between 10 and 20",
    > 20 => "Greater than 20",
    _ => "Unknown"
};
Console.WriteLine(result);
ALess than 10
BUnknown
CGreater than 20
DBetween 10 and 20
Attempts:
2 left
💡 Hint
Check which relational pattern matches the value 15.
Predict Output
intermediate
2:00remaining
Result of relational pattern in if statement
What will be printed by this C# code that uses relational patterns in an if statement?
C Sharp (C#)
int age = 65;
if (age is > 60)
{
    Console.WriteLine("Senior");
}
else
{
    Console.WriteLine("Not senior");
}
ASenior
BNo output
CCompilation error
DNot senior
Attempts:
2 left
💡 Hint
Check if the age is greater than 60.
Predict Output
advanced
2:00remaining
Output of nested relational patterns in switch
What is the output of this C# code using nested relational patterns in a switch expression?
C Sharp (C#)
int score = 85;
string grade = score switch
{
    < 60 => "F",
    >= 60 and < 70 => "D",
    >= 70 and < 80 => "C",
    >= 80 and < 90 => "B",
    >= 90 => "A",
    _ => "Invalid"
};
Console.WriteLine(grade);
AC
BA
CB
DD
Attempts:
2 left
💡 Hint
Find the range where 85 fits.
Predict Output
advanced
2:00remaining
Value of variable after relational pattern matching
What is the value of the variable 'category' after running this C# code?
C Sharp (C#)
int temperature = 0;
string category = temperature switch
{
    < 0 => "Freezing",
    >= 0 and < 15 => "Cold",
    >= 15 and < 25 => "Warm",
    >= 25 => "Hot",
    _ => "Unknown"
};
A"Cold"
B"Warm"
C"Freezing"
D"Hot"
Attempts:
2 left
💡 Hint
Check which pattern matches temperature 0.
Predict Output
expert
3:00remaining
Output of complex relational pattern with property
What is the output of this C# code using relational patterns with a property in a switch expression?
C Sharp (C#)
record Product(string Name, decimal Price);

Product item = new("Book", 19.99m);
string priceCategory = item switch
{
    { Price: < 10 } => "Cheap",
    { Price: >= 10 and <= 20 } => "Moderate",
    { Price: > 20 } => "Expensive",
    _ => "Unknown"
};
Console.WriteLine(priceCategory);
ACheap
BModerate
CExpensive
DUnknown
Attempts:
2 left
💡 Hint
Look at the Price property and see which range 19.99 fits.