C# Questions & Answers – While Loop Statements

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?

  1.  static void Main(string[] args)
  2.  {
  3.      int i, j;
  4.      for (i = 1; i <= 3; i++)
  5.      {
  6.          j = 1;
  7.          while (i % j == 2)
  8.          {
  9.              j++;
  10.          }
  11.          Console.WriteLine(i + " " + j);
  12.      }
  13.      Console.ReadLine();
  14.  }

a) 11 21 31
b) 1 12 13 1
c) 11 21 31
d) 1 1 2 1 3 1
View Answer

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. What will be the output of the following C# code?

advertisement
advertisement
  1.  static void Main(string[] args)
  2.  {
  3.      float s = 0.1f;
  4.      while (s <= 0.5f)
  5.      {
  6.          ++s;
  7.          Console.WriteLine(s);
  8.      }
  9.      Console.ReadLine();
  10.  }

a) 0.1
b) 1.1
c) 0.1 0.2 0.3 0.4 0.5
d) No output
View Answer

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:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
 1.1

3. What will be the output of the following C# code?

advertisement
  1.  static void Main(string[] args)
  2.  {
  3.      int i;
  4.      i = 0;
  5.      while (i++ < 5)
  6.      {
  7.          Console.WriteLine(i);
  8.      }
  9.      Console.WriteLine("\n");
  10.      i = 0;
  11.      while ( ++i < 5)
  12.     {
  13.         Console.WriteLine(i);
  14.     }
  15.     Console.ReadLine();
  16. }

a)

advertisement
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
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?

  1.  static void Main(string[] args)
  2.  {
  3.      int x = 0;  
  4.      while (x < 20)
  5.      {
  6.          while (x < 10)
  7.          {
  8.              if (x % 2 == 0)
  9.              {
  10.                  Console.WriteLine(x);
  11.              }
  12.              x++;
  13.          }
  14.      }
  15.      Console.ReadLine();
  16.  }

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

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. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      int x;
  4.      x = Convert.ToInt32(Console.ReadLine());
  5.      int c = 1;
  6.      while (c <= x)
  7.      {
  8.          if (c % 2 == 0)
  9.          {
  10.              Console.WriteLine("Execute While " + c + "\t" + "time");
  11.          }
  12.          c++;
  13.      }
  14.      Console.ReadLine();
  15.  }
  16. 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
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?

  1.   static void Main(string[] args)
  2.   {
  3.       int n, r;
  4.       n = Convert.ToInt32(Console.ReadLine());
  5.       while (n > 0)
  6.       {
  7.           r = n % 10;
  8.           n = n / 10;
  9.           Console.WriteLine(+r);
  10.       }
  11.       Console.ReadLine();
  12.   }
  13.  for n = 5432.

a) 3245
b) 2354
c) 2345
d) 5423
View Answer

Answer: c
Explanation: Reverse of number using while loop.
Output:

2345.

7. Correct syntax for while statement is:
a)

  1.     while
  2.     {
  3.  
  4.  
  5.  
  6.      }(condition);

b)

  1.     while(condition)
  2.     {
  3.  
  4.  
  5.      };

c)

  1.     while(condition)
  2.     {
  3.  
  4.  
  5.      }

d)

  1.     while(condition);
  2.     {
  3.  
  4.  
  5.      }
View Answer
Answer: c
Explanation: By definition.
 
 

8. What will be the output of the following C# code?

  1.   static void Main(string[] args)
  2.   {
  3.       float i = 1.0f,  j = 0.05f;
  4.       while (i  < 2.0f  &&  j  <= 2.0f)
  5.       {
  6.           Console.WriteLine(i++ - ++j);
  7.       }
  8.       Console.ReadLine();
  9.   }

a) 0.05f
b) 1.50f
c) -0.04999995f
d) 1.50f
View Answer

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 ‘-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?

  1.  static void Main(string[] args)
  2.  {
  3.      int i = 0;
  4.      while (i <= 50)
  5.      {
  6.          if (i % 10 == 0)
  7.          continue;
  8.          else
  9.          break;
  10.          i += 10;
  11.          Console.WriteLine(i % 10);
  12.     }
  13. }

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

Answer: c
Explanation: None.

10. What will be the output of the following C# code?

  1.   static void Main(string[] args)
  2.   {
  3.       int i = 1, j = 1;
  4.       while (++i <= 10)
  5.       {
  6.           j++;
  7.       }
  8.       Console.WriteLine(i+ "  " +j);
  9.       Console.ReadLine();
  10.   }

a) 12 11
b) 10 11
c) 11 10
d) 11 12
View Answer

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. What will be the output of the following C# code?

  1.    static void Main(string[] args)
  2.    {
  3.        int i = 1;
  4.        while (i <= 1)
  5.        {
  6.            if ('A' < 'a')
  7.            {
  8.                Console.WriteLine("Hello...");
  9.            }
  10.            else
  11.            {
  12.               Console.WriteLine("Hi...");
  13.            }
  14.               i++;
  15.        }
  16.        Console.ReadLine();
  17.    }

a) Hi…
b) Hello….
c) Hi…infinite times
d) Hello infinite times
View Answer

Answer: b
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?

  1.   static void Main(string[] args)
  2.   {
  3.       int i = 0;
  4.       while (i++ != 0) ;
  5.       Console.WriteLine(i);
  6.       Console.ReadLine();
  7.   }

a) -127 to +127
b) 0 to 127
c) 1
d) Infinite loop condition
View Answer

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.

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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.