Q:

C#.Net find output programs (default Arguments) | set 1

belongs to collection: C#.Net find output programs

0

Find the output of C#.Net programs | default Arguments | Set 1: Enhance the knowledge of C#.Net default Arguments concepts by solving and finding the output of some C#.Net programs.

Question 1:

using System;

namespace Demo
{
    class Program
    {
        static void fun(int a, int b, int c = 30)
        {
            Console.WriteLine("A: " + a + ", B: " + b + ", C: " + c);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            fun(10, 20, 40);
            fun(10, 20);
        }
    }
}  

Question 2:

using System;

namespace Demo
{
    class Program
    {
        static void fun(int a, int b=50, int c)
        {
            Console.WriteLine("A: " + a + ", B: " + b + ", C: " + c);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            fun(10, 20, 40);
            fun(10, c: 20);
        }
    }
}

Question 3:

using System;

namespace Demo
{
    class Program
    {
        static void fun(int a, int b=50, int c=80)
        {
            Console.WriteLine("A: " + a + ", B: " + b + ", C: " + c);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            fun(10, 20, 40);
            fun(10, c: 20);
        }
    }
}

Question 4:

using System;

namespace Demo
{
    class Program
    {
        static void fun(int a, int b=50, int c=80)
        {
            Console.WriteLine("A: " + a + ", B: " + b + ", C: " + c);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            fun(10, 20, 40);
            fun(10, b: 20);
        }
    }
}

Question 5:

using System;

namespace Demo
{
    class Program
    {
        const int VAL = 10;

        static void fun(int a, int b=50, int c=VAL)
        {
            Console.WriteLine("A: " + a + ", B: " + b + ", C: " + c);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            fun(10, 20, 40);
            fun(10, b: 20);
        }
    }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

A: 10, B: 20, C: 40
A: 10, B: 20, C: 30
Press any key to continue . . .

Explanation:

In the above program, we create a class Program that contains two static methods fun() and Main().

The method fun() contains three arguments a, b, and c. Here, argument c is used as a default argument. It means if we did not pass the value 3rd argument in the method fun(), then it takes value 30 for argument c by default.

Now coming to the Main() method, this is an entry point for program execution.

fun(10, 20, 40);

The above method call will print "A: 10, B: 20, C: 40" on the console screen.

fun(10, 20);

The above method call will print "A: 10, B: 20, C: 30" on the console screen.

Answer 2:

Output:

main.cs(7,46): error CS1737: Optional parameter cannot precede required parameters

Explanation:

The above program will generate syntax error because default parameters must be trailing parameters in C#.

Answer 3:

Output:

A: 10, B: 20, C: 40
A: 10, B: 50, C: 20
Press any key to continue . . .

Explanation:

In the above program, we create a class Program that contains two static methods fun() and Main().

The method fun() contains three arguments a, b, and c. Herem arguments b and c are used as default arguments. It means if we did not pass the values of 2nd or 3rd argument in the method fun(), then it takes use default values.

Now coming to the Main() method, this is an entry point for program execution,

fun(10, 20, 40);

The above method call will print "A: 10, B: 20, C: 40" on the console screen.

fun(10, c:20);

The above method call will print "A: 10, B: 50, C: 20" on the console screen. Here, we used the default value of argument b.

Answer 4:

Output:

A: 10, B: 20, C: 40
A: 10, B: 20, C: 80
Press any key to continue . . .

Explanation:

In the above program, we create a class Program that contains two static methods fun() and Main().

The method fun() contains three arguments a, b, and c. Here, arguments b and c are used as default arguments. It means if we did not pass the values of 2nd or 3rd argument in the method fun(), then it takes use default values.

Now coming to the Main() method, this is an entry point for program execution,

fun(10, 20, 40);

The above method call will print "A: 10, B: 20, C: 40" on the console screen,

fun(10, b:20);

The above method call will print "A: 10, B: 20, C: 80" on the console screen. Here, we used the default value of argument c.

Answer 5:

Output:

A: 10, B: 20, C: 40
A: 10, B: 20, C: 10
Press any key to continue . . .

Explanation:

In the above program, we create a class Program that contains two static methods fun() and Main() and we also defined a constant VAL that contains value 10, which is used as a default value for argument c in fun() method.

The method fun() contains three arguments a, b, and c. Here, arguments b and c are used as default arguments. It means if we did not pass the values of 2nd or 3rd argument in the method fun(), then it takes use default values.

Now coming to the Main() method, this is an entry point for program execution,

fun(10, 20, 40);

The above method call will print "A: 10, B: 20, C: 40" on the console screen,

fun(10, b:20);

The above method call will print "A: 10, B: 20, C: 10" on the console screen. Here, we used the default value of argument c.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C#.Net find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C#.Net find output programs (default Arguments) | ... >>
<< C#.Net find output programs (Arrays) | set 3...