15 C# Questions on For, While and If Else Statements

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.

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:

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
Answer. D.

 

Q-2. What will be the output of the following code snippet:

Output:

A. if Clause executed
B. else Clause executed
C.  if Clause executed
     Entered Main Function
D. else Clause executed
    Entered Main Function

View result
Answer. C.

 

Q-3. What will be the output of the following code snippet:

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
Answer. C.

Suggested Reading.

Q-4. What will be the output of the following code snippet:

Output:

A. If Condition executed
B. else if executed
C. else clause executed
D. Error

View result
Answer. A.

 

Q-5. What will be the output of the following code snippet:

Output:

A. If Condition is True
B. If Condition is False
C. Compile Time Error
D. Runtime Error

View result
Answer. C.

 

Q-6. What will be the output of the following code snippet:

Output:

A. Compile time error: Misplaced else
B. Compile time error: Undefined symbol
C. If executed Successfully
D. Nothing is printed.

View result
Answer. C.

 

Q-7. What will be the output of the following code snippet:

Output:

A. 1 2
B. 2 2
C. 2 3
D. 2 4

View result
Answer. B.

 

Q-8. What will be the output of the following code snippet:

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
Answer. C.

 

Q-9. What will be the output of the following code snippet:

Output:

A. 1 2 3 4
B. 4 3 2 1
C. 2 3 4 1
D. 3 2 4 1

View result
Answer. B.

 

Q-10. What will be the output of the following code snippet:

Output:

A. 12 11
B. 10 11
C. 11 12
D. 11 10

View result
Answer. D.

 

Q-11. What will be the output of the following code snippet:

Output:

A. First
B. Second
C. Third
D. Fourth
E. Fifth

View result
Answer. D.

 

Q-12. What will be the output of the following code snippet:

Output:

A. A
B. B
C. C
D. D
E. Compilation Error cannot use const

View result
Answer. E.

Suggested Reading.

Q-13. What will be the output of the following code snippet:

Output:

A. 1 3 1
B. 2 3 4
C. 5 3 4
D. Compilation Error

View result
Answer. A.

 

Q-14. What will be the output of the following code snippet:

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
Answer. A.

 

Q-15. What will be the output of the following code snippet:

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
Answer. C.

 

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.

All the Best,

TechBeamers.

Leave a Reply


*