C# Questions & Answers – For Loop Statements

This section of our 1000+ C# multiple choice questions focuses on for 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;
  4.      for (i = 0;  ; )
  5.      {
  6.          Console.WriteLine("hello");
  7.      }
  8.      Console.ReadLine();
  9.  }

a) No output
b) hello
c) hello printed infinite times
d) Code will give error as expression syntax
View Answer

Answer: c
Explanation: Testing condition for the loop is absent. So, loop will continue executing.
Output :

advertisement
advertisement
         hello
         hello
         hello
         .
         .
         .

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

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

a) 1.1
b) 0.1
c) 0.1 0.2 0.3 0.4 0.5
d) None of the mentioned
View Answer

Answer: a
Explanation: f = 0.1 and ++f = 0.1+1 = 1.1. So, 1.1>0.5, Condition fails and hence loop terminates.
Output :

advertisement
 1.1

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

advertisement
  1.  static void Main(string[] args)
  2.  {
  3.      int I, X;
  4.      for (I = 1; I <= (9 % 2 + I); I++)
  5.      {
  6.          X = (I * 3 + I * 2) / I;
  7.          Console.WriteLine(X);
  8.      }
  9.      Console.ReadLine();
  10.  }

a) Output of code is 5 10
b) Output is 5 5 5 5
c) Print 5 infinite times
d) None of the mentioned
View Answer

Answer: c
Explanation: Testing condition is always incremented i.e i never ‘>’ (9%2+I). So, loop will never terminate.
Output :

 5 5 5.....

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

  1.  static void Main(string[] args)
  2.  {
  3.      int I, J = 0;
  4.      for (I = 1; I < 10; ) ;
  5.      {
  6.          J = J + I;
  7.          I += 2;
  8.      }
  9.      Console.WriteLine("Sum of first 10 even numbers is:"+J);
  10.      Console.ReadLine();
  11.  }

a) 1 2 3 4 5 6 7 8 9
b) 25
c) 1
d) Run time error
View Answer

Answer: d
Explanation: Due to presence of ‘;’ after for()loop condition do not work as according to the statement. Remove the ‘;’.
Output :

 25

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

  1.   static void Main(string[] args)
  2.   {
  3.       int i = 5;
  4.       for (; Convert.ToBoolean(Convert.ToInt32(i)); Console.WriteLine(i--)) ;
  5.       Console.ReadLine();
  6.   }

a) 4 3 2 1
b) 3 2 1
c) 5 4 3 2 1
d) 2 1
View Answer

Answer: c
Explanation: Since, i = 5 and test condition is executed until i!=0. So, i– decrements value of i till condition is satisfied.
Output:

 5 4 3 2 1

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

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

a) Code report error
b) Code runs in infinite loop condition
c) Code gives output as 0 1 3 6 10 15 21 28 36 45
d) Code give output as 55
View Answer

Answer: d
Explanation: Since occurrence of termination symbol(;) at end of for loop.
Output:

 55

7. Which statement is correct among the mentioned statements?
i. The for loop works faster than a while loop
ii. for( ; ; )implements an infinite loop
a) Only i is correct
b) Only ii is correct
c) Both i and ii are correct
d) Both i and ii are incorrect
View Answer

Answer: b
Explanation: By definition.

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

  1.   {
  2.       int i;
  3.       Console.WriteLine("Hi");
  4.       for (i = 1; i <= 10; i++)
  5.           Program.Main(args);
  6.       Console.ReadLine();
  7.   }

a) Prints ‘Hi’ for one time
b) Prints ‘Hi’ for infinite times
c) Stack overflow exception Condition generated
d) None of the mentioned
View Answer

Answer: c
Explanation: Occurrence of ‘main()’ condition after for loop.
Output:

        Hi
        Hi
         .
         .
        stack overflow exception.

9. Which of the C# code should be added to get the following output?

  1.     * * * * *
  2.     * * * *
  3.     * * *
  4.     * *
  5.     *
  6.    static void Main(string[] args)
  7.    {
  8.        int i,j;
  9.      /* Add code here */
  10.  
  11.   }

a)

   for (i = 0;i &lt;= 4; i++)
   {
        for(j = 0;j &lt;= 4; j++)
        console.WriteLine("*");
        console.WriteLine("\n");
    }

b)

   for (i = 0;i &lt;= 4; i++)
   {
        for(j = 4;j &lt;= i; j--)
        console.WriteLine("*");
        console.WriteLine("\n");
   }

c)

   for (i = 0;i &lt;= 4; i++)
   {
        for (j = i;j &lt;= 4; j++)
        console.WriteLine("*");
        console.WriteLine("\n");
    }

d)

   for ( i = 0;i &lt;= 4; i++)
   {
        for (j = 0;j &lt;= i; j++)
        console.WriteLine("*");
        console.WriteLine("\n");
    }
View Answer
Answer: c
Explanation: Input in Console and run the code.
 
 

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

  1.    static void Main(string[] args)
  2.    {
  3.        int i;
  4.        for (i =-3; i <= 3; i++)
  5.        {
  6.            switch (i)
  7.            {
  8.            case 0:
  9.                Console.WriteLine("zero");
  10.                break;
  11.            }
  12.            if (i > 0)
  13.                Console.WriteLine("A");
  14.            else if (i < 0)
  15.                Console.WriteLine("B");
  16.        }
  17.        Console.ReadLine();
  18.    }

a) B B zero A A A
b) B zero A A A
c) B B B zero A A A
d) A A A zero B B B
View Answer

Answer: c
Explanation: for i = -3, -2, -1 statement executed as B. for i = 0, it is zero and for i = 1, 2, 3 again statement printed as A separately for each value of i.
Output:

B B B zero A A A

11. Which of the following is not infinite loop?
a) for( ;’0′; )
b) for( ;0; )
c) for( ;’1′; )
d) for( ;1; )
View Answer

Answer: b
Explanation: None.

12. 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, j = i; i <= 3 && j >= 0; i++, j--)
  5.       {
  6.           if (i == j)
  7.               continue;
  8.           else
  9.               Console.WriteLine(j);
  10.       }
  11.       Console.ReadLine();
  12.   }

a) i = 0, j = 1;
b) i = 1, j = 0;
c) j = 0;
d) None of the mentioned
View Answer

Answer: c
Explanation: Since for i = 1, j = 1 and 1 <= 3 also 1 >= 0 we had i == j. But after i++ and j–. The initial value of ‘j’ which is ‘0’ as j– preferred other than value of ‘j’ in i = j.
Output:

j = 0

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

  1.  static void Main(string[] args)
  2.  {
  3.      int i = -10;
  4.      for ( ;Convert.ToBoolean(Convert.ToInt32(i)) ;Console.WriteLine(i++)) ;
  5.      Console.ReadLine();
  6.  }

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

Answer: c
Explanation: For first value of i = -10. Condition is executed until i!=0.
Output:

-10 -9 -8 -7 -6 -5 -4 -3 -2 -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.