Dictionary enforces unique keys. Attempting to add a duplicate key throws ArgumentException. Use dict["a"] = 2 to update.
Q.124Medium
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.125Medium
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.
Advertisement
Q.126Medium
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.127Medium
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.128Medium
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.129Medium
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.130Medium
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.131Medium
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.132Medium
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.133Medium
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.134Medium
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.135Medium
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.136Medium
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.
Q.137Medium
What is the time complexity of the IndexOf() method in List<T>?
Answer: C
IndexOf() performs a linear search through the list, resulting in O(n) time complexity in the worst case.
Q.138Medium
What is the primary advantage of using SortedList<K,V> over SortedDictionary<K,V>?
Answer: B
SortedList uses less memory than SortedDictionary but has slower insertion/deletion. SortedDictionary is better for frequent modifications.
Q.139Medium
In C# 2024-25, which collection should be used for thread-safe operations without explicit locking?
Answer: B
ConcurrentBag<T> from System.Collections.Concurrent provides thread-safe operations without explicit locking, suitable for multi-threaded scenarios.
Q.140Medium
What happens when you enumerate a Dictionary<K,V> during modification?
Answer: B
Modifying a Dictionary<K,V> while enumerating throws InvalidOperationException. This is a safety feature to prevent undefined behavior.