C# Mid-Level Interview Essentials Quiz Quiz

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.

  1. Structs vs Classes

    In C#, what is the main difference between a struct and a class when assigning one variable to another?

    1. A struct is copied by value, while a class is copied by reference.
    2. Both structs and classes are copied by reference.
    3. A class is copied by value, while a struct is copied by reference.
    4. Both structs and classes are copied by value.

    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.

  2. Handling Concurrency

    Which C# feature should you use to prevent multiple threads from accessing the same block of code at the same time?

    1. partial class
    2. delegate
    3. yield return
    4. lock statement

    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.

  3. Precision in Financial Calculations

    Why is the decimal type particularly important for financial calculations in C#?

    1. It can store text and numbers together.
    2. It uses less memory than int for currency data.
    3. It automatically encrypts currency values.
    4. It provides high precision and avoids rounding errors in monetary values.

    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.

  4. LINQ Querying

    In C#, what is LINQ primarily used for?

    1. Formatting date and time strings.
    2. Compressing image files.
    3. Querying collections and databases using syntax integrated into the C# language.
    4. Securing web applications from attacks.

    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.

  5. Suffix for Decimal Literals

    Which suffix do you add to a numeric value in C# to indicate it is a decimal type?

    1. l
    2. d
    3. f
    4. m

    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.

  6. Improving Transaction Performance

    Which method is NOT typically used to improve performance when processing large volumes of financial transactions in C#?

    1. Using parallel processing
    2. Adding unnecessary Memory allocations
    3. Caching frequently used data
    4. Employing efficient algorithms

    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.

  7. Thread-Safe Data Collections

    If you want a collection that is safe to use with multiple threads in C#, which should you consider?

    1. Concurrent collections
    2. StringBuilder
    3. Tuple
    4. Simple arrays

    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.

  8. Using the 'async' keyword

    What effect does applying the 'async' keyword to a method in C# have?

    1. It renames the method at runtime.
    2. It enables the method to be executed asynchronously.
    3. It makes the method immutable.
    4. It encrypts the method's contents automatically.

    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'.

  9. Choosing Between Struct and Class

    For representing a simple value like a currency amount in a financial app, which C# type is typically more appropriate?

    1. delegate
    2. interface
    3. struct
    4. class

    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.

  10. LINQ Query Example

    Given a list of transactions where each transaction has an Amount property, which LINQ method filters out transactions with Amount greater than 100?

    1. GroupBy
    2. OrderBy
    3. Where
    4. Select

    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.