250+ TOP MCQs on Use of Ref and Out Parameters and Answers

This set of C# Question Bank on “Use of Ref and Out Parameters”.

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

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          int i = 5;
  6.          int j;
  7.          method1(ref i);
  8.          method2(out j);
  9.          Console.writeline(i + "  " + j);
  10.      }
  11.      static void method1(ref int x)
  12.      { 
  13.          x = x + x;
  14.      }
  15.      static void method2(out int x)
  16.      {
  17.          x = 6;
  18.          x = x * x;
  19.      }
  20.  }

a) 36, 10
b) 10, 36
c) 0, 0
d) 36, 0
Answer: b
Clarification: Variable ‘i’ is passed as reference parameter declared with ‘ref’ modifier and variable ‘j’ is passed as a output parameter declared with ‘out’ keyword. Reference parameter used to pass value by reference is the same with out parameter.
Output :

2. Statements about ‘ref’ keyword used in C#.NET are?
a) The ref keyword causes arguments to be passed by reference
b) While using ‘ref’ keyword any changes made to the parameter in the method will be reflected in the variable when control is passed back to the calling method
c) Ref usage eliminates overhead of copying large data items
d) All of the mentioned
Answer: d
Clarification: None.

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

  1.  static void main(string[] args)
  2.  {
  3.      int n = 1;
  4.      method(n);
  5.      console.Writeline(n);
  6.      method1(ref n);
  7.      console.Writeline(n);
  8.  }
  9.  static void method(int num)
  10.  {
  11.      num += 20;
  12.      console.writeline(num);
  13.  }
  14.  static void method1(ref int num)
  15.  {
  16.      num += 20;
  17.      console.writeline(num);
  18.  }

a)

   1
   1
   1
   1

b)

   21
    1
   21 
   21

c)

   11
   21
   21
   11

d)

   21
    1
   21
   21

View Answer

Answer: d
Clarification: None.
Output :

4. Which method does following C# code explains?

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 10, b = 20;
  4.      method(ref a,  ref b);
  5.      console.writeline(a + "  " + b);
  6.  }
  7.  static void swap(ref int i,  ref int j)
  8.  {  
  9.      int t;
  10.      t = i;
  11.      i = j;
  12.      j = t;
  13.  }

a) Call by reference
b) Call by value
c) Output parameter
d) parameter arrays
Answer: a
Clarification: The following set of code explains swapping of numbers by reference parameters which makes usage of call by reference process.

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

  1.  static void main(string[] args)
  2.  {
  3.      int []arr = new int[]{ 1, 2, 3, 4, 5};
  4.      fun (ref arr);
  5.      for (int i = 0; i < arr.Length ; i++)
  6.      Console.WriteLine( arr[i] + "  ");
  7.  }
  8.  static void fun(ref int[]a)
  9.  {
  10.      a = new int[6];
  11.      a[3] = 32;
  12.      a[1] = 24;
  13.  }

a) 0, 0, 32, 0, 0, 0
b) 0, 24, 0, 32, 0, 0
c) 24, 0, 32, 0, 0, 0
d) 0, 0, 32, 0, 0, 0
Answer: b
Clarification: index positions which are assigned the new values are passed as a reference parameter and hence rest positions are filled with zero values.
Output :

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

  1.  static void main(string[] args)
  2.  {
  3.      int i;
  4.      int res = fun (out i);
  5.      console.writeline(res);
  6.      console.readline();
  7.  }
  8.  static int fun(out int i)
  9.  {
  10.      int s = 1;
  11.      i = 7;
  12.      for (int j = 1; j <= i; j++ )
  13.      s = s * j;
  14.      return s;
  15.  }

a) 4490
b) 5040
c) 5400
d) 3500
Answer: b
Clarification: None.
Output: