Test your knowledge of key C# concepts for mid-level professionals with this quiz. Covering topics like value vs reference types, concurrency, decimal precision, LINQ, and optimizing performance, this quiz helps you prepare for C# interviews and sharpen your industry skills.
In C#, what is the main difference between a struct and a class when assigning one variable to another?
Explanation: Structs are value types, so assigning them copies their data. Classes are reference types, so assignment copies the reference, not the actual data. The second option reverses the concepts. The third and fourth options are incorrect because structs and classes behave differently regarding assignment semantics.
Which C# feature should you use to prevent multiple threads from accessing the same block of code at the same time?
Explanation: The lock statement is designed for thread synchronization, preventing race conditions by ensuring exclusive access. Delegates are used for type-safe method references, not concurrency. 'yield return' is for iterators, and 'partial class' splits class definitions across files. None of the distractors manage thread locking.
Why is the decimal type particularly important for financial calculations in C#?
Explanation: The decimal type was created for high-precision calculations such as currency, greatly reducing rounding errors common in floating-point types. It does not use less memory than int; in fact, it uses more. The decimal type cannot store text and numbers together, and it does not automatically handle encryption.
In C#, what is LINQ primarily used for?
Explanation: LINQ (Language Integrated Query) streamlines querying objects, collections, or databases directly in C#. It does not focus on web security, image compression, or date formatting. The other options are unrelated tasks not addressed by LINQ.
Which suffix do you add to a numeric value in C# to indicate it is a decimal type?
Explanation: The letter 'm' (or 'M') is required to denote a decimal literal in C#. 'd' stands for double, 'f' is for float, and 'l' is for long types. Choosing a different letter will either cause a compilation error or produce an unintended type.
Which method is NOT typically used to improve performance when processing large volumes of financial transactions in C#?
Explanation: Unnecessary memory allocations slow down applications by increasing memory overhead and garbage collection pressure. The other methods—parallel processing, efficient algorithms, and caching—are commonly recommended to optimize performance in transaction-heavy systems.
If you want a collection that is safe to use with multiple threads in C#, which should you consider?
Explanation: Concurrent collections in C# are specifically designed for safe multi-threaded access to shared data. Regular arrays are not thread-safe, StringBuilder is used for string manipulation, and Tuple groups values but does not provide thread safety.
What effect does applying the 'async' keyword to a method in C# have?
Explanation: Marking a method as 'async' in C# allows it to run asynchronously and often use the 'await' keyword. There is no automatic encryption, no effect on mutability, and no runtime renaming when using 'async'.
For representing a simple value like a currency amount in a financial app, which C# type is typically more appropriate?
Explanation: Structs are ideal for small, lightweight data structures such as currency amounts. Classes are more appropriate for complex objects. Interfaces and delegates are used for abstraction and method references, not for simple data representation.
Given a list of transactions where each transaction has an Amount property, which LINQ method filters out transactions with Amount greater than 100?
Explanation: The Where method is used to filter elements based on a condition such as Amount u003E 100. OrderBy sorts, Select projects properties or transforms data, and GroupBy groups items; none of these directly filter data as Where does.