250+ TOP MCQs on Switch Statements and Answers

C# multiple choice questions on switch statements in C# Programming Language.

1. What will be the output of the following C# code?

  1.   static void Main(string[] args)
  2.   {
  3.       int movie = 1;
  4.       switch (movie << 2 + movie)
  5.       {
  6.       default:
  7.           Console.WriteLine("3 Idiots");
  8.           break;
  9.       case 4:
  10.           Console.WriteLine("Ghazini");
  11.           break;
  12.       case 5:
  13.           Console.WriteLine("Krishh");
  14.           break;
  15.       case 8:
  16.           Console.WriteLine("Race");
  17.           break;
  18.       }
  19.       Console.ReadLine();
  20.   }

a) 3 Idiots
b) Ghazini
c) Race
d) Krishh

Answer: c
Clarification: We can put ‘default’ case in any order and hence write cases in any order.
Output:

2. What will be the output of the following C# code?

  1.   static void Main(string[] args)
  2.   {
  3.       int i = 2, j = 4;
  4.       switch (i + j * 2)
  5.       {
  6.       case 1 :
  7.       case 2 :
  8.           Console.WriteLine("1 and 2");
  9.           break;
  10.       case 3 to 10:
  11.           Console.WriteLine("3 to 10");
  12.           break;
  13.       }
  14.       Console.ReadLine();
  15.   }

a) 3 to 10 will be printed
b) 1 and 2 will be printed
c) The code reports an error as missing ; before :
d) The code gives output as 3 to 10

Answer: c
Clarification: Syntax error- switch case does not work with syntax as 3 to 10:
Output :
Here i = 2, j = 4. So, (i + j * 2) gives output as 10 and case 10 is missing. So, prints nothing for given code.

3. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      int i = 2, k = 3;
  4.      switch (i - k)
  5.      {
  6.      case -1:
  7.          ++i;
  8.          ++k;
  9.          break;
  10.      case 2:
  11.          --i;
  12.          ++k;
  13.          break;
  14.      default:
  15.          i += 3;
  16.          k += i;
  17.          break;
  18.      }
  19.      Console.WriteLine(i + "n" + k);
  20.      Console.ReadLine();
  21.  }

a) 2 3 3
b) 3 2 3
c) 3 4 4
d) 5 10 10

Answer: c
Output:

Clarification: i – k = -1. So, case -1 will be executed only.

4. What will be the output of the following C# code?

  1.    static void Main(string[] args)
  2.    {
  3.        int const p = 0;
  4.        switch (3 * 5 / 6)
  5.        {
  6.        case p:
  7.            Console.WriteLine("A");
  8.            break;
  9.        case p * 1:
  10.            Console.WriteLine("B");
  11.            break;
  12.        case p - 2:
  13.            Console.WriteLine("C");
  14.            break;
  15.        default:
  16.            Console.WriteLine("D");
  17.        }
  18.   }

a) A
b) B
c) C
d) Compile time error

Answer: d
Clarification: In case expression we don’t have constant variable.

5. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      int i = 2, j = 3, k = 4;
  4.      switch (i + j - k)
  5.      {
  6.      case 0: case 2: case 4:
  7.          ++i;
  8.          k += j;
  9.          break;
  10.      case 1: case 3: case 5 :
  11.          --i;
  12.          k -= j;
  13.          break;
  14.      default:
  15.          i += j;
  16.          break;
  17.      }
  18.      Console.WriteLine(i + "n" + j + "n" + k);
  19.      Console.ReadLine();
  20.  }

a) 1 3 1
b) 2 3 4
c) 5 3 4
d) Compile time error

Answer: a
Clarification: Solving expression (i + j – k) gives 1 and hence,solving for case 1:case 3:case 5:.
Output :

6. What will be the output of the following C# code?

  1.   static void Main(string[] args)
  2.   {
  3.       int  i = 9 , j = 7;
  4.       switch (i - j + 3)
  5.       {
  6.       case 9: 7:
  7.           j += 6;
  8.           break;
  9.       case 5:
  10.           i -= 4;
  11.           break;
  12.       }
  13.       Console.WriteLine(i + "n" + j);
  14.       Console.ReadLine();
  15.   }

a) 5 7
b) 9 13
c) Compile time error
d) 9 7

Answer: c
Clarification: Invalid expression’7:’ in case 9:7:.

7. What will be the output of the following C# code?

  1.   static void Main(string[] args)
  2.   {
  3.       switch (5)
  4.       {
  5.       case 5.0f:
  6.           Console.WriteLine("harsh");
  7.           break;
  8.       case 5:
  9.           Console.WriteLine("amish");
  10.           break;
  11.       case 5.0L:
  12.           Console.WriteLine("ANKIT");
  13.           break;
  14.       default:
  15.           Console.WriteLine("ashish");
  16.       }
  17.       Console.ReadLine();
  18.    }

a) amish
b) ANKIT
c) harsh
d) Compile time error

Answer: d
Clarification: Only integral values are allowed for case expression.
5.0f = (int)5.0f.
5.0L = (int)5.0L.

8. What will be the output of the following C# code?

  1.   static void Main(string[] args)
  2.   {
  3.       int i;
  4.       int j = 1;
  5.       int []ar = {21, 22, 13, 4};
  6.       switch (ar[j])
  7.       {
  8.       case 1:
  9.           i++;
  10.           break;
  11.       case 2:
  12.           i += 2;
  13.           j = 3;
  14.           continue;
  15.       case 3:
  16.          i %= 2;
  17.          j = 4;
  18.          continue;
  19.       default:
  20.          --i;
  21.       }
  22.       Console.WriteLine(i);
  23.       Console.ReadLine();
  24.   }

a) 23
b) 15
c) Compile time error
d) 12

Answer: c
Clarification: Continue cannot be used as a part of switch statement.

9. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      char ch = Convert.ToChar('a' | 'b' | 'c');
  4.      switch (ch)
  5.      {
  6.      case 'A':
  7.      case 'a':
  8.          Console.WriteLine("case A|case a");
  9.          break;
  10.      case 'B':
  11.      case 'b':
  12.          Console.WriteLine("case B|case b");
  13.          break;
  14.      case 'C':
  15.      case 'c':
  16.      case 'D':
  17.      case 'd':
  18.          Console.WriteLine("case D|case d");
  19.          break;
  20.      }
  21.      Console.ReadLine();
  22.  }

a) Compile time error
b) case A|case a
c) case B|case b
d) case D|case d

Answer: d
Clarification: Case statement declared last will only be executed as no particular case number is declared is to be called.
Output:

10. What will be the output of the following C# code?

  1.   static void Main(string[] args)
  2.   {
  3.       char ch = 'p';
  4.       switch (ch)
  5.       {
  6.       case 'p':
  7.           Console.WriteLine("coco" + "t" + Convert.ToInt32(ch));
  8.           break;
  9.       default:
  10.           Console.WriteLine("default");
  11.           break;
  12.      }
  13.      Console.WriteLine("main");
  14.   }

a) coco main
b) coco 112
c) coco 112 main
d) compile time error

Answer: c
Clarification: ASCII value of ‘p’ is 112. Hence, coco 112 main.
Output:

 

Leave a Reply

Your email address will not be published. Required fields are marked *