Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a new key-value pair to the dictionary.
C Sharp (C#)
var dict = new Dictionary<string, int>(); dict.[1]("apple", 5);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Insert or Set which are not valid Dictionary methods.
Trying to assign directly without using a method.
✗ Incorrect
The Add method is used to add a new key-value pair to a Dictionary in C#.
2fill in blank
mediumComplete the code to check if the dictionary contains the key "banana".
C Sharp (C#)
var dict = new Dictionary<string, int> { {"banana", 3} };
bool hasKey = dict.[1]("banana"); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ContainsValue which checks for values, not keys.
Using non-existent methods like HasKey or Exists.
✗ Incorrect
ContainsKey checks if a specific key exists in the dictionary.
3fill in blank
hardFix the error in the code to safely get the value for key "orange".
C Sharp (C#)
var dict = new Dictionary<string, int> { {"orange", 7} };
int value;
if (dict.[1]("orange", out value)) {
Console.WriteLine(value);
} 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 non-existent methods like GetValue or GetOrDefault.
✗ Incorrect
TryGetValue tries to get the value for a key and returns true if found.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps words to their lengths if length is greater than 3.
C Sharp (C#)
var words = new List<string> { "cat", "house", "dog", "elephant" };
var lengths = words.Where(word => word.[1] > 3)
.ToDictionary(word => word, word => word.[2]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length() which is invalid because Length is a property, not a method.
Using Count or Size which do not exist for strings.
✗ Incorrect
The Length property gives the number of characters in a string in C#.
5fill in blank
hardFill all three blanks to create a dictionary from a list of tuples filtering only those with value greater than 10.
C Sharp (C#)
var data = new List<(string, int)> { ("a", 5), ("b", 15), ("c", 20) };
var filtered = data.Where(item => item.[1] > 10)
.ToDictionary(item => item.[2], item => item.[3]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Value which is not a tuple property.
Mixing up Item1 and Item2 positions.
✗ Incorrect
Tuples use Item1 for the first element and Item2 for the second element in C#.