C# Programming - Functions and Subroutines - Discussion

Discussion :: Functions and Subroutines - General Questions (Q.No.8)

8. 

Which of the following statements are correct?

  1. C# allows a function to have arguments with default values.
  2. C# allows a function to have variable number of arguments.
  3. Omitting the return value type in method definition results into an exception.
  4. Redefining a method parameter in the method's body causes an exception.
  5. params is used to specify the syntax for a function with variable number of arguments.

[A]. 1, 3, 5
[B]. 3, 4, 5
[C]. 2, 5
[D]. 4, 5
[E]. None of these

Answer: Option C

Explanation:

No answer description available for this question.

Soumya said: (Jan 21, 2012)  
What about option 1 ?

Gopesh said: (Jun 27, 2012)  
@Soumya.

You can't write.

int i;
function (i) ;

//Since I have a default value but you can't pass it without initializing.

Ranjan said: (Jun 26, 2013)  
Option 1 is also right.

See the following code:

class Program
{
static void Main(string[] args)
{
int i, j;
Program obj = new Program();
int s = obj.sum(10);
Console.WriteLine("{0}", s);
Console.ReadLine();
}
public int sum( int i,int j=10)
{
return i + j;
}
}

Sunshine said: (Dec 9, 2016)  
Can anyone explain the option 2? I didn't understand.

Dexter Williams said: (Apr 2, 2017)  
@Sunshine.

Option 2 is true due to the params data type.

See the following code:

public class Program
{
public static void Main(string[] args)
{
int i, j;
Program obj = new Program();
i = obj.sumNumbers(10);
j = obj.sumNumbers(10, "desk", 34, 50);
Console.WriteLine("{0}, {1}", i, j);
}


public int sumNumbers(params object[] list)
{
int newSum = 0;
for (int i = 0; i < list.Length; i++)
{
if (list[i].GetType() == typeof(int))
newSum += (int) list[i];
}
return newSum;
}
}

Output: 10, 94.

Bhagwan said: (Jul 15, 2018)  
Why not option A?

Sergeyd said: (Mar 19, 2019)  
1, 2, 5 are correct.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.