C# Questions & Answers – Use of Ref and Out Parameters

This set of C# Question Bank focuses 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
View Answer

Answer: b
Explanation: 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 :

advertisement
advertisement
10, 36

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

Answer: d
Explanation: None.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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
advertisement

b)

   21
    1
   21 
   21
advertisement

c)

   11
   21
   21
   11

d)

   21
    1
   21
   21
View Answer
Answer: d
Explanation: None.
Output :

21 1 21 21

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

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: index positions which are assigned the new values are passed as a reference parameter and hence rest positions are filled with zero values.
Output :

 0 24 0 32 0 0

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

Answer: b
Explanation: None.
Output:

5040

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

Answer: c
Explanation: 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) &&
View Answer

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

Answer: d
Explanation: 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
View Answer

Answer: d
Explanation: None.

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

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

To practice C# Question Bank, 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.