C# multiple choice questions on for loop statements in C# Programming Language.
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
Clarification: Testing condition for the loop is absent. So, loop will continue executing.
Output :
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
Clarification: f = 0.1 and ++f = 0.1+1 = 1.1. So, 1.1>0.5, Condition fails and hence loop terminates.
Output :
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
Clarification: Testing condition is always incremented i.e i never ‘>’ (9%2+I). So, loop will never terminate.
Output :
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
Clarification: Due to presence of ‘;’ after for()loop condition do not work as according to the statement. Remove the ‘;’.
Output :
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
Clarification: Since, i = 5 and test condition is executed until i!=0. So, i– decrements value of i till condition is satisfied.
Output:
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
Clarification: Since occurrence of termination symbol(;) at end of for loop.
Output:
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
Clarification: 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
Clarification: 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?
-
* * * * *
-
* * * *
-
* * *
-
* *
-
* -
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"); }
c)
for (i = 0;i <= 4; i++) { 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"); }
View Answer
Answer: c
Clarification: Input in Console and run the code.
10. What will be the output of the following C# 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
Answer: c
Clarification: 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:
11. Which of the following is not infinite loop?
a) for( ;’0′; )
b) for( ;’0′; )
c) for( ;’1′; )
d) for( ;’1′; )
Answer: b
Clarification: None.
12. What will be the output of the following C# 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
Answer: c
Clarification: Since for i = 1, j = 1 and 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:
13. 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
Clarification: For first value of i = -10. Condition is executed until i!=0.
Output:
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
