0
0
C Sharp (C#)programming~20 mins

Nullable value types in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nullable Value Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
A0
Bnull
C5
DCompilation error
Attempts:
2 left
💡 Hint
Adding a nullable int with null results in null.
🧠 Conceptual
intermediate
1:30remaining
Nullable Boolean Default Value
What is the default value of a nullable bool declared as bool? flag; before any assignment?
Anull
Bfalse
Ctrue
DCompilation error
Attempts:
2 left
💡 Hint
Nullable types default to null if not assigned.
🔧 Debug
advanced
2: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);
Aint y = x.GetValueOrDefault();
Bint y = (int)x;
Cint y = x ?? 0;
Dint y = x.Value ?? 0;
Attempts:
2 left
💡 Hint
Use the null-coalescing operator to provide a default value.
📝 Syntax
advanced
1:30remaining
Identify the Syntax Error
Which option contains a syntax error when declaring a nullable int?
Aint? number = 10;
BNullable<int> number = null;
Cint? number = null;
Dint? number = ?5;
Attempts:
2 left
💡 Hint
Check the placement of the question mark.
🚀 Application
expert
2:30remaining
Count Non-Null Nullable Integers
Given this list of nullable integers, what is the count of non-null values?
List numbers = 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);
A3
B2
C5
DCompilation error
Attempts:
2 left
💡 Hint
Use the HasValue property to check for non-null values.