Challenge - 5 Problems
Nullable Value Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Nullable Int Addition
What is the output of this C# code snippet?
C Sharp (C#)
int? a = 5; int? b = null; var result = a + b; Console.WriteLine(result == null ? "null" : result.ToString());
Attempts:
2 left
💡 Hint
Adding a nullable int with null results in null.
✗ Incorrect
When you add a nullable int with a null value, the result is null because the operation propagates null.
🧠 Conceptual
intermediate1:30remaining
Nullable Boolean Default Value
What is the default value of a nullable bool declared as
bool? flag; before any assignment?Attempts:
2 left
💡 Hint
Nullable types default to null if not assigned.
✗ Incorrect
Nullable value types default to null when declared but not assigned any value.
🔧 Debug
advanced2:30remaining
Fix the NullReferenceException
This code throws a NullReferenceException. Which option fixes it?
C Sharp (C#)
int? x = null; int y = x.Value; Console.WriteLine(y);
Attempts:
2 left
💡 Hint
Use the null-coalescing operator to provide a default value.
✗ Incorrect
Accessing Value on a null nullable throws an exception. Using 'x ?? 0' safely provides 0 if x is null.
📝 Syntax
advanced1:30remaining
Identify the Syntax Error
Which option contains a syntax error when declaring a nullable int?
Attempts:
2 left
💡 Hint
Check the placement of the question mark.
✗ Incorrect
The question mark must come after the type, not before the value.
🚀 Application
expert2:30remaining
Count Non-Null Nullable Integers
Given this list of nullable integers, what is the count of non-null values?
Listnumbers = new() { 1, null, 3, null, 5 };
C Sharp (C#)
List<int?> numbers = new() { 1, null, 3, null, 5 }; int count = numbers.Count(n => n.HasValue); Console.WriteLine(count);
Attempts:
2 left
💡 Hint
Use the HasValue property to check for non-null values.
✗ Incorrect
The list has three non-null values: 1, 3, and 5. Counting those gives 3.