What will be the output of the following C# code?
static void Main(string[] args)
{
int i = 0, j = 0;
l1: while (i < 2)
{
i++;
while (j < 3)
{
Console.WriteLine(“loop\n”);
goto l1;
}
}
Console.ReadLine();
}
a) loop is printed infinite times
b) loop
c) loop loop
d) Compile time error
Answer: c
Explanation: Since outer while loop i.e while(i<2) executes only for two times. Hence, loop while executing third time for (j<3) could not be able to satisfy condition i<2 as i = 2. Hence, loop breaks and control goes out of loop.
Output :
loop loop
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