This section of our 1000+ C# multiple choice questions focuses 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
View Answer
Explanation: 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:
11 21 31.
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
View Answer
Explanation: 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:
1.1
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 5View Answer
Explanation: For while(i++ < 5) current value of ‘i’ is checked first and hence prints incremented value afterwards. So, i =1, 2, 3, 4, 5. But, for while(++i < 5) current value is incremented first and then checks that value with given condition and hence then prints that value. So, i = 1, 2, 3, 4.
Output:
1 2 3 4 5 1 2 3 4
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
View Answer
Explanation: Inner while loop condition checks for even number between 0 an 10 and hence prints number between the given range.
Output:
0 2 4 6 8
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 timeView Answer
Explanation: 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
View Answer
Explanation: Reverse of number using while loop.
Output:
2345.
7. Correct syntax for while statement is:
a)
while
{
}(condition);
b)
while(condition)
{
};
c)
while(condition)
{
}
d)
while(condition);
{
}
Explanation: 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
View Answer
Explanation: 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:
-0.04999995f
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
View Answer
Explanation: 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
View Answer
Explanation: As ++i, first increments then execute so, for ++i i is 11 and j++ is first execute then increments. So, j = 10.
Output:
11 10.
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
View Answer
Explanation: Ascii value of ‘A’ is 65 and ‘a’ is 97. So, clearly ‘A’ < ‘a’.
Output:
Hello.
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
View Answer
Explanation: 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:
1.
Sanfoundry Global Education & Learning Series – C# Programming Language.
To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Apply for C# Internship
- Check C# Books
- Check MCA Books