C# Questions & Answers on Switch Statements for Freshers

1. Select the output for the following set of 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

Explanation: We can put ‘default’ case in any order and hence write cases in any order.
Output: Race.
2. Select the output for the following set of 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

Explanation: Syntax error- switch case does not work with syntax as 3 to 10:
Output :
static void Main(string[] args)
{
int i = 2,j = 4;
switch (i + j * 2)
{
case 1 :
case 2 :
Console.WriteLine(“1 and 2”);
break;
case 3 :
Console.WriteLine(“3 to 10”);
break;
}
Console.ReadLine();
}
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. Select the output for the following set of 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: 3
4
4
Explanation: i – k = -1.So, case -1 will be executed only.
4. Select output for the following set of 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

Explanation: In case expression we don’t have constant variable.
5. Select output for the following set of 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

Explanation: Solving expression (i + j – k) gives 1 and hence,solving for case 1:case 3:case 5:.
Output : 1
3
1
6. Select the output for the following set of 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

Explanation: Invalid expression’7:’ in case 9:7:.
7. Select the output for the following 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

Explanation: Only integral values are allowed for case expression.
5.0f = (int)5.0f.
5.0L =(int)5.0L.
8. Select output for the following 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

Explanation: Continue cannot be used as a part of switch statement.
Output :
static void Main(string[] args)
{
int i;
int j = 1;
int []ar = {21 , 22, 13, };
switch(ar[j])
{
case 1:
i++;
break;
case 2:
i += 2;
j = 3;
continue;
case 3:
i %= 2;
j = 4;
default:
–i;
}
Console.WriteLine(i);
Console.ReadLine();
}
9. Select the output for the following set of 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

Explanation: Case statement declared last will only be executed as no particular casenumber is declared is to be called.
Output : case D|case d
10. Select the output for the following set of 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

Explanation: ASCII value of ‘p’ is 112.Hence, coco 112 main.
Output: coco 112 main.

Related

Multiple Choice Questions 6123952066308301082

Post a Comment

emo-but-icon

item