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

Null-coalescing operator in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign a default value if 'name' is null.

C Sharp (C#)
string displayName = name [1] "Guest";
Drag options to blanks, or click blank then click option'
A||
B??
C&&
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '||' which is a logical OR and does not work with null values.
Using '&&' which is a logical AND and incorrect here.
Using '==' which is a comparison operator, not for assignment.
2fill in blank
medium

Complete the code to assign 'result' to 'input' or 0 if 'input' is null.

C Sharp (C#)
int result = input [1] 0;
Drag options to blanks, or click blank then click option'
A?:
B&&
C||
D??
Attempts:
3 left
💡 Hint
Common Mistakes
Using '||' which is a logical OR and not for null checks.
Using '?:' which is the ternary operator but more verbose here.
Using '&&' which is logical AND and incorrect.
3fill in blank
hard

Fix the error in the code by completing the null-coalescing expression.

C Sharp (C#)
string message = GetMessage() [1] "No message";
Drag options to blanks, or click blank then click option'
A||
B&&
C??
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '||' which is a logical OR and not for null fallback.
Using '==' which is a comparison, not assignment.
Using '&&' which is logical AND and incorrect.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths only if length is greater than 3.

C Sharp (C#)
var lengths = new Dictionary<string, int> { {"apple", "apple".Length}, {"cat", "cat".Length} };
var filtered = lengths.Where(kv => kv.Value [1] 3).ToDictionary(kv => kv.Key, kv => kv.Value [2] 1);
Drag options to blanks, or click blank then click option'
A>
B+
C<
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for filtering.
Using '-' instead of '+' for incrementing values.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values only if value is not null.

C Sharp (C#)
var data = new Dictionary<string, string?> { {"a", "apple"}, {"b", null} };
var result = data.Where(kv => kv.Value [1] null).ToDictionary(kv => kv.Key.[2](), kv => kv.Value.[3]());
Drag options to blanks, or click blank then click option'
A!=
BToUpper
CToLower
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' to filter nulls.
Using 'ToLower()' instead of 'ToUpper()' for uppercase conversion.