Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Questions & Answers ? Methods in Class
C# Questions & Answers – Methods in Class
This section of our 1000+ C# multiple choice questions focuses on methods in class in C# Programming Language.
1. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int a = 5;
-
int s = 0, c = 0;
-
Mul (a, ref s, ref c);
-
Console.WriteLine(s + "t " +c);
-
Console.ReadLine();
-
}
-
static void Mul (int x, ref int ss, ref int cc)
-
{
-
ss = x * x;
-
cc = x * x * x;
-
}
a) 125 25
b) 25 125
c) Compile time error
d) 0 0
Explanation: The value of variable a is passed by value while value of variable s and c is passed by reference.
Output:
25 125
2. Which of the following statements are correct about functions?
a) C# allows a function to have arguments with default values
b) Redefining a method parameter in the method’s body causes an exception
c) C# allows function to have arguments with default values
d) Omitting the return type in method definition results into exception
Explanation: None.
3. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
Mul();
-
m();
-
Console.ReadLine();
-
}
-
static void Mul()
-
{
-
Console.WriteLine("4");
-
}
-
static void m()
-
{
-
Console.WriteLine("3");
-
Mul();
-
}
a) 4 3 3
b) 4 4 3
c) 4 3 4
d) 3 4 4
Explanation: First Mul() will be executed to print the number ‘4’ after that function m() will be executed to print the number ‘3’ and at last mentioned function Mul() will be executed to print the statement 4 to return the output as 4 3 4.
Output:
4 3 4
4. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
m();
-
Console.ReadLine();
-
}
-
static void m()
-
{
-
Console.WriteLine("HI");
-
m();
-
}
a) HI HI HI
b) HI
c) Stack overflow exception
d) Compile time error
Explanation: Control of statement when enters for once in m() does not go out, then it executes again and again inside the block until stack overflow exception occurs.
5. When a function fun() is to receive an int, a single & a double and it is to return a decimal, then the correct way of defining this C# function is?
a)
static fun(int i, single j, double k) { return decimal; }
b)
static decimal fun(int i, single, double k) { }
c)
decimal fun(int i, single j, double k) { }
d)
decimal static fun(int i, single j, double k) { }
Explanation: Correct way of declaration of function is defined as return_type name of function(return type).
6. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i = 10;
-
double d = 35.78;
-
fun(i);
-
fun(d);
-
Console.ReadLine();
-
}
-
static void fun(double d)
-
{
-
Console.WriteLine(d);
-
}
a)
35.78 10
b)
10 35.00
c)
10 35.78
d) None of the mentioned
Explanation: ‘int’ datatype is sub datatype of ‘double’. Hence, when first part of func() is executed it is integer part and hence when the second part is executed it is double.
Output:
10 35.78
7. How many values does a function return?
a) 0
b) 2
c) 1
d) any number of values
Explanation: A method can return only either single value or no value if no then it’s declared as void method();
8. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int y = 3;
-
y++;
-
if (y <= 5)
-
{
-
Console.WriteLine("hi");
-
Main(args);
-
}
-
Console.ReadLine();
-
}
a) hi hi
b) hi
c) Stack overflow exception
d) None of the mentioned
Explanation: If loop never gets over, it will execute continuously. The control never goes out of ‘if’ statement.
Output: hi hi . . . stack overflow exception
9. Which return statement correctly returns the output?
a)
public int cube(int x) { return (x + x); }
b)
public int cube(int x) return (x + x);
c)
public int cube(int x) { return x + x; }
d) None of the mentioned
Explanation: The correct syntax of return statement is defined within block of statements as { return(statement);}.
10. What will be the output of the following C# code?
-
public static void Main(string[] args)
-
{
-
p();
-
void p()
-
{
-
Console.WriteLine("hi");
-
}
-
}
a) Compile time error
b) hi
c) hi infinite times
d) None of the mentioned
Explanation: Invalid definition of function p() inside main().