Q:

C#.Net find output programs (switch statement) | set 2

0

Find the output of C#.Net programs | switch statement | Set 2: Enhance the knowledge of C#.Net switch statement concepts by solving and finding the output of some C#.Net programs.

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int num = 10;

            switch (-4)
            { 
                case 1-5:
                    Console.WriteLine("@@@@@@@@@@@@@@@");
                    break;
                case 21-11:
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                case 31-15:
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                default:
                    Console.WriteLine("###############");
                    break;
            }
        }
    }
}

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            double NUM = 2.34;

            switch (NUM)
            { 
                case 2.34:
                    Console.WriteLine("@@@@@@@@@@@@@@@");
                    break;
                case 1.23:
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                case 4.2:
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                default:
                    Console.WriteLine("###############");
                    break;
            }
        }
    }
}

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            switch ((int)2.34)
            { 
                case 2:
                    Console.WriteLine("@@@@@@@@@@@@@@@");
                    break;
                case 1:
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                case 4:
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                default:
                    Console.WriteLine("###############");
                    break;
            }
        }
    }
}

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            string STR = "hello";
            switch (STR)
            { 
                case "HELLO":
                    Console.WriteLine("@@@@@@@@@@@@@@@");
                    break;
                case "hello":
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                case "STR":
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                default:
                    Console.WriteLine("###############");
                    break;
            }
        }
    }
}

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            string STR = "hello";

            switch (STR[0])
            { 
                case "h":
                    Console.WriteLine("@@@@@@@@@@@@@@@");
                    break;
                case 'h':
                    Console.WriteLine("$$$$$$$$$$$$$$$");
                    break;
                case 'H':
                    Console.WriteLine("%%%%%%%%%%%%%%%");
                    break;
                default:
                    Console.WriteLine("###############");
                    break;
            }
        }
    }
}

All Answers

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

Answer 1:

Output:

@@@@@@@@@@@@@@@
Press any key to continue . . .

Explanation:

In the above program, we created a variable num, which is initialized with 10. After evaluating case statements, code will be like this:

switch (-4)
{ 
    case -4:
        Console.WriteLine("@@@@@@@@@@@@@@@");
        break;
    case 10:
        Console.WriteLine("$$$$$$$$$$$$$$$");
        break;
    case 16:
        Console.WriteLine("$$$$$$$$$$$$$$$");
        break;
    default:
        Console.WriteLine("###############");
        break;
}

Here, we passed -4 in switch then case -4 will execute and print "@@@@@@@@@@@@@@@" on the console screen.

Answer 2 :

Output:

main.cs(12,13): error CS0151: A switch expression of type `double' cannot be 
converted to an integral type, bool, char, string, enum or nullable type

Explanation:

The above program will generate syntax error because we cannot use floating-point values in switch case.

Answer 3:

Output:

@@@@@@@@@@@@@@@
Press any key to continue . . .

Explanation:

In the above program, we passed (int) 2.34 in the switch block, then it will be "2" after typecast, then case 2 will execute and print "@@@@@@@@@@@@@@@" on the console screen.

Answer 4:

Output:

$$$$$$$$$$$$$$$
Press any key to continue . . .

Explanation:

In the above program, we created a string variable STR, which is initialized with "hello". Then (case "hello") will execute and print "$$$$$$$$$$$$$$$" on the console screen.

Answer 5:

Output:

main.cs(14,22): error CS0029: Cannot implicitly convert type `string' to `char'

Explanation:

The above program will generate syntax error, because we used string instead of a character in (case "h" ), here we passed character in the switch block.

Note: One or more character(s) are enclosed with double quotes, is known as a string. And character is represented using single quotes C#.

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now