C# Questions & Answers on While Loop Statements for Freshers

https://www.computersprofessor.com/2018/06/c-questions-answers-on-while-loop.html
1. Select the output for the following set of 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
b) 1 12 13 1
c) 11 21 31
d) 1 1 2 1 3 1
Answer: c
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. Select the output for the following set of 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
b) 1.1
c) 0.1 0.2 0.3 0.4 0.5
d) No output
Answer: b
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. Select the output for the following set of 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
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
Answer: c
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. Select the output for the following set of 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
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
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. Predict the output for the following set of 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
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
Answer: b
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. Select the output for the following set of 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
b) 2354
c) 2345
d) 5423
Answer: c
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);
{
}
Answer:c
Explanation: By defination.
8. Select the output for the following set of 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
b) 1.50f
c) -0.04999995f
d) 1.50f
Answer: c
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 ‘c’ clearly. Now, as i = 2.0f so loop breaks.
Output:-0.04999995f.
9. Select the output for the following set of 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 donot print anything
d) Code generate error
b) code prints output as 10 20 30 40 50
c) infinite loop but donot print anything
d) Code generate error
Answer: c
10. Select the output for the following set of 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
b) 10 11
c) 11 10
d) 11 12
Answer: c
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. Select the output for following set of 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
b) Hello….
c) Hi…infinite times
d) Hello infinite times
Answer: b
Explanation: Ascii value of ‘A’ is 65 and ‘a’ is 97.So, clearly ‘A’ < ‘a’.
Output:Hello.
12. Select the output for the following set of codes:
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
b) 0 to 127
c) 1
d) Infinite loop condition
Answer: c
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.