Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new dictionary in C#.
C Sharp (C#)
var dict = new Dictionary<string, int>[1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} instead of parentheses () to instantiate the dictionary.
Using angle brackets <> which are for generic type parameters, not instantiation.
✗ Incorrect
In C#, to create a new dictionary, you use parentheses after the constructor name. To initialize with elements, you use curly braces.
2fill in blank
mediumComplete the code to add a key-value pair to the dictionary.
C Sharp (C#)
dict.[1]("apple", 5);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Insert or Put which are not dictionary methods in C#.
Using Set which is not a method for adding items to a dictionary.
✗ Incorrect
The Add method is used to add a new key-value pair to a dictionary in C#.
3fill in blank
hardFix the error in accessing a value by key from the dictionary.
C Sharp (C#)
int count = dict[[1]]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the key without quotes which causes a compile error.
Using a number or variable name instead of the string key.
✗ Incorrect
Dictionary keys of type string must be accessed using quotes around the key name.
4fill in blank
hardFill both blanks to check if a key exists and get its value safely.
C Sharp (C#)
if (dict.[1]("banana", out int [2])) { Console.WriteLine([2]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ContainsKey which only checks existence but does not get the value.
Using variable names that are not declared or mismatched.
✗ Incorrect
TryGetValue checks if the key exists and outputs the value to the variable provided.
5fill in blank
hardFill all three blanks to create a dictionary from a list of keys with initial value 0.
C Sharp (C#)
var keys = new List<string> {"a", "b", "c"};
var dict = keys.ToDictionary([1] => [2], [3] => 0); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the two lambdas which causes errors.
Using underscore '_' as a variable name in this context.
✗ Incorrect
The lambda uses the same variable name for key selector and value selector; 'x' is a common choice.