Dictionary enforces unique keys. Attempting to add a duplicate key throws ArgumentException. Use dict["a"] = 2 to update.
Q.8Medium
Which collection should be used when you need sorted key-value pairs automatically?
Answer: B
SortedDictionary<K,V> automatically maintains keys in sorted order. Dictionary<K,V> maintains insertion order but not sorted order.
Q.9Medium
Which collection type is best suited for implementing a priority queue in C#?
Answer: B
PriorityQueue<T, TPriority> was introduced in .NET 6 specifically for priority queue implementation.
Q.10Medium
What happens when you add a duplicate key to a Dictionary<TKey, TValue>?
Answer: B
Dictionary<TKey, TValue> throws ArgumentException when adding duplicate keys. Use indexer notation to update values.
Q.11Medium
Which LINQ method returns the first element matching a condition, or throws if none exists?
Answer: A
First() throws InvalidOperationException if no match. FirstOrDefault() returns null/default. Find() is a List<T> method.
Q.12Medium
What is the primary difference between SortedList<TKey, TValue> and SortedDictionary<TKey, TValue>?
Answer: A
SortedList uses arrays (faster retrieval, slower insertion). SortedDictionary uses red-black tree (balanced insertion/deletion). SortedDictionary has better insertion time O(log n) vs O(n) for SortedList.
Q.13Medium
Which collection in C# is NOT thread-safe by default?
Answer: C
List<T> is not thread-safe. All Concurrent* collections (ConcurrentDictionary, ConcurrentBag, ConcurrentQueue) are thread-safe.
Q.14Medium
What does the Contains() method use in HashSet<T> to determine membership?
Answer: C
HashSet<T> uses hash codes for O(1) average lookup. It computes hash code first, then uses equality comparison.
Q.15Medium
Which of the following collections preserves insertion order and is thread-safe?
Answer: B
ConcurrentDictionary is thread-safe. While it doesn't strictly preserve insertion order like some collections, it's the thread-safe option among these choices.
Q.16Medium
What is the time complexity for removing an element from the middle of a LinkedList<T>?
Answer: A
If you have a LinkedListNode<T>, removal is O(1). However, finding the node is O(n), so overall search + remove is O(n).
Q.17Medium
In C# 2024-25, which collection should be used for fast membership testing with no duplicates?
Answer: B
HashSet<T> provides O(1) average lookup and prevents duplicates. SortedSet maintains order but slower. List<T> requires O(n) lookup.
Q.18Medium
What is the output of the following code?
var dict = new Dictionary<int, string>();
dict[1] = "A";
dict[1] = "B";
Console.WriteLine(dict[1]);
Answer: B
Using the indexer on Dictionary with an existing key updates the value, not throws. Output is 'B'.
Q.19Medium
Which collection in C# maintains insertion order and allows fast removal from both ends?
Answer: B
LinkedList<T> allows O(1) removal from both ends and maintains insertion order. Deque<T> is not a standard C# collection.
Q.20Medium
Which collection type should be used when you need key-value pairs with guaranteed order of insertion?
Answer: D
OrderedDictionary maintains insertion order for key-value pairs. Dictionary<K,V> does not guarantee order in older versions; SortedDictionary sorts by key.