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

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Type Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of type pattern matching with 'is' and 'not'
What is the output of this C# code snippet?
C Sharp (C#)
object obj = 42;
if (obj is not string s)
{
    Console.WriteLine("Not a string");
}
else
{
    Console.WriteLine($"String: {s}");
}
ANot a string
BString: 42
CCompilation error
DNo output
Attempts:
2 left
💡 Hint
Remember that 'is not' checks if the object is NOT of the specified type.
❓ Predict Output
intermediate
2:00remaining
Output of switch expression with type patterns
What will this C# switch expression output?
C Sharp (C#)
object val = 3.14;
string result = val switch
{
    int i => $"Integer {i}",
    double d => $"Double {d}",
    _ => "Unknown"
};
Console.WriteLine(result);
AInteger 3
BUnknown
CDouble 3.14
DCompilation error
Attempts:
2 left
💡 Hint
Check the runtime type of 'val' and which pattern matches first.
🔧 Debug
advanced
2:00remaining
Identify the error in type pattern usage
This code snippet causes a compilation error. What is the cause?
C Sharp (C#)
object obj = "hello";
if (obj is int i && i > 0)
{
    Console.WriteLine(i);
}
ANo error, code compiles fine
BThe variable 'i' is not declared before use
CThe 'is' pattern cannot be combined with '&&' operator
DCannot use relational operator '>' on 'int' in pattern matching
Attempts:
2 left
💡 Hint
Try compiling and running the code to see if it works.
❓ Predict Output
advanced
2:00remaining
Output of nested type patterns with property patterns
What is the output of this C# code?
C Sharp (C#)
record Point(int X, int Y);
object obj = new Point(3, 4);
string output = obj switch
{
    Point { X: 3, Y: 4 } p => $"Point at (3,4)",
    Point p => $"Point at ({p.X},{p.Y})",
    _ => "Unknown"
};
Console.WriteLine(output);
ACompilation error
BPoint at (0,0)
CUnknown
DPoint at (3,4)
Attempts:
2 left
💡 Hint
Look at the property pattern matching inside the switch.
❓ Predict Output
expert
2:00remaining
Output of complex pattern matching with 'or' and 'and' patterns
What is the output of this C# code snippet?
C Sharp (C#)
object val = 10;
string result = val switch
{
    int i and (> 0 and < 5) => "Between 1 and 4",
    int i and (>= 5 or < 0) => "Outside 1 to 4",
    _ => "Other"
};
Console.WriteLine(result);
ABetween 1 and 4
BOutside 1 to 4
COther
DCompilation error
Attempts:
2 left
💡 Hint
Check how the 'and' and 'or' patterns combine conditions.

Practice

(1/5)
1. What does a type pattern in C# do when used in an if statement like if (obj is string s)?
easy
A. Always returns true regardless of obj type
B. Converts obj to a string without checking
C. Checks if obj is a string and assigns it to s if true
D. Throws an exception if obj is not a string

Solution

  1. Step 1: Understand the is keyword with type pattern

    The is keyword checks if the object is of the specified type.
  2. Step 2: Recognize the pattern variable assignment

    If the check is true, the object is assigned to the variable s of that type.
  3. Final Answer:

    Checks if obj is a string and assigns it to s if true -> Option C
  4. Quick Check:

    Type pattern = Check and assign [OK]
Hint: Type pattern checks type and assigns in one step [OK]
Common Mistakes:
  • Thinking it converts without checking
  • Assuming it always returns true
  • Expecting an exception on type mismatch
2. Which of the following is the correct syntax for using a type pattern in an if statement in C#?
easy
A. if (obj as int number) { }
B. if (int obj is number) { }
C. if (obj int is number) { }
D. if (obj is int number) { }

Solution

  1. Step 1: Recall correct type pattern syntax

    The correct syntax is if (variable is Type name).
  2. Step 2: Check each option

    if (obj is int number) { } matches the correct syntax. Options B, C, and D have incorrect order or keywords.
  3. Final Answer:

    if (obj is int number) { } -> Option D
  4. Quick Check:

    Correct syntax = if (obj is Type name) [OK]
Hint: Remember: if (obj is Type var) [OK]
Common Mistakes:
  • Swapping type and variable order
  • Using 'as' instead of 'is' for pattern
  • Placing keywords incorrectly
3. What will be the output of this C# code?
object obj = 42;
if (obj is int number)
{
Console.WriteLine(number + 10);
}
else
{
Console.WriteLine("Not an int");
}
medium
A. 52
B. Not an int
C. Compilation error
D. Runtime exception

Solution

  1. Step 1: Check the type of obj

    obj holds the integer 42, so obj is int is true.
  2. Step 2: Calculate the output inside the if block

    The variable number is assigned 42, so number + 10 equals 52, which is printed.
  3. Final Answer:

    52 -> Option A
  4. Quick Check:

    Type check true, output = 42 + 10 = 52 [OK]
Hint: If type matches, variable holds value for use [OK]
Common Mistakes:
  • Expecting 'Not an int' output
  • Confusing type check with cast
  • Thinking code throws error
4. Identify the error in this code snippet using type patterns:
object obj = "hello";
if (obj is int number)
{
Console.WriteLine(number);
}
medium
A. No output because obj is not an int
B. Compilation error due to missing else block
C. No error, code runs and prints 'hello'
D. Runtime exception when casting string to int

Solution

  1. Step 1: Analyze the type check condition

    obj is a string, so obj is int number is false.
  2. Step 2: Understand the effect on output

    Since the if condition is false and no else block exists, nothing is printed.
  3. Final Answer:

    No output because obj is not an int -> Option A
  4. Quick Check:

    False type check = no output [OK]
Hint: False type check skips block, no output [OK]
Common Mistakes:
  • Expecting runtime exception
  • Thinking else block is mandatory
  • Assuming it prints the string
5. Given this method using type patterns, what will it return?
<pre>string Describe(object input)
{
return input switch
{
int i => $"Integer: {i}",
string s => $"String: {s}",
null => "Null value",
_ => "Unknown type"
};
}

What is the result of Describe(3.14)?
hard
A. "Integer: 3"
B. "Unknown type"
C. "String: 3.14"
D. Compilation error

Solution

  1. Step 1: Check the switch cases for input 3.14

    3.14 is a double, which does not match int, string, or null.
  2. Step 2: Identify the default case

    The underscore _ matches any other type, so it returns "Unknown type".
  3. Final Answer:

    "Unknown type" -> Option B
  4. Quick Check:

    Switch default case = "Unknown type" [OK]
Hint: Switch default (_) handles unmatched types [OK]
Common Mistakes:
  • Assuming 3.14 is int or string
  • Expecting compilation error
  • Ignoring default case
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