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
Related Posts
What will be the output of the following C# code?
static void Main(string[] args)
{
int X = 6,Y = 2;
X *= X / Y;
Console.WriteLine(X);
Console.ReadLine();
}What will be the output of the following C# code?
static void Main(string[] args)
{
int X = 0;
if (Convert.ToBoolean(X = 0))
Console.WriteLine(“It is zero”);
else
Console.WriteLine(“It is not zero”);
Console.ReadLine();
}Select correct differences between ‘=’ and ‘==’ in C#.
Select the wrong statement about ‘ref’ keyword in C#?
What will be the output of the following C# code?
static void Main(string[] args)
{
int []a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
func(ref a);
Console.ReadLine();
}
static void func(ref int[] x)
{
Console.WriteLine(” numbers are:”);
for (int i = 0; i < x.Length; i++)
{
if (x[i] % 2 == 0)
{
x[i] = x[i] + 1;
Console.WriteLine(x[i]);
}
}
}What will be the output of the following C# code?
static void Main(string[] args)
{
int[] arr = new int[] {1, 2, 3, 4, 5};
fun1(ref arr);
Console.ReadLine();
}
static void fun1(ref int[] array)
{
for (int i = 0; i < array.Length; i++)
{
array[i] = array[i] + 5;
Console.WriteLine(array[i] + ” “);
}
}What will be the output of the following C# code?
static void Main(string[] args)
{
int a = 5;
fun1 (ref a);
Console.WriteLine(a);
Console.ReadLine();
}
static void fun1(ref int a)
{
a = a * a;
}
Join The Discussion