C# Questions & Answers – Do While Loop Statements

This section of our 1000+ C# multiple choice questions focuses on do 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 = 1, j = 2, k = 3;
  4.      do
  5.      {
  6.          Console.WriteLine((Convert.ToBoolean(Convert.ToInt32(i++))) 
  7.          && (Convert.ToBoolean(Convert.ToInt32(++j))));
  8.      }while (i <= 3);
  9.      Console.ReadLine();
  10.  }

a) 0 0 0
b) True True True
c) 1 1 1
d) False False False
View Answer

Answer: b
Explanation: 1 AND 1 = True. Similarly, non zero number || non zero number = True.
Output:

True True True

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

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

a) 0.05
b) -0.05
c) 0.95
d) -0.04999995
View Answer

Answer: d
Explanation: None.
Output :

Note: Join free Sanfoundry classes at Telegram or Youtube
 -0.04999995

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

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

a) 5 10 15 20 25 30 35 40 45 50
b) 5 25
c) 5 11 16 21 26 31 36 41 46 51
d) 5 30
View Answer

Answer: b
Explanation: For first step of loop i = 1 .So, i++ * j = 1 * 5 = 5. For second step of loop i = 5, j = 5. So, i++ * j = 25. As, i = 25 hence, 25 >=10 loop condition breaks.
Output:

advertisement
5 25.

4. For the incomplete C# program below, which of the C# code fragment will not result in an infinite loop?

  1.  static void Main(string[] args)
  2.  {
  3.      int i = 1234 ,j = 0;
  4.       /*ADD CODE HERE */
  5.      Console.WriteLine(j);
  6.  }

a)

 do
    {
        j = j + (i % 10);
    }while ((i = i / 10)!= 0);

b)

do
    {
        j = j + (i % 10);
    }while ((i / 10)!= 0);

c)

do
    {
        j = j + (i % 10);
    }while ((i % 10)!= 0);

d)

do
    {
        j = j + (i % 10);
    }while ((i/10 == 0)!= 0);
View Answer
Answer: a
Explanation: None.
Output :

static void Main(string[] args)
        {
              int i = 1234,j = 0;
              do
              {
                  j = j +( i % 10);
 
              }while ((i = i / 10)!= 0);
              Console.WriteLine(j);

}

 
 

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

  1.  static void Main(string[] args)
  2.  {
  3.      long  x;
  4.      x = Convert.ToInt32(Console.ReadLine());
  5.      do
  6.      {
  7.          Console.WriteLine(x % 10);
  8.      }while ((x = x / 10) != 0);
  9.      Console.ReadLine();
  10.  }
  11.  enter x = 1234.

a) number of digits present in x
b) prints ‘1’
c) prints reverse of x
d) prints sum of digits of ‘x’
View Answer

Answer: c
Explanation: Reverse of digits using while loop statements.
Output:

 4321.

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

  1.  static void Main(string[] args)
  2.  {
  3.      int i, s = 0, a = 1, d;
  4.      i = Convert.ToInt32(Console.ReadLine());
  5.      do
  6.      {
  7.          d = i % (2 * 4);
  8.          s = s + d * a;
  9.      }while ((Convert.ToInt32(i = i / (2 * 4))) != 0 
  10.       && (Convert.ToBoolean(Convert.ToInt32((a) = (a * 10)))));
  11.      Console.WriteLine(s);
  12.      Console.ReadLine();
  13.  }
  14. enter i = 342.

a) It finds binary equivalent of i
b) It finds octal equivalent of i
c) It finds sum of digits of i
d) It finds reverse of i
View Answer

Answer: b
Explanation: None.
Output :

         i = 342.
         s = 526.

7. What is the correct syntax for do while loop?
a)

   do;
   {
    statement;
   }while (condition);

b)

   do(condition)
   {
     statement;
   }while;

c)

   do
   {
     statement;
   }while (condition)

d)

   do
   {
        statement;
   }while (condition);
View Answer
Answer: d
Explanation: By definition
Output:

do
       {
          statement;
        }while (condition);
 
 

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

  1. static void Main(string[] args)
  2. {
  3.     int x = 10;
  4.     do
  5.     {
  6.         Console.WriteLine( x++);
  7.     }
  8.     while(Convert.ToBoolean(5) && Convert.ToBoolean(4) && Convert.ToBoolean(3) 
  9.     && Convert.ToBoolean(2) && Convert.ToBoolean(1) && Convert.ToBoolean(0));    
  10.     Console.ReadLine();
  11. }

a) 13
b) 15
c) 11
d) 10
View Answer

Answer: d
Explanation: Here in do while condition ‘&&’ i.e ‘AND’operator return ‘0’ i.e false. So, as condition is false so program comes out of the loop.
Output :

10.

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

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

a) 0 0 0….infinite times
b) 1 1 1….infinite times
c) 1 1 1 1 1 1
d) System outofflow exception error
View Answer

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

1 1 1 1 1 1

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

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

a) 1 2 3 4 5
b) 10
c) 5 6 7 8 9 10
d) 1 2 3 4 5 6 7 8 9 10
View Answer

Answer: d
Explanation: The condition will print the numbers from 1 to 10 when x == 5 and when x does not satisfy if condition until x < 10.
Output:

1 2 3 4 5 6 7 8 9 10

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

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

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

Answer: c
Explanation: None.
Output :

 12 22 32

Sanfoundry Global Education & Learning Series – C# Programming Language.

Here’s the list of Best Books in C# Programming Language.

To practice all features of C# programming language, here is complete set on 1000+ Multiple Choice Questions and Answers on C#.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.