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 x;
for (x = 10; x <= 15; x++)
while (Convert.ToBoolean(Convert.ToInt32(x)))
{
do
{
Console.WriteLine(1);
if (Convert.ToBoolean(x >> 1))
continue;
}while (Convert.ToBoolean(0));
break;
}
Console.ReadLine();
}

What will be the output of the following C# code?
static void Main(string[] args)
{
int x;
for (x = 10; x <= 15; x++)
while (Convert.ToBoolean(Convert.ToInt32(x)))
{
do
{
Console.WriteLine(1);
if (Convert.ToBoolean(x >> 1))
continue;
}while (Convert.ToBoolean(0));
break;
}
Console.ReadLine();
}
a) 0 0 0….infinite times
b) 1 1 1….infinite times
c) 1 1 1 1 1 1
d) System outofflow exception error

Answer: c
Explanation: The execution of for loop is done for six consecutive times.
Output : 1 1 1 1 1 1

Join The Discussion