Home Subjects C# Programming

C# Programming

C# and .NET for campus placement

154 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 31–40 of 154
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
Q.39 Medium OOP in C#
In a real-time inventory management system, a Product class needs to track stock levels that should not be modified directly from outside the class. Which OOP principle should be applied here, and how?
A Encapsulation - using private fields with public properties to control access
B Inheritance - creating a base Product class with derived classes for each product type
C Polymorphism - overriding methods in derived classes to change behavior
D Abstraction - using abstract classes to hide implementation details
Correct Answer:  A. Encapsulation - using private fields with public properties to control access
EXPLANATION

Encapsulation is the principle of bundling data and methods together while hiding internal details. Using private fields with public properties (getters/setters) allows controlled access to the stock level, preventing unauthorized direct modifications. This is the standard practice in inventory systems.

Test
Q.40 Medium OOP in C#
In a logistics system, both Truck and Bicycle need to calculate delivery cost. Instead of duplicating code, you decide to use an interface. What advantage does this provide?
A It enforces implementation of delivery cost calculation in both classes
B It automatically calculates delivery cost
C It restricts inheritance to one parent class only
D It provides default implementation for all methods
Correct Answer:  A. It enforces implementation of delivery cost calculation in both classes
EXPLANATION

Interfaces establish a contract that forces implementing classes to provide their own logic for calculating delivery costs, promoting code consistency without duplicating 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