C# Programming - Functions and Subroutines - Discussion

Discussion Forum : 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.
1, 3, 5
3, 4, 5
2, 5
4, 5
None of these
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
7 comments Page 1 of 1.

SergeyD said:   6 years ago
1, 2, 5 are correct.

Bhagwan said:   7 years ago
Why not option A?

Dexter Williams said:   8 years ago
@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.

Sunshine said:   9 years ago
Can anyone explain the option 2? I didn't understand.

Ranjan said:   1 decade ago
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;
}
}

Gopesh said:   1 decade ago
@Soumya.

You can't write.

int i;
function (i) ;

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

Soumya said:   1 decade ago
What about option 1 ?

Post your comments here:

Your comments will be displayed after verification.