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)
{
int i = 2, j = 3, k = 4;
switch (i + j – k)
{
case 0: case 2: case 4:
++i;
k += j;
break;
case 1: case 3: case 5 :
–i;
k -= j;
break;
default:
i += j;
break;
}
Console.WriteLine(i + “\n” + j + “\n” + k);
Console.ReadLine();
}

What will be the output of the following C# code?
static void Main(string[] args)
{
int i = 2, j = 3, k = 4;
switch (i + j – k)
{
case 0: case 2: case 4:
++i;
k += j;
break;
case 1: case 3: case 5 :
–i;
k -= j;
break;
default:
i += j;
break;
}
Console.WriteLine(i + “\n” + j + “\n” + k);
Console.ReadLine();
}
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

Join The Discussion