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.7Easy
Which collection in C# maintains insertion order and allows duplicate elements?
Answer: A
List<T> maintains insertion order and allows duplicates. HashSet<T> doesn't allow duplicates, SortedSet<T> sorts elements, and Queue<T> follows FIFO.
Q.8Easy
Which collection throws an exception when you try to dequeue from an empty Queue<T>?
Answer: A
Dequeue() throws InvalidOperationException on empty queue. TryDequeue() returns false instead. Peek() also throws but doesn't remove.
Q.9Easy
What is the time complexity of accessing an element by index in List<T>?
Answer: C
List<T> is backed by an array, allowing O(1) random access by index.
Q.10Easy
Which method in Stack<T> returns the top element without removing it?
Answer: B
Peek() returns the top element without removal. Pop() removes it. Top() and Get() are not Stack<T> methods.
Q.11Easy
Which IEnumerable method should be used to filter elements based on a condition and maintain type safety?
Answer: A
Where<T> is a LINQ method that filters elements based on a predicate condition while maintaining type safety in the collection.
Q.12Easy
In a HashSet<T>, what happens when you try to add a duplicate element?
Answer: B
HashSet<T>.Add() returns false if the element already exists and does not add duplicates, maintaining set semantics.
Q.13Easy
Which LINQ method should you use to transform each element of a collection into a new form?
Answer: B
Select() is the LINQ method that projects each element to a new form, similar to map in functional programming languages.
Q.14Easy
Which method in Queue<T> allows you to examine the front element without removing it?
Answer: A
Peek() returns the element at the front of the queue without removing it. Dequeue() would remove it.
Q.15Easy
Which collection type uses a hash table internally and provides O(1) average lookup?
Answer: B
HashSet<T> uses a hash table internally, providing O(1) average case lookup, insert, and delete operations.
Q.16Easy
What does the Add() method return when adding a duplicate element to a HashSet<T>?
Answer: B
HashSet<T>.Add() returns false if the element already exists, true if added successfully. No exception is thrown.
Q.17Easy
What is the space complexity of a HashSet<T> with n elements?
Answer: B
HashSet<T> requires O(n) space to store n unique elements in the underlying hash table structure.
Q.18Easy
What does the Peek() method do in a Stack<T>?
Answer: B
Peek() returns the top element without modifying the stack. Pop() removes and returns it. Peek() throws InvalidOperationException if the stack is empty.