C# multiple choice questions on while loop statements in C# Programming Language.
1. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i, j;
-
for (i = 1; i <= 3; i++)
-
{
-
j = 1;
-
while (i % j == 2)
-
{
-
j++;
-
}
-
Console.WriteLine(i + " " + j);
-
}
-
Console.ReadLine();
-
}
a) 11 21 31
b) 1 12 13 1
c) 11 21 31
d) 1 1 2 1 3 1
Answer: c
Clarification: Since, condition never satisfied for any value of i and j for which (i % j == 2). Hence, j is always constant ‘1’ and ‘i’ increments for i = 1, 2, 3.
Output:
2. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
float s = 0.1f;
-
while (s <= 0.5f)
-
{
-
++s;
-
Console.WriteLine(s);
-
}
-
Console.ReadLine();
-
}
a) 0.1
b) 1.1
c) 0.1 0.2 0.3 0.4 0.5
d) No output
Answer: b
Clarification: For the first while condition check when s = 0. If it is true as control goes inside loop ++s increments value of s by 1 as 1+0.1 = 1.1. So, for next condition while loop fails and hence, prints final value of s as 1.1.
Output:
3. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i;
-
i = 0;
-
while (i++ < 5)
-
{
-
Console.WriteLine(i);
-
}
-
Console.WriteLine("n");
-
i = 0;
-
while ( ++i < 5)
-
{
-
Console.WriteLine(i);
-
}
-
Console.ReadLine();
-
}
a)
1 2 3 4 1 2 3 4 5
b)
1 2 3 1 2 3 4
c)
1 2 3 4 5 1 2 3 4
d)
1 2 3 4 5 1 2 3 4 5
View Answer
Answer: c
Clarification: For while(i++
Output:
4. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int x = 0;
-
while (x < 20)
-
{
-
while (x < 10)
-
{
-
if (x % 2 == 0)
-
{
-
Console.WriteLine(x);
-
}
-
x++;
-
}
-
}
-
Console.ReadLine();
-
}
a)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
b) 0 2 4 6 8 10 12 14 16 18 20
c) 0 2 4 6 8
d) 0 2 4 6 8 10
Answer: c
Clarification: Inner while loop condition checks for even number between 0 an 10 and hence prints number between the given range.
Output:
5. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int x;
-
x = Convert.ToInt32(Console.ReadLine());
-
int c = 1;
-
while (c <= x)
-
{
-
if (c % 2 == 0)
-
{
-
Console.WriteLine("Execute While " + c + "t" + "time");
-
}
-
c++;
-
}
-
Console.ReadLine();
-
}
-
for x = 8.
a)
Execute while 1 time Execute while 3 time Execute while 5 time Execute while 7 time
b)
Execute while 2 time Execute while 4 time Execute while 6 time Execute while 8 time
c)
Execute while 1 time Execute while 2 time Execute while 3 time Execute while 4 time Execute while 5 time Execute while 6 time Execute while 7 time
d)
Execute while 2 time Execute while 3 time Execute while 4 time Execute while 5 time
View Answer
Answer: b
Clarification: Checks condition if number is divisible by 2 then it will print it even number times as given for x = 8 so, prints between 2 to 8 times Similarly, for x = 5, Execute 2 and 4 time.
OUTPUT:
Execute while 2 time. Execute while 4 time. Execute while 6 time. Execute while 8 time.
6. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int n, r;
-
n = Convert.ToInt32(Console.ReadLine());
-
while (n > 0)
-
{
-
r = n % 10;
-
n = n / 10;
-
Console.WriteLine(+r);
-
}
-
Console.ReadLine();
-
}
-
for n = 5432.
a) 3245
b) 2354
c) 2345
d) 5423
Answer: c
Clarification: Reverse of number using while loop.
Output:
7. Correct syntax for while statement is:
a)
-
while
-
{
-
}(condition);
b)
-
while(condition)
-
{
-
};
c)
d)
-
while(condition);
-
{
-
}
View Answer
Answer: c
Clarification: By definition.
8. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
float i = 1.0f, j = 0.05f;
-
while (i < 2.0f && j <= 2.0f)
-
{
-
Console.WriteLine(i++ - ++j);
-
}
-
Console.ReadLine();
-
}
a) 0.05f
b) 1.50f
c) -0.04999995f
d) 1.50f
Answer: c
Clarification: for while(i = 1.0f and j = 0.05f). We, had ‘&&’ condition which gives ‘1’. So, control enters while loop. Since, i = 1 and i++ = first execute then increment. So, first with ‘i’ value as 1.0f and ++j = first increment and then executes we had j = 1.05f and Since operation (i++ – ++j) gives us a negative sign number. So, we can stick our choice to option ‘-0.04999995f’ clearly. Now, as i = 2.0f so loop breaks.
Output:
9. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i = 0;
-
while (i <= 50)
-
{
-
if (i % 10 == 0)
-
continue;
-
else
-
break;
-
i += 10;
-
Console.WriteLine(i % 10);
-
}
-
}
a) code prints output as 0 0 0 0 0
b) code prints output as 10 20 30 40 50
c) infinite loop but doesn’t print anything
d) Code generate error
Answer: c
Clarification: None.
10. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i = 1, j = 1;
-
while (++i <= 10)
-
{
-
j++;
-
}
-
Console.WriteLine(i+ " " +j);
-
Console.ReadLine();
-
}
a) 12 11
b) 10 11
c) 11 10
d) 11 12
Answer: c
Clarification: As ++i, first increments then execute so, for ++i i is 11 and j++ is first execute then increments. So, j = 10.
Output:
11. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i = 1;
-
while (i <= 1)
-
{
-
if ('A' < 'a')
-
{
-
Console.WriteLine("Hello...");
-
}
-
else
-
{
-
Console.WriteLine("Hi...");
-
}
-
i++;
-
}
-
Console.ReadLine();
-
}
a) Hi…
b) Hello….
c) Hi…infinite times
d) Hello infinite times
Answer: b
Clarification: Ascii value of ‘A’ is 65 and ‘a’ is 97. So, clearly ‘A’
Output:
12. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i = 0;
-
while (i++ != 0) ;
-
Console.WriteLine(i);
-
Console.ReadLine();
-
}
a) -127 to +127
b) 0 to 127
c) 1
d) Infinite loop condition
Answer: c
Clarification: i++ = first executes then increments as i = 0. So, i++ != 0, which is false clearly as i = 0. Now, control goes inside loop with i = 1. So, statement prints i = 1.
Output: