C# Interview Prep: Tricky Questions and Real-World Scenarios Quiz Quiz

Sharpen your C# knowledge with this quiz focused on tricky interview questions and real-world scenarios. Test your understanding of key C# concepts, syntax, best practices, and problem-solving techniques commonly assessed in interviews.

  1. Boxing Values

    What happens when you assign an int variable to an object variable in C# (e.g., object obj = 5;)?

    1. The int is converted to string automatically.
    2. Boxing occurs, storing a value type as an object.
    3. A syntax error is thrown.
    4. Unboxing, converting object to value type, occurs.

    Explanation: Boxing is the process of converting a value type (like int) into an object in C#, which happens in the assignment shown. Unboxing is the reverse process, which does not occur here. There is no syntax error in this code, and an int is not converted to a string automatically in this scenario.

  2. Method Overloading

    Which of the following is a valid method overload for public void Show(int x)?

    1. public void Show(double x)
    2. public int Show(int x)
    3. public void show(int x)
    4. public void Show(int y)

    Explanation: A valid overload changes the parameter type, such as using double instead of int. Changing the return type or just the parameter name does not create a valid overload. Method names are case-sensitive, so 'show' is not considered an overload of 'Show'.

  3. Nullable Types

    Which declaration allows an integer variable to store null values for optional data in C#?

    1. nullableu003Cintu003E age();
    2. int age = null;
    3. optional int age;
    4. int? age;

    Explanation: Using int? makes a nullable integer, enabling it to hold either an integer value or null. 'optional int' is not valid C# syntax, and 'int age = null;' will result in an error because int by itself cannot be null. 'nullableu003Cintu003E age();' is incorrect syntax for variable declaration.

  4. Exception Handling

    Where should you place cleanup code, such as closing a file, to make sure it runs regardless of success or exception?

    1. Only in the catch block
    2. Only in the try block
    3. In the finally block
    4. Right before the try block

    Explanation: The finally block is executed after try and catch blocks, regardless of whether an exception occurred. Placing cleanup code only in the catch or try block could result in missed cleanup if an exception happens. Putting it before the try block makes no sense, as the file is not yet open.

  5. String Immutability

    When you concatenate two strings in C#, such as s1 + s2, what happens to the original strings?

    1. The first string gets overwritten.
    2. They remain unchanged because strings are immutable.
    3. The original strings are cleared from memory.
    4. Both strings are joined by reference.

    Explanation: Strings in C# are immutable, meaning they cannot be changed after creation; concatenation creates a new string. The original strings are not joined by reference. Neither string is cleared from memory immediately, and no string is overwritten.

  6. Index Out of Range

    What exception is thrown when you access an array element at an invalid index in C# (e.g., arr[10] in a 5-element array)?

    1. ArgumentException
    2. IndexOutOfRangeException
    3. NullReferenceException
    4. ArrayBoundsException

    Explanation: Trying to access an invalid index in an array triggers an IndexOutOfRangeException in C#. NullReferenceException occurs when dereferencing a null variable. ArgumentException is used for invalid arguments, not array bounds. ArrayBoundsException does not exist in C#.

  7. Static Class Characteristics

    Which of these statements about static classes in C# is correct?

    1. Static classes can be used as base classes.
    2. Static classes must always implement an interface.
    3. Static classes cannot be instantiated or inherited.
    4. Static classes can be instantiated but not inherited.

    Explanation: Static classes in C# cannot be instantiated or inherited, ensuring they only provide static members and methods. They cannot be instantiated like non-static classes. Implementing interfaces is not required, and static classes cannot serve as base classes for inheritance.

  8. Interfaces vs Abstract Classes

    Which feature differentiates an interface from an abstract class in C#?

    1. Abstract classes cannot be partially implemented.
    2. Interfaces can contain method implementations only.
    3. Interfaces cannot contain fields, while abstract classes can.
    4. Interfaces can have constructors, but abstract classes cannot.

    Explanation: Interfaces define contracts and do not contain fields, whereas abstract classes can contain both fields and methods with or without implementation. Interfaces cannot have constructors. Abstract classes can have partial implementation, contradicting the third option. Interfaces cannot generally contain method bodies unless using special syntax, which is not allowed in older versions.

  9. LINQ Query Result

    If you run the query var result = numbers.Where(n =u003E n u003E 5); on an int[] numbers = {2, 4, 6, 8}, what does result contain?

    1. All elements: 2, 4, 6, 8
    2. Only the first element greater than 5: 6
    3. All elements greater than 5: 6, 8
    4. An error due to unexpected syntax

    Explanation: The Where method filters the array to include only numbers greater than 5, so result will contain 6 and 8. It does not return all elements, nor does it return only the first matching element. There is no syntax error in this usage.

  10. Implicit vs Explicit Casting

    Given double d = 10.5; int i = (int)d;, what value does i receive after the cast?

    1. 10.5
    2. 11
    3. 10
    4. Compilation error

    Explanation: Casting a double to an int in C# truncates the decimal portion, so i becomes 10. It does not round up to 11 or keep the decimal as 10.5. The cast is allowed, so no compilation error occurs.