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.22Medium
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.23Medium
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.24Medium
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.
Q.25Medium
In a scenario with large datasets, which collection should you use to avoid boxing overhead?
Answer: C
List<T> is a generic collection that avoids boxing of value types, whereas ArrayList boxes all values, causing performance overhead.
Advertisement
Q.26Medium
Which method allows you to get an element from a SortedSet<T> at a specific index without using indexer?
Answer: B
ElementAt() is a LINQ method that retrieves an element at a specific index. SortedSet<T> doesn't support direct indexing.
Q.27Medium
What does TryDequeue() return in ConcurrentQueue<T> when the queue is empty?
Answer: B
TryDequeue() returns false if the queue is empty and sets the out parameter to the default value of T, avoiding exceptions.
Q.28Medium
Which scenario best uses Stack<T> instead of Queue<T> in a practical application?
Answer: B
Stack<T> follows LIFO (Last-In-First-Out) principle, making it ideal for undo/redo functionality where the most recent action is reversed first.
Q.29Medium
In a scenario where you need to store key-value pairs with fast lookup times, which collection is optimal?
Answer: B
Dictionary<K,V> provides O(1) average-case lookup time using hash tables, making it optimal for key-value pair storage with fast retrieval.
Q.30Medium
Which method should be used to safely check and retrieve a value from a Dictionary<K,V> without throwing an exception?
Answer: B
TryGetValue() is the safe method that returns a boolean indicating success and outputs the value without throwing KeyNotFoundException.
Q.31Medium
What is the primary difference between ArrayList and List<T>?
Answer: B
List<T> is strongly-typed (type-safe) and eliminates boxing/unboxing overhead. ArrayList is non-generic and slower due to boxing of value types.
Q.32Medium
In a multiprocessing environment, which thread-safe collection should be used for concurrent operations?
Answer: B
ConcurrentBag<T> and other Concurrent* collections from System.Collections.Concurrent are thread-safe for multi-threaded scenarios without explicit locking.
Q.33Medium
What happens when you modify a List<T> while iterating over it using a foreach loop?
Answer: B
Modifying a collection during enumeration invalidates the enumerator and throws InvalidOperationException. Use a for loop or ToList() to avoid this.
Q.34Medium
Which LINQ method is used to convert a Dictionary<K,V> to a List<T> of key-value pairs?
Answer: B
ToList() converts the Dictionary's KeyValuePair<K,V> enumeration into a List<KeyValuePair<K,V>>. ToArray() would create an array instead.
Q.35Medium
In a data processing pipeline, if you need FIFO (First-In-First-Out) semantics with O(1) enqueue/dequeue, which collection is best?
Answer: C
Queue<T> provides FIFO semantics with O(1) Enqueue() and Dequeue() operations. Stack<T> is LIFO, and LinkedList operations vary.
Q.36Medium
Which collection maintains elements in sorted order and is backed by a binary search tree?
Answer: B
SortedDictionary<K,V> uses a red-black tree (binary search tree) for O(log n) operations. SortedSet<T> also uses BST but for single values.
Q.37Medium
What is the time complexity of Contains() method in a HashSet<T>?
Answer: C
HashSet<T> uses hash-based lookup for Contains(), achieving O(1) average-case time complexity, with O(n) worst-case in case of hash collisions.
Q.38Medium
In a scenario with frequent additions and removals at both ends, which collection is most efficient?
Answer: B
LinkedList<T> provides O(1) AddFirst(), AddLast(), RemoveFirst(), and RemoveLast() operations. List<T> requires O(n) for head removals due to reindexing.