This set of 30 multiple-choice questions (MCQs) covers key concepts in C# functions and methods, including method declaration, parameters, overloading, recursion, and the use of optional parameters and named arguments.
Chapter 5: Functions and Methods in C# – MCQs
1. Method Declaration and Definition
Which of the following is the correct syntax for declaring a method in C#?
a) void MethodName();
b) void MethodName() { }
c) Method void Name() { }
d) MethodName void() { }
What is the correct way to define a method that returns an integer in C#?
a) int MethodName() { }
b) MethodName int() { }
c) void MethodName(int result) { }
d) int MethodName { }
What is the purpose of a method signature in C#?
a) It includes the method’s name, return type, and parameters.
b) It defines the return type of the method only.
c) It defines the visibility of the method.
d) It specifies the method’s access modifiers only.
Which access modifier makes a method accessible only within its own class in C#?
a) private
b) public
c) protected
d) internal
In C#, which keyword is used to declare a method that does not return any value?
a) void
b) static
c) return
d) out
Which of the following correctly declares a static method in C#?
a) void static MethodName() { }
b) static void MethodName() { }
c) MethodName static void() { }
d) MethodName() static void { }
What happens if a method in C# does not have a return type?
a) It results in a compile-time error.
b) It defaults to returning an integer.
c) It returns null by default.
d) It is considered a constructor.
2. Method Parameters and Return Types
What is a parameter in C# method?
a) A variable passed into a method to be used inside the method.
b) A variable that holds the return value of a method.
c) A predefined value used inside the method.
d) A constant value passed from outside the method.
Which type of parameter allows a method to modify its argument value in C#?
a) out
b) ref
c) in
d) readonly
What is the correct way to declare a method that takes an integer parameter and returns a string in C#?
a) string MethodName(int value);
b) MethodName(int value) -> string;
c) int MethodName(string value);
d) void MethodName(int value) => string;
What is the default value for method parameters in C# if they are not explicitly passed?
a) 0
b) null
c) undefined
d) It depends on the type of the parameter.
Which keyword is used to pass a parameter by reference in C#?
a) ref
b) out
c) in
d) readonly
What is the correct way to declare a method that returns a value of type double in C#?
a) MethodName(): double { }
b) MethodName() { return 5.5; }
c) double MethodName() { return 5.5; }
d) double return MethodName() { }
Which of the following statements is true about return in C# methods?
a) A method can return only one value.
b) A method must return a value of the same type as the method’s return type.
c) A method cannot return values.
d) A method can return multiple values by using out parameters.
3. Method Overloading
What is method overloading in C#?
a) Using the same method name with different parameter types or numbers.
b) Defining a method with a fixed number of parameters.
c) Calling a method from another class.
d) Changing the method’s return type.
Which of the following is an example of method overloading in C#?
a) void MethodName(int x) { } and void MethodName(string y) { }
b) void MethodName() { } and void MethodName(int x) { }
c) Both (a) and (b)
d) void MethodName() { } and int MethodName() { }
In C#, what happens if you overload a method with identical parameter types but different return types?
a) It is allowed, and the method will work based on return type.
b) It results in a compile-time error.
c) The compiler chooses one method based on its return type.
d) The method will always return the first method’s type.
How does C# differentiate between overloaded methods?
a) By the method’s return type.
b) By the number or types of parameters.
c) By the access modifiers.
d) By the visibility scope.
Can a method be overloaded based on the return type alone in C#?
a) Yes
b) No
c) Only if parameters are different
d) It depends on the method’s body
4. Recursive Methods
What is recursion in C#?
a) When a method calls itself.
b) When a method is executed iteratively.
c) When a method uses a loop.
d) When a method is called from another method.
What is the base case in a recursive method?
a) The part of the code that ensures the recursion stops.
b) The part of the code that continues recursion indefinitely.
c) The first recursive call.
d) The return value of the recursion.
Which of the following is true about recursive methods in C#?
a) Recursion is faster than iteration.
b) Recursion can lead to stack overflow if not properly defined.
c) Recursive methods cannot return values.
d) Recursive methods must have an infinite number of parameters.
What is the primary disadvantage of using recursion in C#?
a) It consumes more memory due to function calls being pushed onto the stack.
b) It leads to compile-time errors.
c) It makes the code more readable.
d) It is always slower than iteration.
Which of the following is an example of a recursive method in C#?
a) public int Factorial(int n) { if (n == 0) return 1; return n * Factorial(n - 1); }
b) public int Factorial(int n) { for (int i = 1; i <= n; i++) { result *= i; } return result; }
c) public int Factorial(int n) { return n * n; }
d) public int Factorial() { return 1; }
How do you prevent a stack overflow in recursive methods?
a) By using an iterative solution instead of recursion.
b) By ensuring there is a proper base case.
c) By using dynamic memory allocation.
d) By increasing the method’s stack size.
5. Optional Parameters and Named Arguments
How do you declare a method with optional parameters in C#?
a) void MethodName(int x = 10) { }
b) void MethodName(int x) { }
c) void MethodName(int x) => 10;
d) void MethodName(int x = null) { }
What happens if a method is called with missing optional parameters in C#?
a) The method will use the default values for the optional parameters.
b) A compile-time error occurs.
c) The method cannot be called without all parameters.
d) The missing parameters are automatically set to null.
Which of the following is true about named arguments in C#?
a) Named arguments must be specified in the order they are declared.
b) Named arguments allow parameters to be passed in any order.
c) Named arguments cannot be used with optional parameters.
d) Named arguments are only allowed for methods with a single parameter.
How would you call a method with named arguments in C#?
a) MethodName(x: 10, y: 20);
b) MethodName(10, 20);
c) MethodName(x, y);
d) MethodName(10 => x, 20 => y);
Can a method in C# have both optional parameters and named arguments?
a) Yes
b) No
c) Only one or the other
d) It depends on the compiler version
Answer Key
Qno
Answer
1
b) void MethodName() { }
2
a) int MethodName() { }
3
a) It includes the method’s name, return type, and parameters.
4
a) private
5
a) void
6
b) static void MethodName() { }
7
a) It results in a compile-time error.
8
a) A variable passed into a method to be used inside the method.
9
b) ref
10
a) string MethodName(int value);
11
d) It depends on the type of the parameter.
12
a) ref
13
c) double MethodName() { return 5.5; }
14
b) A method must return a value of the same type as the method’s return type.
15
a) Using the same method name with different parameter types or numbers.
16
c) Both (a) and (b)
17
b) It results in a compile-time error.
18
b) By the number or types of parameters.
19
b) No
20
a) When a method calls itself.
21
a) The part of the code that ensures the recursion stops.
22
b) Recursion can lead to stack overflow if not properly defined.
23
a) It consumes more memory due to function calls being pushed onto the stack.
24
a) public int Factorial(int n) { if (n == 0) return 1; return n * Factorial(n - 1); }
25
b) By ensuring there is a proper base case.
26
a) void MethodName(int x = 10) { }
27
a) The method will use the default values for the optional parameters.
28
b) Named arguments allow parameters to be passed in any order.