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

Dictionary methods and access patterns in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dictionary Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing dictionary with TryGetValue
What is the output of this C# code snippet?
C Sharp (C#)
var dict = new Dictionary<string, int> { {"apple", 3}, {"banana", 5} };
if (dict.TryGetValue("banana", out int value))
{
    Console.WriteLine(value);
}
else
{
    Console.WriteLine("Not found");
}
A5
BRuntime error
C3
DNot found
Attempts:
2 left
💡 Hint
TryGetValue returns true if the key exists and outputs the value.
Predict Output
intermediate
2:00remaining
Result of removing a key from dictionary
What is the output of this C# code?
C Sharp (C#)
var dict = new Dictionary<int, string> { {1, "one"}, {2, "two"} };
bool removed = dict.Remove(3);
Console.WriteLine(removed);
AFalse
BTrue
CKeyNotFoundException
DCompilation error
Attempts:
2 left
💡 Hint
Remove returns false if the key does not exist.
Predict Output
advanced
2:00remaining
Output of dictionary indexer with missing key
What happens when this C# code runs?
C Sharp (C#)
var dict = new Dictionary<string, int> { {"x", 10} };
Console.WriteLine(dict["y"]);
A0
BKeyNotFoundException
Cnull
DCompilation error
Attempts:
2 left
💡 Hint
Accessing a missing key with indexer throws an exception.
Predict Output
advanced
2:00remaining
Count of items after TryAdd calls
What is the count of items in the dictionary after this code runs?
C Sharp (C#)
var dict = new Dictionary<int, string>();
dict.TryAdd(1, "one");
dict.TryAdd(1, "uno");
dict.TryAdd(2, "two");
Console.WriteLine(dict.Count);
A3
B1
C2
DRuntime error
Attempts:
2 left
💡 Hint
TryAdd does not overwrite existing keys.
Predict Output
expert
2:00remaining
Output of iterating dictionary keys and values
What is the output of this C# code?
C Sharp (C#)
var dict = new Dictionary<string, int> { {"a", 1}, {"b", 2} };
foreach (var key in dict.Keys)
{
    Console.Write(key);
}
foreach (var value in dict.Values)
{
    Console.Write(value);
}
Ab2a1
B12ab
Ca1b2
Dab12
Attempts:
2 left
💡 Hint
Keys and values are iterated in the same order as insertion.