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 21–30 of 38
Topics in C# Programming
Q.21 Medium Collections
What is the output of the following code?
var dict = new Dictionary();
dict[1] = "A";
dict[1] = "B";
Console.WriteLine(dict[1]);
A A
B B
C ArgumentException
D Null
Correct Answer:  B. B
EXPLANATION

Using the indexer on Dictionary with an existing key updates the value, not throws. Output is 'B'.

Test
Q.22 Medium Collections
In C# 2024-25, which collection should be used for fast membership testing with no duplicates?
A List
B HashSet
C SortedSet
D Queue
Correct Answer:  B. HashSet
EXPLANATION

HashSet<T> provides O(1) average lookup and prevents duplicates. SortedSet maintains order but slower. List<T> requires O(n) lookup.

Test
Q.23 Medium Collections
What is the time complexity for removing an element from the middle of a LinkedList?
A O(1)
B O(n)
C O(log n)
D O(n log n)
Correct Answer:  A. O(1)
EXPLANATION

If you have a LinkedListNode<T>, removal is O(1). However, finding the node is O(n), so overall search + remove is O(n).

Test
Q.24 Medium Collections
Which of the following collections preserves insertion order and is thread-safe?
A SortedDictionary
B ConcurrentDictionary
C Dictionary
D HashSet
Correct Answer:  B. ConcurrentDictionary
EXPLANATION

ConcurrentDictionary is thread-safe. While it doesn't strictly preserve insertion order like some collections, it's the thread-safe option among these choices.

Test
Q.25 Medium Collections
What does the Contains() method use in HashSet to determine membership?
A Sequential search
B Binary search
C Hash code and equality comparison
D Index-based lookup
Correct Answer:  C. Hash code and equality comparison
EXPLANATION

HashSet<T> uses hash codes for O(1) average lookup. It computes hash code first, then uses equality comparison.

Test
Q.26 Medium Collections
Which collection in C# is NOT thread-safe by default?
A ConcurrentDictionary
B ConcurrentBag
C List
D ConcurrentQueue
Correct Answer:  C. List
EXPLANATION

List<T> is not thread-safe. All Concurrent* collections (ConcurrentDictionary, ConcurrentBag, ConcurrentQueue) are thread-safe.

Test
Q.27 Medium Collections
What is the primary difference between SortedList and SortedDictionary?
A SortedList uses arrays internally; SortedDictionary uses a tree
B SortedDictionary allows faster insertion
C SortedList supports duplicate keys
D They have identical performance characteristics
Correct Answer:  A. SortedList uses arrays internally; SortedDictionary uses a tree
EXPLANATION

SortedList uses arrays (faster retrieval, slower insertion). SortedDictionary uses red-black tree (balanced insertion/deletion). SortedDictionary has better insertion time O(log n) vs O(n) for SortedList.

Test
Q.28 Medium Collections
Which LINQ method returns the first element matching a condition, or throws if none exists?
A First()
B FirstOrDefault()
C Find()
D Single()
Correct Answer:  A. First()
EXPLANATION

First() throws InvalidOperationException if no match. FirstOrDefault() returns null/default. Find() is a List<T> method.

Test
Q.29 Medium Collections
What happens when you add a duplicate key to a Dictionary?
A It adds both entries
B It throws ArgumentException
C It updates the existing key's value
D It ignores the new entry
Correct Answer:  B. It throws ArgumentException
EXPLANATION

Dictionary<TKey, TValue> throws ArgumentException when adding duplicate keys. Use indexer notation to update values.

Test
Q.30 Medium Collections
Which collection type is best suited for implementing a priority queue in C#?
A Stack
B PriorityQueue
C SortedDictionary
D LinkedList
Correct Answer:  B. PriorityQueue
EXPLANATION

PriorityQueue<T, TPriority> was introduced in .NET 6 specifically for priority queue implementation.

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