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

Dictionary methods and access patterns 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 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'
AAdd
BInsert
CPut
DSet
Attempts:
3 left
💡 Hint
Common Mistakes
Using Insert or Set which are not valid Dictionary methods.
Trying to assign directly without using a method.
2fill in blank
medium

Complete 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'
AContainsKey
BContainsValue
CHasKey
DExists
Attempts:
3 left
💡 Hint
Common Mistakes
Using ContainsValue which checks for values, not keys.
Using non-existent methods like HasKey or Exists.
3fill in blank
hard

Fix 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'
AGetOrDefault
BGetValue
CContainsKey
DTryGetValue
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.
4fill in blank
hard

Fill 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'
ALength
BCount
CSize
DLength()
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.
5fill in blank
hard

Fill 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'
AItem2
BItem1
DValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using Value which is not a tuple property.
Mixing up Item1 and Item2 positions.