Quiz: C#
C# questions from real interviews as a quiz, with an explanation for every option.
Try it: 5 questions
Loading…
Sample questions
Which statements about searching in
List<T>andDictionary<TKey, TValue>in C# are correct?- Looking up a key in
Dictionary<TKey, TValue>always takes O(1), even when there are many collisions. Dictionary<TKey, TValue>searches for keys with binary search in a sorted array.- Searching for an element in
List<T>usually takes O(n). - A
Dictionaryhashes the key, selects a bucket, and resolves collisions by comparing keys. - Looking up a key in
Dictionary<TKey, TValue>takes O(1) on average.
- Looking up a key in
What does the
IEnumerableinterface provide in C#?- Index-based access to elements
- A guarantee that elements are loaded lazily
- Methods for adding and removing elements
- A way to iterate over elements using
GetEnumerator()
CalendarService.IsWorkingDay()checksDateTime.Now.DayOfWeek, but the test always expectstrue. What is the main problem with the test, and how can it be made reliable?- It checks only the result; replace
Assert.TruewithAssert.Equal(true, actual). - The service is created incorrectly; create a new
CalendarServicefor each day of the week. - The method should return
trueevery day, so remove the weekend check from the service. - It can fail on weekends; control the time source and test multiple days of the week.
- It checks only the result; replace
How does the
Systemnamespace relate to .NET assemblies?- Each of its types must reside in a separate DLL.
- It is a logical grouping; its types can reside in multiple assemblies.
- It is always implemented entirely in one DLL.
- It combines types from only one source assembly.
How do the
try,catch, andfinallyblocks work in C#?trycontains code that may fail,catchhandles a matching exception, andfinallyruns aftertryandcatchwhether or not an exception occurred.trycontains code,catchruns only when no exception occurs, andfinallyruns only when one does.trychecks code for errors before execution,catchfixes any errors it finds, andfinallyruns the code again.tryhandles exceptions,catchalways runs after it, andfinallyruns only if no exception occurred.
Which statements correctly explain why BULK INSERT can load data faster than ordinary row-by-row inserts? Select all that apply.
- BULK INSERT completely disables transaction logging.
- When the requirements are met, SQL Server can use minimal logging.
- BULK INSERT always disables the table's indexes, so it does not spend time maintaining them.
- Loading large batches and deferring index maintenance until after the load can reduce overhead.
- Under suitable conditions, such as with TABLOCK, SQL Server can perform the load in parallel.
Can C# value types be allocated on the managed heap?
- Yes, but only if the struct exceeds a certain size limit.
- No, all value types are always allocated on the stack.
- Yes, but only when boxed; they cannot be fields of class objects.
- Yes, for example, as a field of a class object or when boxed.
In C#, what happens when you assign a value using a key that is already present in a
Dictionary?- A new key is created with its value changed automatically.
- The value for that key is replaced.
- A second entry with the same key is added.
- The operation always throws an exception.
What does RPS measure, and what can happen if a system cannot keep up as it rises?
- Requests handled per second; latency may rise and requests may be lost.
- The size of the database; an increase means more disk space is needed.
- Average response time; as RPS rises, latency always decreases.
- The number of concurrent users; a high RPS guarantees there is no overload.
What does the Unit of Work pattern do, and which category is it usually associated with?
- Creates an object with the required configuration; it is a GoF creational pattern.
- Provides a unified interface to a complex subsystem; it is a GoF structural pattern.
- Tracks changes within an operation and saves them together; it is an Enterprise Application Architecture pattern.
- Synchronizes concurrent threads accessing data; it is a concurrency pattern.