Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the relational pattern is > 10 check in C#?
easy
A. If a value is greater than 10
B. If a value is equal to 10
C. If a value is less than 10
D. If a value is not equal to 10

Solution

  1. Step 1: Understand the relational pattern syntax

    The pattern is > 10 checks if a value is greater than 10.
  2. Step 2: Compare with other options

    Options A, B, and D check less than, equality, or inequality, which are different from > 10.
  3. Final Answer:

    If a value is greater than 10 -> Option A
  4. Quick Check:

    Relational pattern is > 10 means greater than 10 [OK]
Hint: Relational patterns use normal math signs like >, <, == [OK]
Common Mistakes:
  • Confusing > with >= or ==.
  • Thinking it checks equality instead of comparison.
  • Mixing up less than and greater than signs.
2. Which of the following is the correct syntax to check if an integer n is less than or equal to 5 using a relational pattern?
easy
A. if (n is <= 5) { }
B. if (n <= 5) { }
C. if (n is < = 5) { }
D. if (n is <=5) { }

Solution

  1. Step 1: Identify correct relational pattern syntax

    The relational pattern syntax uses is followed by the operator and value without spaces inside the operator, like is <= 5.
  2. Step 2: Check each option

    if (n is <= 5) { } uses correct syntax: if (n is <= 5). if (n <= 5) { } is normal comparison, not a relational pattern. if (n is < = 5) { } has a space between < and = which is invalid. if (n is <=5) { } misses a space after is, which is required.
  3. Final Answer:

    if (n is <= 5) { } -> Option A
  4. Quick Check:

    Relational pattern syntax: is <= value [OK]
Hint: Relational patterns need 'is' then operator with no spaces inside operator [OK]
Common Mistakes:
  • Using normal comparison without 'is'.
  • Adding spaces inside the operator like '< ='.
  • Omitting space after 'is'.
3. What will be the output of this C# code?
int x = 7;
if (x is >= 5 and < 10)
    Console.WriteLine("In range");
else
    Console.WriteLine("Out of range");
medium
A. No output
B. Out of range
C. In range
D. Compilation error

Solution

  1. Step 1: Evaluate the relational pattern condition

    The condition x is >= 5 and < 10 checks if x is between 5 and 9 inclusive. Since x = 7, it satisfies both parts.
  2. Step 2: Determine which branch runs

    Because the condition is true, the Console.WriteLine("In range") runs, printing "In range".
  3. Final Answer:

    In range -> Option C
  4. Quick Check:

    7 is between 5 and 10, so output is "In range" [OK]
Hint: Check both parts of 'and' relational pattern carefully [OK]
Common Mistakes:
  • Thinking 'and' means 'or'.
  • Ignoring one part of the condition.
  • Assuming syntax error due to 'and'.
4. Identify the error in this code snippet using relational patterns:
int score = 85;
switch (score)
{
    case is > 90:
        Console.WriteLine("Excellent");
        break;
    case is >= 80 and < 90:
        Console.WriteLine("Good");
        break;
    default:
        Console.WriteLine("Average");
        break;
}
medium
A. The 'and' keyword is not allowed in switch case patterns
B. Missing braces around case blocks
C. Relational patterns cannot be used in switch cases
D. The pattern 'is > 90' should be 'case > 90:' without 'is'

Solution

  1. Step 1: Identify the syntax error in switch case patterns

    Relational patterns in switch statements use syntax like case > 90:, without the is keyword. The code incorrectly uses case is > 90: and case is >= 80 and < 90:.
  2. Step 2: Evaluate the options

    The 'and' keyword is not allowed in switch case patterns is incorrect; and is allowed to combine patterns like case >= 80 and < 90:. The pattern 'is > 90' should be 'case > 90:' without 'is' correctly identifies the misuse of is. Relational patterns cannot be used in switch cases is false; relational patterns are supported in switch cases. Missing braces around case blocks is wrong; braces are not required around case contents.
  3. Final Answer:

    The pattern 'is > 90' should be 'case > 90:' without 'is' -> Option D
  4. Quick Check:

    Switch case relational pattern: case > value, no is [OK]
Hint: No 'is' keyword needed for relational patterns in switch cases [OK]
Common Mistakes:
  • Adding 'is' before relational operators in switch cases.
  • Thinking 'and' cannot combine patterns in switch cases.
  • Confusing switch statements with 'is' patterns.
5. You want to write a method that returns "Child", "Teen", or "Adult" based on age using relational patterns. Which of these switch expressions correctly implements this?
string GetAgeGroup(int age) => age switch
{
    < 13 => "Child",
    >= 13 and < 20 => "Teen",
    _ => "Adult"
};
hard
A. Replace 'and' with '&&' in the second pattern
B. Correct as is
C. Use nested if statements instead of switch
D. Add parentheses around '>= 13 and < 20'

Solution

  1. Step 1: Analyze the switch expression with relational patterns

    The switch expression uses relational patterns correctly: < 13 for "Child", combined pattern >= 13 and < 20 for "Teen", and default for "Adult".
  2. Step 2: Confirm syntax correctness

    The 'and' keyword is valid in switch expressions for combining patterns. No parentheses or '&&' are needed here.
  3. Final Answer:

    Correct as is -> Option B
  4. Quick Check:

    Switch expressions support 'and' for combined relational patterns [OK]
Hint: Use 'and' to combine relational patterns in switch expressions [OK]
Common Mistakes:
  • Replacing 'and' with '&&' which is invalid in patterns.
  • Adding unnecessary parentheses.
  • Thinking switch expressions don't support relational patterns.
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard