Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Methods Aptitude Questions and Answers
C# Methods Aptitude Questions: This section contains aptitude questions and answers on C# methods.
Submitted by Nidhi, on March 24, 2020
1) There are following statements are given below, which of them are correct about methods in C#?
- Methods are used for the reusability of code.
- The ref keyword is used to implement pass by reference.
- Methods cannot be declared in the class.
- In C #, methods cannot call recursively.
Options:
- Only A
- Only B
- A and B
- C and D
Correct answer: 3
A and B
We can reuse the same functionality multiple times by creating methods. We have passed by value and pass by reference both mechanisms for parameter passing.
2) How many values a method can return in C#?
- Any number of values
- Only 1 value
- Depends on the argument passed
- Depends on class
Correct answer: 2
Only 1 value
The maximum 1 value can be returned from methods in C#.
3) If the return type of method is "void" then what is returns?
- It returns void type values
- It does not return any value
- "void" is not the valid return type
- It returns three string values
Correct answer: 2
It does not return any value
If we declare a method with return type "void", it means we do not want to return any value form method.
4) There are following options are given below, which of them CANNOT occur more than one time in a C# program?
- Program entry point
- Namespace
- Class
- Methods
Correct answer: 1
Program entry point
The program entry point can only occur one time in C# programs, other options can occur multiple times.
5) What kind of arguments we used to get updated value from methods?
- ref
- out
- default argument
- actual argument
Options:
- Only C
- Only D
- A and B
- Only A
Correct answer: 3
A and B
In C#, ref and out both types of arguments are used to get updated value from a method.
6) Is it possible to have arguments with default values in C# methods?
- Yes
- No
Correct answer: 1
Yes
Yes, it is possible to have arguments with default values in methods.
7) Is it required the arguments should NOT initialize before it pass to ref in the method?
- Yes
- No
Correct answer: 2
No
When we use ref keyword to pass arguments to the method, it is mandatory to initialize before it passes to the method.
8) In the case of "out" parameters, is it required the arguments should initialize before returning to the calling methods?
- Yes
- No
Correct answer: 1
Yes
Yes, it is mandatory that the arguments should initialize before returning to the calling methods.
9) The optional or default argument must be placed at the end of the argument list in the method.
- Yes
- No
Correct answer: 1
Yes
The default argument must be placed at the end of the argument list in the method.
10) There are following statements are given below, which of them are correct about the static method?
- Static methods are called by class name.
- The static keyword is not required to create a static method.
- A non-Static method cannot be called a static method.
- Static methods can never be public.
Options:
- Only A
- Only C
- Only D
- A and C
Correct answer: 4
A and C
Statement A and C are correct about static methods.
11) There are the following keywords that are given below, which of them are NOT access specifiers in C#?
- Static
- Public
- Protected
- Internal
Options:
- Only A
- Only D
- A and D
- B and C
Correct answer: 1
Only A
In C#, there are following access specifiers are available:
- Public
- Private
- Protected
- Internal
- Protected internal
12) Can be access to private methods outside the class?
- Yes
- No
Correct answer: 2
No
No, private methods can never be accessed outside the class.
13) What is the correct output of given code snippets?
public class Ex
{
public void sayhello()
{
Console.WriteLine("Hello World");
}
static public void Main()
{
sayhello();
}
}
- Hello World
- Runtime Exception
- Syntax error
- Array index out of bound
Correct answer: 3
Syntax error
We cannot call a non-static method from a static method.
14) What is the correct output of given code snippets?
public class Ex
{
private static void sayhello()
{
Console.WriteLine("Hello World");
}
static public void Main()
{
sayhello();
}
}
- Hello World
- Runtime Exception
- Syntax error
- Array index out of bound
Correct answer: 1
Hello World
The above program will print "Hello World" message on the console screen.
15) What is the correct output of given code snippets?
public class Ex
{
public static int calculateFactorial(int num)
{
int i = 0;
int fact = 1;
for (i = 1; i >= num; i++)
{
fact = fact * i;
}
return fact;
}
static public void Main()
{
int num = 5;
int result = 0;
result = calculateFactorial(num);
Console.WriteLine("Result : "+result);
}
}
- Result : 120
- Result : 1
- Runtime Exception
- Syntax error
Correct answer: 2
Result : 1
In the method calculateFactorial() loop will execute only once, because for second-time condition get false.
16) What is the correct output of given code snippets?
using System;
public class Ex
{
public static float calculateFactorial(int num)
{
int i = 0;
float fact = 1;
for (i = 1; i <= num; i++)
{
fact = fact * i;
}
return fact;
}
static public void Main()
{
int num = 5;
int result = 0;
result = calculateFactorial(num);
Console.WriteLine("Result : "+result);
}
}
- Result : 120
- Result : 1
- Runtime Exception
- Syntax error
Correct answer: 4
Syntax error
The above code will generate compile-time error, because method calculateFactorial() returns a float value and we are assigning it to an integer value. Then this implicit conversion is not possible.
17) What is the correct output of given code snippets?
using System;
public class Sample
{
public static void printTable(int num)
{
int i = 0;
for (i = 1; i <= 10; i++)
{
Console.Write(num * i + " ");
}
}
static public void Main()
{
Sample S = new Sample();
S.printTable(3);
}
}
- 2 4 6 8 10 12 14 16 18 20
- 2 4 6 8 10 12 14 16 18
- Compiler Error
- Runtime Exception
Correct answer: 3
Compiler Error
The above program generates a compile-time error, because we cannot access a static method using the object of the class.
18) What is the correct output of given code snippets?
using System;
public static void printTable(int num)
{
int i = 0;
for (i = 1; i <= 10; i++)
{
Console.Write(num * i + " ");
}
}
public class Sample
{
static public void Main()
{
printTable(3);
}
}
- 2 4 6 8 10 12 14 16 18 20
- 2 4 6 8 10 12 14 16 18
- Compiler Error
- Runtime Exception
Correct answer: 3
Compiler Error
The above program generates a compile-time error because we cannot create any method outside the class.
19) In C# can we create more than one method with the same name?
- Yes
- No
Correct answer: 1
Yes
Yes, we can create more than one method with the same name using method overloading.
20) What is the correct output of given code snippets?
using System;
public class Sample
{
static public void printChar()
{
Console.Write("* ");
}
static public int printChar(char ch)
{
Console.Write(ch+" ");
return 1;
}
static public void Main()
{
printChar();
printChar('#');
}
}
- Syntax Error
- * #
- Runtime Error
- # *
Correct answer: 2
* #
The above method will print "* #" on the console screen.
21) What is the correct output of given code snippets?
using System;
public class Sample
{
public static void MyMethod(int A, ref int X, ref int Y)
{
X = A * A;
Y = A * A * A;
}
static public void Main()
{
int AA = 5;
int xx = 0;
int yy = 0;
MyMethod(AA, ref xx, ref yy);
Console.WriteLine("Values: {0},{1}", xx, yy);
}
}
- 25 25
- 0 0
- 25 125
- 125 25
Correct answer: 3
25 125
The above program will print (25 125) on the console screen.
22) If we want to create a static method MyMethod() is to receive a string and a char and it returns a int then what is the correct way to define this method?
- public int static MyMethod(string s, char c) {...}
- public int MyMethod(string s, char c) {...}
- public static int MyMethod(string s, char c) {...}
- public static MyMethod(string s, char c) {...}
Correct answer: 3
public static int MyMethod(string s, char c) {...}
Option 3 is the correct way to create a method according to a given requirement.
23) If we want to create a static method MyMethod() that accept int or sometime string then what is the correct way to define this method?
- public static void MyMethod(int X) {...}
- public static void MyMethod(object X) {...}
- public static void MyMethod(string X) {...}
- public static void MyMethod(int X, string Y) {...}
Correct answer: 2
public static void MyMethod(object X) {...}
The option 2 is the correct way to create method according to given requirement.
24) What is the correct output of given code snippets?
using System;
public class Sample
{
public static void MyMethod(int A, ref int X, ref int Y)
{
X = A * A;
Y = A * A * A;
return 0;
}
static public void Main()
{
int AA = 5;
int xx = 0;
int yy = 0;
MyMethod(AA, ref xx, ref yy);
Console.WriteLine("Values: {0},{1}", xx, yy);
}
}
- Syntax Error
- 25 125
- Runtime Exception
- 125 125
Correct answer: 1
Syntax Error
We cannot use the return statement in a method that's return type is void.
25) What is the correct output of given code snippets?
using System;
public class Sample
{
public static int MyMethod(int A, ref int X, ref int Y)
{
X = A * A;
Y = A * A * A;
return X,Y;
}
static public void Main()
{
int AA = 5;
int xx = 0;
int yy = 0;
MyMethod(AA, ref xx, ref yy);
Console.WriteLine("Values: {0},{1}", xx, yy);
}
}
- Syntax Error
- 25 125
- Runtime Exception
- 125 125
Correct answer: 1
Syntax Error
We cannot return more than one value using the return statement.