15 C# Questions - For, While Loops and If Else Statements

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

9/15/2019 15 C# Questions - For, While Loops and If Else Statements

🏠 Python ▾ Selenium QA Agile Angular SQL MySQL Java Linux C C# Web PHP Android

15 C# Questions on For, While and If Else Statements Example: Python sockets


SPONSORED SEARCHES

practice exam test questions c# interview questions

java programming code sample interview questions and answers

C# Interview | By Meenakshi Agarwal

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.

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 no. 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.

15 C# Questions – For, While Loops And If Else Statements

15 C# Questions – Loops

Q-1. What Will Be The Output Of The Following Code Snippet:

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

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

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

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

View result

Suggested Reading.

💡 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;
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

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

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

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.

View result

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(+
{
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

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

View result

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
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

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

View result

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();
}
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

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

View result

Suggested Reading.

💡 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
{
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

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

View result

Q-15. What Will Be The Output Of The Following Code Snippet: ▲

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

Summary – 15 C# Questions – For, While Loops And If Else Statements


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

All the Best,

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

Tutorials Interviews Questions Web Development Quick Info


☛ About Us
Python Tutorial Python Interview Questions AngularJS Questions ☛ Privacy Policy
☛ Disclaimer
Selenium Python Tutorial SQL Interview Questions JavaScript Questions
☛ Contact Us
Selenium Webdriver Tutorial Selenium Interview Questions Web Developer Questions

Selenium Demo Websites QA Interview Questions NodeJS Interview Questions

Python Multithreading Manual Testing Questions HTML Interview Questions

Java Multithreading Rest API Interview Questions PHP Interview Questions-1

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

You might also like