Challenge - 5 Problems
Null-Conditional Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of null-conditional operator with null object
What is the output of this C# code snippet using the null-conditional operator?
C Sharp (C#)
class Program { static void Main() { string? text = null; int? length = text?.Length; System.Console.WriteLine(length == null ? "null" : length.ToString()); } }
Attempts:
2 left
💡 Hint
The null-conditional operator returns null if the object is null instead of throwing an exception.
✗ Incorrect
Since 'text' is null, 'text?.Length' evaluates to null, so the output is 'null'. No exception is thrown.
❓ Predict Output
intermediate2:00remaining
Null-conditional operator with method invocation
What will this C# program print?
C Sharp (C#)
class Program { static string? GetName(bool returnNull) { if (returnNull) return null; return "Alice"; } static void Main() { string? name = GetName(true); int? length = name?.Length; System.Console.WriteLine(length ?? -1); } }
Attempts:
2 left
💡 Hint
The null-coalescing operator '??' provides a default value if the left side is null.
✗ Incorrect
GetName(true) returns null, so 'name?.Length' is null. The '?? -1' returns -1 as default.
🔧 Debug
advanced2:00remaining
Identify the error with null-conditional operator usage
What error does this C# code produce?
C Sharp (C#)
class Program { static void Main() { string? text = null; int length = text?.Length; System.Console.WriteLine(length); } }
Attempts:
2 left
💡 Hint
The null-conditional operator returns a nullable type, but the variable is non-nullable.
✗ Incorrect
The expression 'text?.Length' returns 'int?', but 'length' is declared as 'int'. This causes a compile-time error.
🧠 Conceptual
advanced2:00remaining
Behavior of null-conditional operator with events
Consider this C# code snippet. What is the purpose of using the null-conditional operator when invoking the event?
C Sharp (C#)
public class Publisher { public event System.Action? OnChange; public void Raise() { OnChange?.Invoke(); } }
Attempts:
2 left
💡 Hint
Events can be null if no methods are subscribed.
✗ Incorrect
Using 'OnChange?.Invoke()' safely calls the event only if it has subscribers, avoiding exceptions.
❓ Predict Output
expert2:00remaining
Output with chained null-conditional operators and null-coalescing
What is the output of this C# program?
C Sharp (C#)
class Person { public string? Name { get; set; } public Person? Friend { get; set; } } class Program { static void Main() { Person? p = new Person { Name = "Bob", Friend = null }; string friendName = p?.Friend?.Name ?? "No friend"; System.Console.WriteLine(friendName); } }
Attempts:
2 left
💡 Hint
The null-conditional operator returns null if any part of the chain is null, then the null-coalescing operator provides a default.
✗ Incorrect
Since 'p.Friend' is null, 'p?.Friend?.Name' is null, so the output is the default string 'No friend'.