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:

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

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 5;
  4.      int b = 0, c = 0;
  5.      method (a,  ref b, ref c);
  6.      Console.WriteLine(b + "  " + c);
  7.      Console.ReadLine();
  8.  }
  9.  static int method(int x, int p, ref int k)
  10.  {
  11.      p = x + x * x;
  12.      k = x * x + p;
  13.      return 0;
  14.  }

a) 30, 55
b) 55, 30
c) Compile time error
d) 0, 0
Answer: c
Clarification: Error occurrence as mismatch in parameter of method() definition. Keyword ‘ref’ should be used with parameter ‘p’ as ref int p.

8. Keyword used to define call by reference parameter in C# .NET?
a) &
b) out
c) ref
d) &&

  250+ TOP MCQs on Array and Initialization and Answers

Answer: c
Clarification: By definition.

9. Select the correct match of parameter declaration.

  1.  static Void main(string[] args)
  2.  {
  3.      int a = 5;
  4.      int b = 6;
  5.      float c = 7.2f;
  6.      math (ref a, ref b, ref c);
  7.      Console.WriteLine(a + "  " + b + "  " + c);
  8.  }
  9.  static int math(/*add parameter declaration */)
  10.  {
  11.      a += b;
  12.      b *= (int)c;
  13.      c += a * b;
  14.      return 0;
  15.  }

a) ref int a, int b, ref float c
b) ref int a, ref float c, ref int b
c) ref int a, ref int b, float c
d) ref int a, ref int b, ref float c
Answer: d
Clarification: static Void main(string[] args)
{
int a = 5;
int b = 6;
float c = 7.2f;
math(ref a, ref b, ref c);
console.writeLine(a + ” ” + b + ” ” + c);
}

10. Which statement is/are correct?
a) An argument passed to a ref parameter need not be initialized first
b) Variables passed as out arguments need to be initialized prior to being passed
c) To use a ref parameter, only the calling method must explicitly use the ref keyword
d) None of the mentioned
Answer: d
Clarification: None.

To practice C# Question Bank,

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top