C# Questions & Answers on While Loop Statements for Freshers

1. Select the output for the following set of 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
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:
  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
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:
  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) 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
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:
  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
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 :
  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
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:
  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
Answer: c

Explanation: Reverse of number using while loop.
Output: 2345.
7. Correct syntax for while statement is:
  1.  a) while
  2.     {
  3.  
  4.  
  5.  
  6.      }(condition);
  7.  b) while(condition)
  8.     {
  9.  
  10.  
  11.      };
  12.  c) while(condition)
  13.     {
  14.  
  15.  
  16.      }
  17.  d) while(condition);
  18.     {
  19.  
  20.  
  21.      }

Answer:c

Explanation: By defination.
 
 
8. Select the output for the following set of 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
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 :
  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 donot print anything
d) Code generate error
Answer: c
10. Select the output for the following set of 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
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 :
  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
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:
  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
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.

Related

Multiple Choice Questions 3971785784780272281

Post a Comment

emo-but-icon

item