C# Programming Questions and Answers Part-13

1. What will be the output of the following C# code?
static void Main(string[] args)
{
int x = 4 ,b = 2;
x -= b/= x * b;
Console.WriteLine(x + " " + b);
Console.ReadLine();
}
a) 4 2
b) 0 4
c) 4 0
d) 2 2

Answer: c
Explanation: x = x – b and b = b/(x*b).
Output:
4 0

2. What will be the output of the following C# expression?
int a+= (float) b/= (long)c.
a) float
b) int
c) long
d) none of the mentioned

Answer: b
Explanation: int

3. What will be the output of the following C# code?
static void Main(string[] args)
{
int x = 8;
int b = 16;
int C = 64;
x /= b /= C;
Console.WriteLine(x + " " + b+ " " +C);
Console.ReadLine();
}
a) 8 2 32
b) 32 4 8
c) 32 2 8
d) Compile time error

Answer: d
Explanation: Exception handling error of dividing by zero.

4. What will be the output of the following C# code?
static void Main(string[] args)
{
int x = 8;
int b = 16;
int C = 64;
x /= b /= C;
Console.WriteLine(x + " " + b+ " " +C);
Console.ReadLine();
}
a) 8 2 32
b) 32 4 8
c) 32 2 8
d) Compile time error

Answer: c
Explanation: x / = b / = C is x = x * c /b.
Output:
32 2 8

5. 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

Answer: b
Explanation: The value of variable a is passed by value while value of variable s and c is passed by reference.
Output: 25 125

6. 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

Answer: a
Explanation: C# allows a function to have arguments with default values

7. 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

Answer: c
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

8. 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

Answer: c
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.

9. How many values does a function return?
a) 0
b) 2
c) 1
d) any number of values

Answer: c
Explanation: A method can return only either single value or no value if no then it’s declared as void method();

10. 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

Answer: c
Explanation: If loop never gets over, it will execute continuously. The control never goes out of ‘if’ statement.
Output: hi
hi
.
.
.
stack overflow exception