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.
What happens when you assign an int variable to an object variable in C# (e.g., object obj = 5;)?
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.
Which of the following is a valid method overload for public void Show(int x)?
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'.
Which declaration allows an integer variable to store null values for optional data in C#?
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.
Where should you place cleanup code, such as closing a file, to make sure it runs regardless of success or exception?
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.
When you concatenate two strings in C#, such as s1 + s2, what happens to the original strings?
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.
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)?
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#.
Which of these statements about static classes in C# is correct?
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.
Which feature differentiates an interface from an abstract class in C#?
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.
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?
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.
Given double d = 10.5; int i = (int)d;, what value does i receive after the cast?
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.