Challenge - 5 Problems
Type Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of type pattern matching with 'is' and 'when'
What is the output of the following C# code snippet?
C Sharp (C#)
object obj = 42; string result = obj is int i && i > 40 ? "Greater than 40" : "40 or less"; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Check the type of obj and the condition in the 'when' clause.
✗ Incorrect
The variable obj is an int with value 42, which matches the pattern 'int i'. The 'when' clause checks if i > 40, which is true, so the result is 'Greater than 40'.
❓ Predict Output
intermediate2:00remaining
Output of switch expression with type patterns
What will be printed by this C# code?
C Sharp (C#)
object val = 3.14; string output = val switch { int i => $"Integer: {i}", double d => $"Double: {d}", _ => "Unknown type" }; Console.WriteLine(output);
Attempts:
2 left
💡 Hint
Look at the type of val and the matching patterns in the switch expression.
✗ Incorrect
val is a double with value 3.14, so the pattern 'double d' matches and the output is 'Double: 3.14'.
🔧 Debug
advanced2:00remaining
Identify the runtime error in type pattern matching
This code throws an exception at runtime. Which option correctly identifies the cause?
C Sharp (C#)
object data = null; if (data is string s) { Console.WriteLine(s.Length); } else { Console.WriteLine("Not a string"); }
Attempts:
2 left
💡 Hint
Consider how pattern matching with 'is' handles null values.
✗ Incorrect
The pattern 'data is string s' fails if data is null, so the else branch runs printing 'Not a string'. No exception occurs.
❓ Predict Output
advanced2:00remaining
Output of nested type patterns with property matching
What is the output of this C# code?
C Sharp (C#)
record Person(string Name, int Age); object obj = new Person("Alice", 30); string message = obj switch { Person { Age: > 18 } p => $"Adult: {p.Name}", Person p => $"Minor: {p.Name}", _ => "Unknown" }; Console.WriteLine(message);
Attempts:
2 left
💡 Hint
Check the property pattern and the value of Age.
✗ Incorrect
The object is a Person with Age 30, which matches the first pattern with Age > 18, so the output is 'Adult: Alice'.
🧠 Conceptual
expert2:00remaining
Which option causes a compile-time error in type pattern usage?
Given the following code snippets, which one will cause a compile-time error?
Attempts:
2 left
💡 Hint
Check the syntax rules for combining patterns with 'or' and variable declarations.
✗ Incorrect
Option D tries to declare two variables in an 'or' pattern, which is not allowed and causes a compile-time error.