C# Methods MCQ

1. How do you define a method in C#?

a) dataType methodName(parameters) { /* code */ }
b) method dataType methodName(parameters) { /* code */ }
c) dataType methodName = (parameters) { /* code */ }
d) function dataType methodName(parameters) { /* code */ }

Answer:

a) dataType methodName(parameters) { /* code */ }

Explanation:

A method in C# is defined with a return type, followed by the method name and parameters within parentheses.

2. What is the return type of a method that does not return any value?

a) void
b) null
c) None
d) Empty

Answer:

a) void

Explanation:

In C#, the 'void' keyword is used as the return type for methods that do not return a value.

3. How can you call a method in C#?

a) methodName.call(parameters);
b) methodName(parameters);
c) Call methodName(parameters);
d) Execute methodName(parameters);

Answer:

b) methodName(parameters);

Explanation:

Methods are called by specifying the method name followed by parentheses containing any arguments.

4. What are parameters in a C# method?

a) Variables passed into the method
b) The return values of the method
c) Internal variables of the method
d) External requirements for the method

Answer:

a) Variables passed into the method

Explanation:

Parameters are variables that are passed into a method to provide input for its execution.

5. Which keyword is used to indicate that a method parameter is optional in C#?

a) optional
b) default
c) params
d) OptionalParameter

Answer:

c) params

Explanation:

The 'params' keyword in C# is used to specify that a method parameter is optional.

6. What is method overloading in C#?

a) Changing the return type of a method
b) Creating a method with the same name but different parameters
c) Increasing the efficiency of a method
d) Writing the method code in multiple languages

Answer:

b) Creating a method with the same name but different parameters

Explanation:

Method overloading in C# refers to creating multiple methods with the same name but different parameters.

7. What is the correct way to declare a method that returns an integer in C#?

a) int methodName() { /* code */ }
b) method int methodName() { /* code */ }
c) function int methodName() { /* code */ }
d) methodName() : int { /* code */ }

Answer:

a) int methodName() { /* code */ }

Explanation:

The return type (int) precedes the method name in C# method declarations.

8. How are arguments passed to a method by default in C#?

a) By reference
b) By value
c) By pointer
d) By object

Answer:

b) By value

Explanation:

In C#, arguments are passed to methods by value by default, meaning a copy of the value is passed.

9. What keyword is used to define a method that belongs to a class rather than an instance of a class in C#?

a) static
b) const
c) class
d) shared

Answer:

a) static

Explanation:

The 'static' keyword is used to define a method that belongs to a class itself rather than any instance of the class.

10. How can you return multiple values from a method in C#?

a) Using a struct or class
b) Returning an array or a list
c) Using out or ref parameters
d) All of the above

Answer:

d) All of the above

Explanation:

Multiple values can be returned from a C# method using a struct/class, an array/list, or out/ref parameters.

11. What does the 'ref' keyword do in a C# method signature?

a) Specifies a reference type parameter
b) Indicates that a parameter is optional
c) Allows the method to modify the argument's value
d) Signals a recursive method

Answer:

c) Allows the method to modify the argument's value

Explanation:

The 'ref' keyword allows a method to modify the value of the argument passed to it.

12. What is recursion in C#?

a) A method that calls itself
b) Repeating a method multiple times
c) Looping through method calls
d) Overloading a method

Answer:

a) A method that calls itself

Explanation:

Recursion is a programming technique where a method calls itself.

13. What is a constructor in C#?

a) A method that constructs data types
b) A static method that initializes a class
c) A special method used to initialize objects
d) A method that is called when an object is destroyed

Answer:

c) A special method used to initialize objects

Explanation:

A constructor is a special method in a class that is called when an instance of the class is created, used to initialize objects.

14. Can a C# method return null?

a) Yes, if the method's return type is a reference type or a nullable value type
b) No, methods in C# cannot return null
c) Yes, but only for string return types
d) No, unless explicitly allowed by the 'null' keyword

Answer:

a) Yes, if the method's return type is a reference type or a nullable value type

Explanation:

In C#, a method can return null if its return type is either a reference type or a nullable value type.

15. What is the difference between 'ref' and 'out' parameters in C#?

a) 'ref' parameters must be initialized before being passed; 'out' parameters do not
b) 'out' parameters can be passed by value
c) 'ref' parameters are for input only; 'out' parameters are for output only
d) There is no difference

Answer:

a) 'ref' parameters must be initialized before being passed; 'out' parameters do not

Explanation:

'ref' parameters require initialization before being passed to a method, whereas 'out' parameters do not require initialization and are intended to return a value.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top