You've built your toolbox, mastered parameters, overloading, and advanced passing techniques. Now it's time to prove your expertise. These 15 carefully crafted questions cover the kind of deep method knowledge that separates junior developers from architects. Topics span method signatures, parameter passing, overloading, extension methods, local functions, expression-bodied members, and more.
Take your time. Think like a compiler. Choose wisely, Architect!
📋 Instructions
🧠 Quiz Time
0 / 15 answered
1
What is the difference between a method signature and a method definition in C#?
A. A method signature includes the return type, name, and body; a definition is just the name
B. A method signature consists of the method name and its parameter list (types and order); a definition includes the signature plus the return type and body
C. They are identical — both terms mean the same thing
D. A method signature includes the return type; a definition does not
In C#, the method signature is specifically the method name and the parameter list (number, types, and order of parameters). The return type is NOT part of the signature (which is why you can't overload by return type alone). A method definition includes the signature plus the return type, access modifier, and the method body.
2
Which statement correctly describes the difference between `ref` and `out` parameters?
A. `ref` parameters must be assigned inside the method; `out` parameters must be initialized before passing
B. `ref` parameters must be initialized before passing; `out` parameters must be assigned inside the method before it returns
C. Both `ref` and `out` require the variable to be initialized before passing
D. `ref` passes by value; `out` passes by reference
A `ref` parameter requires the variable to be definitely assigned (initialized) before being passed to the method. An `out` parameter does NOT require prior initialization, but the method MUST assign a value to it before the method returns. Both pass the variable by reference.
3
What happens if you try to modify an `in` parameter inside a method?
A. The original variable is modified
B. A copy is modified, leaving the original unchanged
C. A compile-time error occurs — `in` parameters are read-only references
D. A runtime exception is thrown
The `in` modifier passes a parameter by read-only reference. Any attempt to assign a new value to an `in` parameter inside the method results in a compile-time error (CS8331). It's designed to avoid the cost of copying large structs while preventing modification.
4
Which rule about the `params` keyword is correct?
A. A method can have multiple `params` parameters if they are of different types
B. `params` can appear anywhere in the parameter list
C. `params` must be the last parameter and only one `params` parameter is allowed per method
D. `params` only works with `string[]` arrays
The `params` keyword must be applied to the LAST parameter of a method, and only ONE `params` parameter is allowed per method. It can be used with any array type (int[], string[], object[], etc.), not just string[]. Placing it before other parameters or using multiple `params` causes a compile-time error.
5
Given these overloads, which one is called for `Calculate(5, 3.0)`?
```csharp
static int Calculate(int a, int b) => a + b; // A
static double Calculate(double a, double b) => a * b; // B
static double Calculate(int a, double b) => a - b; // C
```
A. Overload A — `Calculate(int, int)`
B. Overload B — `Calculate(double, double)`
C. Overload C — `Calculate(int, double)`
D. Compile error — the call is ambiguous
The compiler performs overload resolution by finding the best match. The arguments are `int` and `double`. Overload C with `(int, double)` is an exact match — no type conversion needed. Overload A would require converting `3.0` to `int` (narrowing, not implicit). Overload B would require widening `5` to `double`, which is possible but C is a better match.
6
Why can't you overload methods by changing ONLY the return type?
A. Because the CLR cannot differentiate return types at runtime
B. Because the return type is not part of the method signature in C#, so the compiler cannot distinguish the overloads at the call site
C. Because C# doesn't support different return types for methods
D. Because it would cause a runtime stack overflow
In C#, the method signature consists of the method name and parameter list (types and order). The return type is NOT part of the signature. Since the compiler resolves overloads based on the signature, two methods with identical names and parameter lists but different return types are indistinguishable at the call site — the compiler wouldn't know which one to call.
7
What is the key difference between a static method and an instance method?
A. Static methods are faster; instance methods are slower
B. Static methods belong to the class itself and are called on the type; instance methods belong to an object and require an instance to call
C. Static methods can't have parameters; instance methods can
D. Instance methods are always private; static methods are always public
A static method belongs to the class and is invoked on the type name (e.g., `Math.Sqrt()`). It cannot access instance members or `this`. An instance method belongs to a specific object and is called on an instance (e.g., `myString.ToUpper()`). It can access both instance and static members.
8
What is an expression-bodied method in C#?
A. A method that only works with mathematical expressions
B. A shorthand syntax using `=>` for methods whose body is a single expression
C. A method that returns an `Expression<T>` object
D. A method declared inside a LINQ query
Expression-bodied members (introduced in C# 6 for methods, expanded in C# 7) use the `=>` syntax to write concise single-expression method bodies. For example: `static int Square(int x) => x * x;` is equivalent to `static int Square(int x) { return x * x; }`. It works for both returning and void methods.
9
What is a local function in C# 7+, and how does it differ from a lambda?
A. A local function is a named method defined inside another method; unlike lambdas it supports recursion naturally and doesn't allocate heap memory for captures in many cases
B. A local function is the same as a lambda but with a different syntax
C. Local functions must be static and cannot access outer variables
D. Local functions are defined at the namespace level, outside any class
Local functions are methods declared inside another method (C# 7+). They can access variables from the enclosing method, support recursion without delegates, can use `ref`/`out`/`params`, and in many cases avoid heap allocation that lambdas incur (since lambdas may create closure objects). They're ideal for helper logic that only one method needs.
10
What will the following recursive method return for `Factorial(5)`?
```csharp
static int Factorial(int n)
{
if (n <= 1) return 1;
return n * Factorial(n - 1);
}
```
What is an extension method in C#, and what is required to define one?
A. A method that extends the runtime; it must be defined in a partial class
B. A static method in a static class whose first parameter uses the `this` keyword, allowing it to be called as if it were an instance method on that type
C. Any method that overrides a base class method
D. A method marked with the `[Extension]` attribute in any class
An extension method is a static method in a static class where the first parameter is preceded by `this` (e.g., `static int WordCount(this string str)`). This allows calling it like an instance method: `"hello world".WordCount()`. The containing class must be non-generic and static. Extension methods are heavily used in LINQ.
12
When should you prefer optional parameters over method overloading?
A. Always — optional parameters are superior in every case
B. When the overloads differ only in that some parameters have sensible defaults and the logic is identical across variants
C. Never — overloading is always better
D. Only when working with interfaces
Optional parameters are ideal when multiple overloads would share identical logic and differ only in providing default values. They reduce code duplication. However, overloading is better when different parameter combinations require different logic, different types, or when you need binary compatibility (changing a default value in optional params requires recompilation of callers).
13
What does the `new` keyword do when used on a method in a derived class (method hiding)?
A. It creates a new instance of the method
B. It hides the base class method with the same signature; the base version is still called when using a base-type reference
C. It overrides the base class method like `override`
D. It prevents the method from being called
The `new` keyword on a derived class method hides (shadows) the base class method rather than overriding it. Unlike `override`, method hiding is NOT polymorphic: if you call the method through a base-type reference, the base version runs. If you call through the derived-type reference, the new version runs. The `new` keyword explicitly suppresses the compiler warning about hiding.
14
What are Caller Info attributes in C#, and which method parameter attribute provides the caller's method name?
A. `[CallerFilePath]` provides the method name
B. `[CallerMemberName]` provides the name of the calling method or property
C. `[CallerLineNumber]` provides the method name as a line number
D. `[CallerIdentity]` provides the method name and class
Caller Info attributes (in `System.Runtime.CompilerServices`) automatically inject call-site information. `[CallerMemberName]` gives the calling method/property name, `[CallerFilePath]` gives the source file path, and `[CallerLineNumber]` gives the line number. They must be applied to optional `string`/`int` parameters. They're commonly used in `INotifyPropertyChanged` implementations and logging.
15
In C#, when you pass a reference type (like an array) to a method WITHOUT `ref`, and the method modifies the array's elements, what happens to the original array?
A. Nothing — a complete copy of the array is passed, so the original is unchanged
B. The original array's elements ARE modified because the reference (pointer) is copied, not the object — both references point to the same array on the heap
C. A compile error occurs — arrays must always be passed with `ref`
D. The original array is replaced with a new array
For reference types, 'pass by value' means the REFERENCE (pointer) is copied, not the object. The method receives a copy of the reference that points to the same heap object. Modifying elements through this reference modifies the original object. However, reassigning the parameter to a new object does NOT affect the caller's reference — for that, you'd need `ref`. This is a crucial distinction in C# interviews.