Home Subjects C# Programming Collections

C# Programming
Collections

C# and .NET for campus placement

38 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 1–10 of 38
Topics in C# Programming
Q.1 Medium Collections
In a scenario with frequent additions and removals at both ends, which collection is most efficient?
A List
B LinkedList
C Queue
D Stack
Correct Answer:  B. LinkedList
EXPLANATION

LinkedList<T> provides O(1) AddFirst(), AddLast(), RemoveFirst(), and RemoveLast() operations. List<T> requires O(n) for head removals due to reindexing.

Test
Q.2 Medium Collections
What is the time complexity of Contains() method in a HashSet?
A O(n)
B O(log n)
C O(1) average case
D O(n²)
Correct Answer:  C. O(1) average case
EXPLANATION

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.

Test
Q.3 Medium Collections
Which collection maintains elements in sorted order and is backed by a binary search tree?
A SortedList
B SortedDictionary
C SortedSet
D TreeMap
Correct Answer:  B. SortedDictionary
EXPLANATION

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.

Test
Q.4 Medium Collections
In a data processing pipeline, if you need FIFO (First-In-First-Out) semantics with O(1) enqueue/dequeue, which collection is best?
A Stack
B LinkedList
C Queue
D SortedList
Correct Answer:  C. Queue
EXPLANATION

Queue<T> provides FIFO semantics with O(1) Enqueue() and Dequeue() operations. Stack<T> is LIFO, and LinkedList operations vary.

Test
Q.5 Medium Collections
Which LINQ method is used to convert a Dictionary to a List of key-value pairs?
A ToArray()
B ToList()
C AsEnumerable()
D Cast()
Correct Answer:  B. ToList()
EXPLANATION

ToList() converts the Dictionary's KeyValuePair<K,V> enumeration into a List<KeyValuePair<K,V>>. ToArray() would create an array instead.

Test
Q.6 Medium Collections
What happens when you modify a List while iterating over it using a foreach loop?
A It works without issues
B It throws InvalidOperationException
C It silently skips elements
D It doubles the iteration time
Correct Answer:  B. It throws InvalidOperationException
EXPLANATION

Modifying a collection during enumeration invalidates the enumerator and throws InvalidOperationException. Use a for loop or ToList() to avoid this.

Test
Q.7 Medium Collections
In a multiprocessing environment, which thread-safe collection should be used for concurrent operations?
A List
B ConcurrentBag
C HashSet
D Dictionary
Correct Answer:  B. ConcurrentBag
EXPLANATION

ConcurrentBag<T> and other Concurrent* collections from System.Collections.Concurrent are thread-safe for multi-threaded scenarios without explicit locking.

Test
Q.8 Medium Collections
What is the primary difference between ArrayList and List?
A ArrayList is faster
B List is type-safe and more performant
C ArrayList uses generics
D No difference in modern C#
Correct Answer:  B. List is type-safe and more performant
EXPLANATION

List<T> is strongly-typed (type-safe) and eliminates boxing/unboxing overhead. ArrayList is non-generic and slower due to boxing of value types.

Test
Q.9 Medium Collections
Which method should be used to safely check and retrieve a value from a Dictionary without throwing an exception?
A Get()
B TryGetValue()
C ContainsKey() followed by indexer
D Retrieve()
Correct Answer:  B. TryGetValue()
EXPLANATION

TryGetValue() is the safe method that returns a boolean indicating success and outputs the value without throwing KeyNotFoundException.

Test
Q.10 Medium Collections
In a scenario where you need to store key-value pairs with fast lookup times, which collection is optimal?
A List
B Dictionary
C Queue
D LinkedList
Correct Answer:  B. Dictionary
EXPLANATION

Dictionary<K,V> provides O(1) average-case lookup time using hash tables, making it optimal for key-value pair storage with fast retrieval.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips