Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The null-coalescing operator '??' returns the left-hand operand if it is not null; otherwise, it returns the right-hand operand.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The '??' operator returns the left operand if it is not null; otherwise, it returns the right operand.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The '??' operator correctly provides a fallback value if GetMessage() returns null.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for filtering.
Using '-' instead of '+' for incrementing values.
✗ Incorrect
We filter lengths greater than 3 and add 1 to each value in the new dictionary.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' to filter nulls.
Using 'ToLower()' instead of 'ToUpper()' for uppercase conversion.
✗ Incorrect
We filter out null values using '!=', then convert keys and values to uppercase using 'ToUpper()'.