C# Questions & Answers on For Loop Statements for Freshers

https://www.computersprofessor.com/2018/06/c-questions-answers-on-for-loop.html
1. Select the output for the following set of 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
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. Select the output for the following set of 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
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.
Output : 1.1
3. Select the output for the following set of 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
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. Select output for the following set of 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
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 ‘;’.
Output : 25.
5. Select the output for the following set of 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
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. Select the output for the following set of 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
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?
1. The for loop works faster than a while loop
2. for( ; ; )implements an infinite loop
a) Only 1 is correct
b) Only 2 is correct
c) Both 1 and 2 are correct
d) Both 1 and 2 are incorrect
Answer: b
Explanation: By defination.
8. Select the output for the following set of 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 above mentioned
b) Prints ‘Hi’ for infinite times
c) Stack overflow exception Condition generated
d) None of above mentioned
Answer: c
Explanation: Ocurrence of ‘main()’ condition after for loop.
Output: Hi
Hi
.
.
stack overflow exception.
9. Which of the code should be added to get the following output?
* * * * *
* * * *
* * *
* *
*
static void Main(string[] args)
{
int i,j;
/* Add code here */
}
a) for (i = 0;i <= 4; i++)
{
for(j = 0;j <= 4; j++)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
b) for (i = 0;i <= 4; i++)
{
for(j = 4;j <= i; j–)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
{
for(j = 0;j <= 4; j++)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
b) for (i = 0;i <= 4; i++)
{
for(j = 4;j <= i; j–)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
c) for (i = 0;i <= 4; i++)
{
for (j = i;j <= 4; j++)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
{
for (j = i;j <= 4; j++)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
d) for ( i = 0;i <= 4; i++)
{
for (j = 0;j <= i; j++)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
{
for (j = 0;j <= i; j++)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
Answer: c
Explanation: Input in Console and run the code.
10. Predict the output for the following set of code :
static void Main(string[] args)
{
int i;
for (i =-3; i <= 3; i++)
{
switch (i)
{
case 0:
Console.WriteLine("zero");
break;
}
if (i > 0)
Console.WriteLine("A");
else if (i < 0)
Console.WriteLine("B");
}
Console.ReadLine();
}
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
b) B zero A A A
c) B B B zero A A A
d) A A A zero B B B
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 seperately 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′; )
Answer: b
12. Select the output for the following set of code :
static void Main(string[] args)
{
int i, j;
for (i = 1, j = i; i <= 3 && j >= 0; i++, j--)
{
if (i == j)
continue;
else
Console.WriteLine(j);
}
Console.ReadLine();
}
a) i = 0, j = 1;
b) i = 1, j = 0;
c) j = 0;
d) None of the mentioned
b) i = 1, j = 0;
c) j = 0;
d) None of the mentioned
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. Select the output for the following set of 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
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.