C# Programming Questions and Answers - For Loop Statements

1. What will be the output of the following C# code?
static void Main(string[] args)
{
int i;
for (i = 0; ; )
{
Console.WriteLine("hello");
}
Console.ReadLine();
}
a) No output
b) hello
c) hello printed infinite times
d) Code will give error as expression syntax

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

2. What will be the output of the following C# code?
static void Main(string[] args)
{
float f;
for (f = 0.1f; f <= 0.5; f += 1)
Console.WriteLine( ++f );
Console.ReadLine();
}
a) 1.1
b) 0.1
c) 0.1 0.2 0.3 0.4 0.5
d) None of the mentioned

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

3. What will be the output of the following C# code?
static void Main(string[] args)
{
int I, X;
for (I = 1; I <= (9 % 2 + I); I++)
{
X = (I * 3 + I * 2) / I;
Console.WriteLine(X);
}
Console.ReadLine();
}

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

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?
static void Main(string[] args)
{
int I, J = 0;
for (I = 1; I < 10; ) ;
{
J = J + I;
I += 2;
}
Console.WriteLine("Sum of first 10 even numbers is:"+J);
Console.ReadLine();
}
a) 1 2 3 4 5 6 7 8 9
b) 25
c) 1
d) Run time error

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

5. What will be the output of the following C# code?
static void Main(string[] args)
{
int i = 5;
for (; Convert.ToBoolean(Convert.ToInt32(i)); Console.WriteLine(i--)) ;
Console.ReadLine();
}
a) 4 3 2 1
b) 3 2 1
c) 5 4 3 2 1
d) 2 1

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?
static void Main(string[] args)
{
int i, s = 0;
for (i = 1; i <= 10; s = s + i, i++);
{
Console.WriteLine(s);
}
Console.ReadLine();
}
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

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

Answer: b
Explanation: By definition.

8. What will be the output of the following C# code?
{
int i;
Console.WriteLine("Hi");
for (i = 1; i <= 10; i++)
Program.Main(args);
Console.ReadLine();
}
a) Prints ‘Hi’ for one time
b) Prints ‘Hi’ for infinite times
c) Stack overflow exception Condition generated
d) None of the mentioned

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

9. Which of the following is not infinite loop?
a) for( ;’2′; )
b) for( ;’0′; )
c) for( ;’1′; )
d) None of the above

Answer: b
Explanation: for( ;’0′; )

10. What will be the output of the following C# code?
static void Main(string[] args)
{
int i = -10;
for ( ;Convert.ToBoolean(Convert.ToInt32(i)) ;Console.WriteLine(i++)) ;
Console.ReadLine();
}
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

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