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 31–38 of 38
Topics in C# Programming
Q.31 Medium Collections
Which collection should be used when you need sorted key-value pairs automatically?
A Dictionary
B SortedDictionary
C List
D Hashtable
Correct Answer:  B. SortedDictionary
EXPLANATION

SortedDictionary<K,V> automatically maintains keys in sorted order. Dictionary<K,V> maintains insertion order but not sorted order.

Test
Q.32 Medium Collections
Consider: Dictionary dict = new(); dict.Add("a", 1); dict.Add("a", 2); What happens?
A Value is updated to 2
B ArgumentException is thrown
C Both values are stored
D The second add is ignored
Correct Answer:  B. ArgumentException is thrown
EXPLANATION

Dictionary enforces unique keys. Attempting to add a duplicate key throws ArgumentException. Use dict["a"] = 2 to update.

Test
Q.33 Medium Collections
What is the difference between Stack.Push() and Stack.Pop()?
A Push adds, Pop removes and returns the element
B Pop adds, Push removes and returns the element
C Both are identical operations
D Push returns a value, Pop doesn't
Correct Answer:  A. Push adds, Pop removes and returns the element
EXPLANATION

Push() adds an element to the stack, while Pop() removes and returns the top element. If stack is empty, Pop throws InvalidOperationException.

Test
Q.34 Medium Collections
Which interface must be implemented to use a collection in a foreach loop?
A IEnumerable
B ICollection
C IList
D IComparable
Correct Answer:  A. IEnumerable
EXPLANATION

IEnumerable interface provides GetEnumerator() method, enabling foreach iteration. IEnumerable<T> is the generic version.

Test
Q.35 Medium Collections
What happens when you try to access an index beyond the Count in a List?
A Returns null
B Returns the last element
C Throws IndexOutOfRangeException
D Returns default value of type
Correct Answer:  C. Throws IndexOutOfRangeException
EXPLANATION

Accessing an invalid index throws IndexOutOfRangeException. This is a runtime error that must be caught to prevent program crashes.

Test
Q.36 Medium Collections
Which collection maintains insertion order but uses a hash table for faster lookups?
A Dictionary
B SortedDictionary
C LinkedHashMap (not in C#)
D Both A and B
Correct Answer:  A. Dictionary
EXPLANATION

Dictionary<K,V> in .NET maintains insertion order (in recent versions) and provides O(1) lookup. SortedDictionary maintains sorted order instead.

Test
Q.37 Medium Collections
What is the time complexity of accessing an element by index in a List?
A O(n)
B O(log n)
C O(1)
D O(n log n)
Correct Answer:  C. O(1)
EXPLANATION

List<T> is backed by an array, allowing direct index-based access in constant time O(1).

Test
Q.38 Medium Collections
Which method removes all elements from a collection in C#?
A Remove()
B RemoveAt()
C Clear()
D Delete()
Correct Answer:  C. Clear()
EXPLANATION

Clear() removes all elements from a collection. Remove() removes a specific element, and RemoveAt() removes an element at a specific index.

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