C# Programming Questions and Answers Part-12

1. What does the following C# code imply?
csharp abc;
abc = new charp();
a) Object creation on class csharp
b) Create an object of type csharp on heap or on stack depending on the size of object
c) create a reference c on csharp and an object of type csharp on heap
d) create an object of type csharp on stack

Answer: c
Explanation: create a reference c on csharp and an object of type csharp on heap

2. What will be the output of the following C# code?
class test
{
public void print()
{
Console.WriteLine("Csharp:");
}
}
class Program
{
static void Main(string[] args)
{
test t;
t.print();
Console.ReadLine();
}
}
a) Code runs successfully prints nothing
b) Code runs and prints “Csharp”
c) Syntax error as t is unassigned variable which is never used
d) None of the mentioned

Answer: c
Explanation: object of class test should be declared as test t = new test();
test t = new test();
t.print();
Console.ReadLine();

3. Which reference modifier is used to define reference variable?
a) &
b) ref
c) #
d) $

Answer: b
Explanation: ref

4. What will be the output of the following C# code?
static void Main(string[] args)
{
int a = 5;
fun1 (ref a);
Console.WriteLine(a);
Console.ReadLine();
}
static void fun1(ref int a)
{
a = a * a;
}
a) 5
b) 0
c) 20
d) 25

Answer: d
Explanation: Here ‘a’ = 5. Copy of variable is passed as reference to parameter ‘a’.
Output: 25

5. What will be the output of the following C# code?
static void Main(string[] args)
{
int[] arr = new int[] {1, 2, 3, 4, 5};
fun1(ref arr);
Console.ReadLine();
}
static void fun1(ref int[] array)
{
for (int i = 0; i < array.Length; i++)
{
array[i] = array[i] + 5;
Console.WriteLine(array[i] + " ");
}
}
a) 6 7 8 9 10
b) 15 17 8 8 20
c) 15 17 8 29 20
d) Syntax error while passing reference of array variable

Answer: a
Explanation: array ‘arr’ after declaration is passed as reference parameter.
a[0] = 1 + 5 = 6.
a[1] = 2 + 5 = 7.
.
.
a[4] = 5 + 5 = 10.
Output :
15 17 8 29 20

6. What will be the output of the following C# code?
static void Main(string[] args)
{
int []a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
func(ref a);
Console.ReadLine();
}
static void func(ref int[] x)
{
Console.WriteLine(" numbers are:");
for (int i = 0; i < x.Length; i++)
{
if (x[i] % 2 == 0)
{
x[i] = x[i] + 1;
Console.WriteLine(x[i]);
}
}
}
a) numbers are : 2 4 6 8 10
b) numbers are : 3 5 7 9 11
c) numbers are : 2 3 4 5 6
d) none of the mentioned

Answer: b
Explanation: Those numbers divisible by 2 are 2, 4, 6, 8, 10 and when condition of loop is executed it increments by 1.
i.e for x[1] = 2%2 == 0.So, x[1] = 2 + 1 = 3.
x[3] = 4%2 == 0.So, x[3] = 4 + 1 = 5 and so on.
Output :
3 5 7 9 11

7. Select the wrong statement about ‘ref’ keyword in C#?
a) References can be called recursively
b) The ‘ref’ keyword causes arguments to be passed by reference
c) When ‘ref’ are used, any changes made to parameters in method will be reflected in variable when control is passed back to calling method
d) All of the mentioned

Answer: a
Explanation: References can be called recursively

8. Select correct differences between ‘=’ and ‘==’ in C#.
a) '==' operator is used to assign values from one variable to another variable
'=' operator is used to compare value between two variables
b) '=' operator is used to assign values from one variable to another variable
'==' operator is used to compare value between two variables
c) No difference between both operators
d) None of the mentioned

Answer: b
Explanation: '=' operator is used to assign values from one variable to another variable
'==' operator is used to compare value between two variables

9. What will be the output of the following C# code?
static void Main(string[] args)
{
int X = 0;
if (Convert.ToBoolean(X = 0))
Console.WriteLine("It is zero");
else
Console.WriteLine("It is not zero");
Console.ReadLine();
}
a) It is zero
b) It is not zero
c) Infinite loop
d) None of the mentioned

Answer: b
Explanation: The operator ‘=’ used is not comparison operator it is assignment operator. Since value assigned to ‘X’ = 0. So,’0′ value is stored in ‘X’ and with the help of if condition implementation it is converted to ‘false’ which directly means It is not zero but ‘1’ which means ‘true’.

10. What will be the output of the following C# code?
static void Main(string[] args)
{
int X = 6,Y = 2;
X *= X / Y;
Console.WriteLine(X);
Console.ReadLine();
}
a) 12
b) 6
c) 18
d) Compile time error

Answer: c
Explanation: X*=X/Y.
X=x*(X/Y).
Output:
18