15 C# Questions - For, While Loops and If Else Statements
15 C# Questions - For, While Loops and If Else Statements
15 C# Questions - For, While Loops and If Else Statements
🏠 Python ▾ Selenium QA Agile Angular SQL MySQL Java Linux C C# Web PHP Android
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 difference, then do let us know as well.
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.
15 C# Questions – Loops
using System;
namespace ProgrammingExercise
{ ▲
class FindOutput
{
https://www.techbeamers.com/c-sharp-questions-for-while-loop-statements/ 1/11
9/15/2019 15 C# Questions - For, While Loops and If Else Statements
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
View result
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
https://www.techbeamers.com/c-sharp-questions-for-while-loop-statements/ 2/11
9/15/2019 15 C# Questions - For, While Loops and If Else Statements
View result
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
View result
Suggested Reading.
using System;
namespace ProgrammingExercise
{
class FindOutput
{
▲
static void Main(string[] args)
{
int val = 5;
https://www.techbeamers.com/c-sharp-questions-for-while-loop-statements/ 3/11
9/15/2019 15 C# Questions - For, While Loops and If Else Statements
int val = 5;
if (Convert.ToBoolean((.002f) -(0.1f)))
Console.WriteLine("If Condition executed");
else if (val == 5)
Console.WriteLine("else if executed");
else
Console.WriteLine("else clause executed");
Console.ReadLine();
}
}
}
Output:
A. If Condition executed
B. else if executed
C. else clause executed
D. Error
View result
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
View result
▲
https://www.techbeamers.com/c-sharp-questions-for-while-loop-statements/ 4/11
9/15/2019 15 C# Questions - For, While Loops and If Else Statements
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:
View result
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(+
{
Console.WriteLine(a + "\n" + b);
}
else
Console.WriteLine(" C# questions ");
}
}
}
Output:
A. 1 2
B. 2 2
C. 2 3
D. 2 4
View result
https://www.techbeamers.com/c-sharp-questions-for-while-loop-statements/ 5/11
9/15/2019 15 C# Questions - For, While Loops and If Else Statements
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
View result
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
https://www.techbeamers.com/c-sharp-questions-for-while-loop-statements/ 6/11
9/15/2019 15 C# Questions - For, While Loops and If Else Statements
C. 2 3 4 1
D. 3 2 4 1
View result
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
View result
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();
}
https://www.techbeamers.com/c-sharp-questions-for-while-loop-statements/ 7/11
9/15/2019 15 C# Questions - For, While Loops and If Else Statements
}
}
Output:
A. First
B. Second
C. Third
D. Fourth
E. Fifth
View result
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
View result
Suggested Reading.
using System;
namespace ProgrammingExercise
{ ▲
class FindOutput
{
https://www.techbeamers.com/c-sharp-questions-for-while-loop-statements/ 8/11
9/15/2019 15 C# Questions - For, While Loops and If Else Statements
{
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
View result
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
View result
using System;
https://www.techbeamers.com/c-sharp-questions-for-while-loop-statements/ 9/11
9/15/2019 15 C# Questions - For, While Loops and If Else Statements
g y ;
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
View result
If you like to share any feedback or an idea about a post, then use the comment box and let your voice
reach us.
TechBeamers
https://www.techbeamers.com/c-sharp-questions-for-while-loop-statements/ 10/11
9/15/2019 15 C# Questions - For, While Loops and If Else Statements
RELATED
15 C# Interview Questions Test C# Programming Skills –
Every Programmer Should 15 Questions On Classes For
Know Beginners
Python Tips & Tricks Linux Interview Questions PHP Interview Questions-2
© 2019 TechBeamers.com
https://www.techbeamers.com/c-sharp-questions-for-while-loop-statements/ 11/11