Challenge - 5 Problems
Relational Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Check which relational pattern matches the value 15.
✗ Incorrect
The value 15 matches the pattern '>= 10 and <= 20', so the output is 'Between 10 and 20'.
❓ Predict Output
intermediate2: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"); }
Attempts:
2 left
💡 Hint
Check if the age is greater than 60.
✗ Incorrect
The age 65 is greater than 60, so the condition is true and 'Senior' is printed.
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
Find the range where 85 fits.
✗ Incorrect
85 is between 80 and 90, so the grade is 'B'.
❓ Predict Output
advanced2: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" };
Attempts:
2 left
💡 Hint
Check which pattern matches temperature 0.
✗ Incorrect
0 is >= 0 and < 15, so category is 'Cold'.
❓ Predict Output
expert3: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);
Attempts:
2 left
💡 Hint
Look at the Price property and see which range 19.99 fits.
✗ Incorrect
The Price 19.99 is between 10 and 20 inclusive, so the output is 'Moderate'.