C# Interview Questions and Answers Part 5

1)   what is the output?

    static void Main(string[] args)

    {

        int val;

        for (val = -5; val <= 5; val++)

        {

            switch (val)

            {

                case 0:

                    Console.Write("India");

                    break;

            }

            if (val > 0)

                Console.Write("B");

            else if (val < 0)

                Console.Write("X");

        }

        Console.ReadLine();      

    }

OUTPUT :  

XXXXXIndiaBBBBB

2)   What is the output?

static void Main(string[] args)

    {

        char ch = Convert.ToChar('a' | 'b' | 'c');

        switch (ch)

        {

            case 'A':

            case 'a':

                Console.WriteLine("case A | case a");

                break;

            case 'B':

            case 'b':

                Console.WriteLine("case B | case b");

                break;

            case 'C':

            case 'c':

            case 'D':

            case 'd':

                Console.WriteLine("case D | case d");

                break;

        }

        Console.ReadLine();      

    }

OUTPUT :  

case D | case d

3)   What is the output?

static void Main(string[] args)

    {

        int i = 1, j = 1, val;

        while (i < 25)

        {

            Console.Write(j + " ");

            val = i + j;

            j = i;

            i = val;

        }

        Console.ReadLine();      

    }

OUTPUT :  

1 1 2 3 5 8 13

 

4)   Which of the following statements are correct?

  1. A switch statement can act on numerical as well as Boolean types.
  2. A switch statement can act on characters, strings and enumerations types.
  3. We cannot declare variables within a case statement if it is not enclosed by{ }.
  4. All of the expressions of the for statement are not optional.

Ans:-1,2

 

5)   What is the output?

static void Main(string[] args)

    {

        float a = 4.5;

        Console.WriteLine(a);

        Console.ReadLine();   

    }

OUTPUT :  

Error    1    Literal of type double cannot be implicitly converted to type 'float'



Subscribe

Receive Quality Tutorials Straight in your Inbox by submitting your Email below:

Delivered by FeedBurner

Protected by Copyscape Duplicate Content Checker