Which namespace in C# contains the collections like List<T>, Dictionary<K,V>, and Queue<T>?
Answer: A
System.Collections.Generic is the standard namespace for generic collections in C#. It provides type-safe collections that prevent runtime errors.
Q.2Easy
What is the main difference between ArrayList and List<T> in C#?
Answer: B
List<T> is generic and type-safe, preventing boxing/unboxing overhead and providing compile-time type checking. ArrayList stores objects and requires casting.
Q.3Easy
Which collection allows duplicate keys in C#?
Answer: C
List<T> allows duplicates as it stores elements sequentially. Dictionary and Hashtable enforce unique keys; attempting to add a duplicate key throws an exception.
Q.4Easy
What does the Count property of a collection return in C#?
Answer: B
Count returns the number of elements currently stored. Capacity is different and refers to allocated space.
Q.5Easy
Which collection in C# implements LIFO (Last In First Out) principle?
Answer: B
Stack<T> follows LIFO where the last element added is the first to be removed. Queue<T> follows FIFO (First In First Out).
Advertisement
Q.6Easy
What will be the output of the following code?
List<int> nums = new List<int> {1, 2, 3};
nums.Add(4);
Console.WriteLine(nums.Count);
Answer: B
The list initially has 3 elements. After Add(4), it has 4 elements. Count returns 4.
Q.7Medium
Which method removes all elements from a collection in C#?
Answer: C
Clear() removes all elements from a collection. Remove() removes a specific element, and RemoveAt() removes an element at a specific index.
Q.8Medium
What is the time complexity of accessing an element by index in a List<T>?
Answer: C
List<T> is backed by an array, allowing direct index-based access in constant time O(1).
Q.9Medium
Which collection maintains insertion order but uses a hash table for faster lookups?
Answer: A
Dictionary<K,V> in .NET maintains insertion order (in recent versions) and provides O(1) lookup. SortedDictionary maintains sorted order instead.
Q.10Medium
What happens when you try to access an index beyond the Count in a List<T>?
Answer: C
Accessing an invalid index throws IndexOutOfRangeException. This is a runtime error that must be caught to prevent program crashes.
Q.11Medium
Which interface must be implemented to use a collection in a foreach loop?
Answer: A
IEnumerable interface provides GetEnumerator() method, enabling foreach iteration. IEnumerable<T> is the generic version.
Q.12Medium
What is the difference between Stack<T>.Push() and Stack<T>.Pop()?
Answer: A
Push() adds an element to the stack, while Pop() removes and returns the top element. If stack is empty, Pop throws InvalidOperationException.