Home Subjects C# Programming

C# Programming

C# and .NET for campus placement

60 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 60
Topics in C# Programming
Q.11 Hard Collections
In a concurrent environment, which collection should be used instead of Dictionary in C# 2024-25?
A Hashtable
B ConcurrentDictionary
C SortedDictionary
D List
Correct Answer:  B. ConcurrentDictionary
EXPLANATION

ConcurrentDictionary<K,V> from System.Collections.Concurrent is thread-safe for multi-threaded applications, unlike Dictionary<K,V>.

Test
Q.12 Hard Collections
What is the difference between ICollection and IEnumerable in terms of capabilities?
A ICollection adds Count and Add/Remove/Clear methods
B Both are identical
C IEnumerable is a collection, ICollection is not
D ICollection cannot iterate, IEnumerable can
Correct Answer:  A. ICollection adds Count and Add/Remove/Clear methods
EXPLANATION

IEnumerable<T> only provides iteration. ICollection<T> extends it with Count property and methods like Add(), Remove(), Clear() for modification.

Test
Q.13 Hard Collections
Consider: var result = list.OrderByDescending(x => x.Age).ThenBy(x => x.Name); What does this do?
A Sorts by Age ascending then Name descending
B Sorts by Age descending then Name ascending
C Sorts by Name then Age
D Throws a compilation error
Correct Answer:  B. Sorts by Age descending then Name ascending
EXPLANATION

OrderByDescending sorts by Age in descending order. ThenBy applies secondary sort by Name in ascending order. This is a multi-level LINQ sort.

Test
Q.14 Hard Collections
What is the space complexity of a HashSet with n elements storing unique values?
A O(n)
B O(log n)
C O(1)
D O(n²)
Correct Answer:  A. O(n)
EXPLANATION

HashSet<T> requires O(n) space to store n unique elements. Hash tables require linear space proportional to the number of stored elements.

Test
Q.15 Hard Collections
In C# 2024-25, which LINQ method would you use to find all elements matching a condition in a List?
A FindAll()
B Where()
C Both A and B work
D Filter()
Correct Answer:  C. Both A and B work
EXPLANATION

Both FindAll() and Where() (LINQ) can filter collections. Where() returns IEnumerable and is more modern. FindAll() returns a List directly.

Test
Q.16 Hard OOP in C#
In a healthcare system, Doctor is a derived class from Employee. If you want to ensure that Doctor's constructor calls Employee's constructor before executing, which approach is correct?
A Use the 'base' keyword in Doctor's constructor initialization list
B Manually call Employee's constructor inside Doctor's constructor body
C Use the 'this' keyword to call parent constructor
D This is automatic and requires no explicit code
Correct Answer:  A. Use the 'base' keyword in Doctor's constructor initialization list
EXPLANATION

The 'base' keyword in the constructor initialization list ensures the parent class constructor is called before the derived class constructor body executes, maintaining proper initialization order.

Test
Q.17 Hard OOP in C#
Consider a scenario where class X implements interfaces IA and IB, and both interfaces have a method named Process(). How should X implement this?
A X must implement Process() twice with different names
B X can implement Process() once, satisfying both interfaces using explicit interface implementation if needed
C This will cause a compilation error and is not allowed
D X must choose which interface's Process() to implement
Correct Answer:  B. X can implement Process() once, satisfying both interfaces using explicit interface implementation if needed
EXPLANATION

C# allows one implementation to satisfy multiple interfaces when they have the same method signature. Explicit interface implementation can be used if methods need different behaviors.

Test
Q.18 Hard OOP in C#
In a real estate system, consider Property (base) with House and Apartment (derived). If Property has a sealed method CalculateTax(), what happens in House class?
A House can override CalculateTax()
B House cannot override CalculateTax() and must inherit it as-is
C House must reimplement CalculateTax() in a new virtual method
D Compilation error occurs
Correct Answer:  B. House cannot override CalculateTax() and must inherit it as-is
EXPLANATION

The sealed keyword on a virtual method prevents overriding in derived classes, ensuring that implementation is locked in the current class.

Test
Q.19 Hard OOP in C#
A financial system implements IEquatable for comparing accounts. What is the primary benefit of implementing this interface?
A It allows sorting accounts
B It provides type-safe equality comparison and optimizes collections
C It makes accounts serializable
D It allows accounts to be used as dictionary keys
Correct Answer:  B. It provides type-safe equality comparison and optimizes collections
EXPLANATION

IEquatable<T> provides type-safe equality comparison, improves performance by avoiding boxing, and is essential for proper behavior in collections like Dictionary.

Test
Q.20 Hard OOP in C#
In a complex system where a class needs to inherit from multiple sources of functionality while maintaining a single base type, which C# feature is most appropriate?
A Multiple inheritance
B Interface implementation combined with single inheritance
C Only interfaces without classes
D Static classes
Correct Answer:  B. Interface implementation combined with single inheritance
EXPLANATION

C# doesn't support multiple inheritance of classes, but allows one base class and multiple interfaces, providing flexibility while maintaining hierarchy.

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