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.62Easy
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.63Easy
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).
Q.64Easy
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.65Easy
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.
Advertisement
Q.66Easy
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.67Easy
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.68Easy
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.69Easy
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.70Easy
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.71Easy
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.72Easy
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.73Easy
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.74Easy
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.75Easy
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.76Easy
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.