Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

What will be the output of the following C# code?
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();
}

What will be the output of the following C# code?
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();
}
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 case number is declared is to be called.
Output: case D|case d

Join The Discussion