C# Questions & Answers – Reference Variables and Assignment

This set of C# Multiple Choice Questions & Answers (MCQs) focuses on “Reference Variables and Assignment”.

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

Answer: b
Explanation: None.

2. What will be the output of the following C# code?

  1.    static void Main(string[] args)
  2.    {
  3.        int a = 5;
  4.        fun1 (ref a);
  5.        Console.WriteLine(a);
  6.        Console.ReadLine();
  7.     }
  8.     static void fun1(ref int a)
  9.     {
  10.         a = a * a;
  11.     }

a) 5
b) 0
c) 20
d) 25
View Answer

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

advertisement
advertisement
25

3. What will be the output of the following C# code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.  static void Main(string[] args)
  2.  {
  3.      int[] arr = new int[] {1, 2, 3, 4, 5};
  4.      fun1(ref arr);
  5.      Console.ReadLine();
  6.   }
  7.  static void fun1(ref int[] array)
  8.  {
  9.      for (int i = 0; i < array.Length; i++)
  10.      {
  11.          array[i] = array[i] + 5;
  12.          Console.WriteLine(array[i] + " ");
  13.      }
  14.  }

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
View Answer

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 :

advertisement
 15 17 8 29 20

advertisement

4. What will be the output of the following C# code?

  1.   static void Main(string[] args)
  2.   {
  3.       int a = 10 , b = 20;
  4.       Console.WriteLine("Result before swap is: "+ a +" "+b);
  5.       swap(ref a, ref b);
  6.       Console.ReadLine();
  7.   }
  8.   static void swap(ref int i, ref int j)
  9.   {
  10.       int t;
  11.       t = i;
  12.       i = j;
  13.       j = t;
  14.       Console.WriteLine("Result after swap is:"+ i +" "+j);
  15.   }

a)

Result before swap is: 20 10
Result after swap is: 20 10

b)

Result before swap is: 10 20
Result after swap is:20 10

c)

Result before swap is: 10 20
Result after swap is:10 20

d)

Result before swap is: 20 10
Result after swap is:10 20
View Answer
Answer: b
Explanation: Makes use of call by reference parameter.
Output:

       Result before swap is: 10 20.
       Result after swap is:20 10.
 
 

5. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      int []a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  4.      func(ref a);
  5.      Console.ReadLine();
  6.  }
  7.  static void func(ref int[] x)
  8.  {
  9.      Console.WriteLine(" numbers are:");
  10.      for (int i = 0; i < x.Length; i++)
  11.      {
  12.          if (x[i] % 2 == 0)
  13.          {
  14.              x[i] = x[i] + 1;
  15.              Console.WriteLine(x[i]);
  16.          }
  17.      }
  18.  }

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
View Answer

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

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

Answer: a
Explanation: None.

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

Answer: b
Explanation: None.

8. What will be the output of the following C# code?

  1.   static void Main(string[] args)
  2.   {
  3.       int X = 0;
  4.       if (Convert.ToBoolean(X = 0))
  5.       Console.WriteLine("It is zero");
  6.       else 
  7.       Console.WriteLine("It is not zero");
  8.       Console.ReadLine();
  9.   }

a) It is zero
b) It is not zero
c) Infinite loop
d) None of the mentioned
View Answer

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

9. What will be the output of the following C# code?

  1.     static void Main(string[] args)
  2.     {
  3.        int X = 6,Y = 2;
  4.        X  *= X / Y;
  5.        Console.WriteLine(X);
  6.        Console.ReadLine();
  7.     }

a) 12
b) 6
c) 18
d) Compile time error
View Answer

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

18

10. What will be the output of the following C# code?

  1.     static void Main(string[] args)
  2.     {
  3.         int x = 4 ,b = 2;
  4.         x -= b/= x * b;
  5.         Console.WriteLine(x + " " + b);
  6.         Console.ReadLine();
  7.     }

a) 4 2
b) 0 4
c) 4 0
d) 2 2
View Answer

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

 4 0

11. 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
View Answer

Answer: b
Explanation: None.

12. What will be the output of the following C# code?

  1.    static void Main(string[] args)
  2.    {
  3.        int x = 8;
  4.        int b = 16;
  5.        int C = 64;
  6.        x /= b /= C;
  7.        Console.WriteLine(x + " " + b+ " " +C);
  8.        Console.ReadLine();
  9.    }

a) 8 2 32
b) 32 4 8
c) 32 2 8
d) Compile time error
View Answer

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

13. What will be the output of the following C# code?

  1.    static void Main(string[] args)
  2.    {
  3.        int x = 8;
  4.        int b = 16;
  5.        int C = 64;
  6.        x /= b /= C /= x;
  7.        Console.WriteLine(x + " " + b+ " " +C);
  8.        Console.ReadLine();
  9.    }

a) 8 2 4
b) 2 4 8
c) 4 2 8
d) Compile time error
View Answer

Answer: c
Explanation: x / = b / = C /= x is evaluated as follows:
C = C/x = 64/8 = 8
b = b/8 = 16/8 = 2
x = x/2 = 8/2 = 4
Output:

4 2 8

Sanfoundry Global Education & Learning Series – C# Programming Language.

Here’s the list of Best Books in C# Programming Language.

To practice all areas of C#, here is complete set on 1000+ Multiple Choice Questions and Answers on C#.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.