Check out 15 C# Questions – For, While Loops, and If Else Statements. These questions will help you to test and improve your C# programming skills.
Loops and conditional constructs are an integral part of any programming language. So you need to know how to use them efficiently to produce scalable and quality. A good programmer should be able to decide when to choose a for loop and when should he prefer the while loop.
But before you move to the questions and answers section, please check below if you’ve not read the basic differences between the two types of loops. Also, if you are aware of any other differences, then do let us know as well.
For Vs While Loops in C#
There is a semantic difference between a for and while loop which you must understand. While loops are meant to handle an indefinite number of iterations. So you should use it in cases like while reading a file to its EOF. Whereas, the for loops are more appropriate to tackle definite no. of operations. For example – traversing through the elements in a list.
Similarly, there are ways to use an if…else block so that your code can produce correct results with minimum iterations.
Let’s now begin to read the top 15 C# questions – for, while loops and conditional operators.
C# Questions – For, While Loops, and If Else
Q-1. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int i; int div = 8, num = 32; for (i = 0; i <= 10; i++) { if ((num / div * 3)== 6) { Console.WriteLine( i + " "); continue; } else if (i != 5) Console.Write(i + " "); else break; } Console.ReadLine(); } } }
Output:
A. 1 2 3 4 5 6 7 8 9 10
B. 0 1 2 3 4 5 6 7 8 9 10
C. 0 1 2 3 4 5
D. 0 1 2 3 4
Show Answer
Option – D
Q-2. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int i = 30; int j = 5 % 5; if (Convert.ToBoolean(Convert.ToInt32(i != j))) { Console.WriteLine("if clause executed"); } else { Console.WriteLine("else clause executed"); } Console.WriteLine("Entered Main Function"); Console.ReadLine(); } } }
Output:
A. If clause executed
B. else clause executed
C. If clause executed
Entered Main Function
D. else clause executed
Entered Main Function
Show Answer
Option – C
Q-3. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int a, b; for (a = 2; a >= 0; a--) { for (b = 0; b <= 2; b++) { if (a == b) { Console.WriteLine("1"); } else { Console.WriteLine("0"); } } Console.WriteLine("\n"); } } } }
Output:
A. 1 0 0
0 1 0
0 0 1
B. 0 1 0
1 0 0
0 0 1
C. 0 0 1
0 1 0
1 0 0
D. 1 0 0
0 0 1
0 1 0
Show Answer
Option – C
Also Read: 15 C# Subjective Questions and Answers
Q-4. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int val = 5; if (Convert.ToBoolean((.002f) -(0.1f))) Console.WriteLine("The 'if condition' executed"); else if (val == 5) Console.WriteLine("The 'else if' executed"); else Console.WriteLine("The 'else clause' executed"); Console.ReadLine(); } } }
Output:
A. The ‘if condition’ executed
B. The ‘else if’ executed
C. The ‘else clause’ executed
D. Error
Show Answer
Option – A
Q-5. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int a = -2; int b = -1; if (Convert.ToBoolean (++a = ++b)) Console.WriteLine("If Condition is True"); else Console.WriteLine("If Condition is False"); Console.ReadLine(); } } }
Output:
A. If Condition is True
B. If Condition is False
C. Compile Time Error
D. Runtime Error
Show Answer
Option – C
Q-6. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { if (Convert.ToBoolean(Convert.ToInt32(0xB))) if (Convert.ToBoolean(Convert.ToInt32(022))) if (Convert.ToBoolean(Convert.ToInt32('\xeb'))) Console.WriteLine("If executed Successfully"); else ; else ; else ; } } }
Output:
A. Compile time error: Misplaced else
B. Compile time error: Undefined symbol
C. If executed Successfully
D. Nothing is printed.
Show Answer
Option – C
Q-7. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int a = 1, b = 2; if (Convert.ToBoolean(Convert.ToInt32(++a)) || Convert.ToBoolean(Convert.ToInt32(++b))) { Console.WriteLine(a + "\n" + b); } else Console.WriteLine(" C# questions "); } } }
Output:
A. 1 2
B. 2 2
C. 2 3
D. 2 4
Show Answer
Option – B
Q-8. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int i; i = 0; while (i++ < 5) { Console.WriteLine(i); } Console.WriteLine("\n"); i = 0; while ( ++i < 5) { Console.WriteLine(i); } Console.ReadLine(); } } }
Output:
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
Show Answer
Option – C
Q-9. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int num= 1234, r; while (num > 0) { r = num % 10; num = num / 10; Console.WriteLine(+r); } } } }
Output:
A. 1 2 3 4
B. 4 3 2 1
C. 2 3 4 1
D. 3 2 4 1
Show Answer
Option – B
Q-10. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int i = 1, j = 1; while (++i <= 10) { j++; } Console.WriteLine(i+ " " +j); } } }
Output:
A. 12 11
B. 10 11
C. 11 12
D. 11 10
Show Answer
Option – D
Q-11. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int val = 1; switch (val << 2 + val) { default: Console.WriteLine("First"); break; case 4: Console.WriteLine("Second"); break; case 5: Console.WriteLine("Third"); break; case 8: Console.WriteLine("Fourth"); break; case 9: Console.WriteLine("Fifth"); break; } Console.ReadLine(); } } }
Output:
A. First
B. Second
C. Third
D. Fourth
E. Fifth
Show Answer
Option – D
Q-12. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int const c = 2; switch (c) { case c: Console.WriteLine("A"); break; case c * 1: Console.WriteLine("B"); break; case c - 2: Console.WriteLine("C"); break; default: Console.WriteLine("D"); } } } }
Output:
A. A
B. B
C. C
D. D
E. Compilation Error cannot use const
Show Answer
Option – E
Also Read: 15 C# Coding Related Questions and Answers
Q-13. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int a = 2, b = 3, c = 4; switch (a + b - c) { case 0: case 2: case 4: ++a; c += b; break; case 1: case 3: case 5 : --a; c -= b; break; default: a += b; break; } Console.WriteLine(a + "\n" + b + "\n" + c); } } }
Output:
A. 1 3 1
B. 2 3 4
C. 5 3 4
D. Compilation Error
Show Answer
Option – A
Q-14. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int i = 1, j = 5; do { Console.WriteLine(i = i++ * j); }while (i <= 10); } } }
Output:
A. 5 25
B. 5 10 15 20 25 30 35 40 45 50
C. 25 30
D. 5 11 16 21 26 31 36 41 46 51
Show Answer
Option – A
Q-15. What will be the output of the following code snippet:
using System; namespace ProgrammingExercise { class FindOutput { static void Main(string[] args) { int a; for (a = 10; a <= 15; a++) while (Convert.ToBoolean(Convert.ToInt32(a))) { do { Console.WriteLine(1); if (Convert.ToBoolean(a >> 1)) continue; }while (Convert.ToBoolean(0)); break; } } } }
Output:
A. 0 0 0……infinite times
B. 1 1 1 1…..infinite times
C. 1 1 1 1 1 1
D. Exception
Show Answer
Option – C
Before You Leave
We hope you have enjoyed the above set of questions. Here, we’ve tried to cover all types of loops and conditional statements that C# language supports.
If you like to share any feedback or an idea about a post, then use the comment box and let your voice reach us. Check out more CSharp Q&A on various programming topics:
1. 50 CSharp Coding Interview Questions
2. 15 CSharp Programming Interview Questions
3. 15 CSharp Interview Questions Every Programmer
4. 15 CSharp Questions on For, While, and If Else
5. 15 CSharp Programming Questions on Classes
6. 20 CSharp OOPS Interview Questions Set-1
7. 20 CSharp OOPS Interview Questions Set-2
8. 25 CSharp OOPs Interview Questions Set-3
9. 35 CSharp Exception Handling Questions
Finally, to keep our site free, we need your support. If you found this tutorial helpful, please share it on LinkedIn or YouTube.
All the Best,
TechBeamers.