C# Questions & Answers – Use of Variable Number of Arguments

This set of C# Questions and Answers for Experienced people focuses on “Use of Variable Number of Arguments”.

1. The method in which large or variable number of arguments are handled is known as ________________
a) Value parameters
b) Output parameters
c) Parameter arrays
d) None of the mentioned
View Answer

Answer: c
Explanation: None.

2. The modifiers used to define an array of parameters or list of arguments is __________
a) ref
b) out
c) param
d) var
View Answer

Answer: c
Explanation: None.

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

advertisement
advertisement
  1.  static void Main(string[] args)
  2.  {
  3.      object[] a = {" 1 ", 4.0f, " harsh "};
  4.      fun(a);
  5.      Console.ReadLine();
  6.  }
  7.  static void fun(params object[] b)
  8.  {
  9.      for (int i = 0; i < b.Length - 1; i++)
  10.          Console.WriteLine(b[i] + " ");
  11.  }

a) 1 4.0 harsh
b) 1 4
c) 1 4 hars
d) 1 4 harsh
View Answer

Answer: d
Explanation: ‘a’ is declared as array of objects which is passed as a parameter to a single method fun() using variable number of parameters.
Output :

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
 1 4 harsh

4. Which of the following statements are correct?
a) C SHARP allows a function to have arguments with default values
b) C SHARP allows a function to have variable number of arguments
c) Params are used to specify the syntax for a function having arguments
d) Omitting the return value type in method definition results into an exception
View Answer

Answer: b
Explanation: None.
advertisement

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};
  4.     fun(a);
  5.     Console.ReadLine();
  6. }
  7. static void fun(params int[] b )
  8. {
  9.     int[] k = { 3, 4, 7, 8,'\0' };
  10.     for (int i = 0; i < b.Length; i++)
  11.     {
  12.         b[i] = b[i] + k[i] ;
  13.         Console.WriteLine( b[i] + " ");
  14.     }
  15. }

a) Compile time error
b) 3, 4, 7, 8, 5
c) 3, 4, 7, 8, 5, 1, 2, 3, 4, 5
d) 4, 6, 10, 12, 5
View Answer

Answer: d
Explanation: Passing of array parameters declared in main() and hence adding elements of array passed using param to another array k[] declared in fun() method.
Output :

advertisement
4, 6, 10, 12, 5

6. 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};
  4.      fun(a);
  5.      Console.ReadLine();
  6.  }
  7.  static void fun(params int[] b )
  8.  {
  9.      for (int i = 0; i < b.Length; i++)
  10.      {
  11.          b[i] = b[i] * 5 ;
  12.          Console.WriteLine(b[i] + "");
  13.      }
  14.  }

a) 1, 2, 3, 4, 5
b) 5, 10, 15, 20, 25
c) 5, 25, 125, 625, 3125
d) 6, 12, 18, 24, 30
View Answer

Answer: b
Explanation: None.
Output :

 5, 10, 15, 20, 25

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

  1.  static void Main(string[] args)
  2.  {
  3.      int[] a = { 2, 21, 34, 46, 85, 88, 90};
  4.      fun(a);
  5.      Console.WriteLine(a + " ");
  6.      Console.ReadLine();
  7.  }
  8.  static void fun(params int [] b )
  9.  {
  10.      int [] c = { 1, 2, 3, 4, 5, 6, 7};
  11.      int i ;
  12.      for (i = 0 ;i < b.Length ;i++)
  13.      if (b[i] % 2 == 0)
  14.      {
  15.          c[i] = b[i];
  16.      }
  17.      Console.WriteLine("even numbers are:");
  18.      for (i = 0 ;i <= b.Length ;i++)
  19.      {
  20.          Console.WriteLine(c[i]);
  21.      }
  22.  }

a) Compile time error
b) 2, 21, 34, 4, 6, 46, 88, 90
c) 2, 4, 34, 46, 6, 88, 90
d) 2, 34, 46, 88, 90
View Answer

Answer: d
Explanation: None.

8. Select the correct declaration of the defining array of parameters.
a)

   void func(int[] x) 
   {
    
   }

b)

   void func(int x)
   {

   }

c)

   void func(param int[])
   {
   
   } 

d)

   void fun(param int[] x)
   {
  
   }
View Answer
Answer: d
Explanation: None.
 
 

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

  1.  static void Main(string[] args)
  2.  {
  3.      int[] x = { 80, 82, 65, 72, 83, 67 };
  4.      fun(x);
  5.      Console.ReadLine();
  6.  }
  7.  static void fun(params int [] b )
  8.  {
  9.      int i;
  10.      for (i = 5; i >=0 ; i--)
  11.      {
  12.          Console.WriteLine(Convert.ToChar(b[i]));
  13.      }
  14.  }

a) 67 83 72 65 82 80
b) P R A H S C
c) C S H A R P
d) 80 82 65 72 83 67
View Answer

Answer: c
Explanation: None.

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

  1.  static void Main(string[] args)
  2.  {
  3.      int[] x = {65, 66, 67, 68, 69, 70};
  4.      fun(x);
  5.      Console.ReadLine();
  6.  }
  7.  static void fun(params int[] b )
  8.  {
  9.      int i;
  10.      for (i = 5; i > 0 ; i--)
  11.      {
  12.          b[i] = b[i] + 32;
  13.          Console.WriteLine(Convert.ToChar(b[i]));
  14.      }
  15.  }

a) A, B, C, D, E, F
b) F, E, D, C, B, A
c) f, e, d, c, b
d) b, c, d, e, f
View Answer

Answer: c
Explanation: None.

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# for Experienced people, 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.