TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: 15 C# Questions on For, While and If Else Statements
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
CSharp InterviewWeb Development

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

Last updated: Feb 24, 2024 9:22 pm
By Meenakshi Agarwal
Share
13 Min Read
C# Questions - For, While Loops and If Else Statements.
15 C# Questions - Loops
SHARE

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.

Contents
C# Questions – For, While Loops, and If ElseQ-1. What will be the output of the following code snippet:Q-2. What will be the output of the following code snippet:Q-3. What will be the output of the following code snippet:Q-4. What will be the output of the following code snippet:Q-5. What will be the output of the following code snippet:Q-6. What will be the output of the following code snippet:Q-7. What will be the output of the following code snippet:Q-8. What will be the output of the following code snippet:Q-9. What will be the output of the following code snippet:Q-10. What will be the output of the following code snippet:Q-11. What will be the output of the following code snippet:Q-12. What will be the output of the following code snippet:Q-13. What will be the output of the following code snippet:Q-14. What will be the output of the following code snippet:Q-15. What will be the output of the following code snippet:

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

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

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer. 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.

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer.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

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer. 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

Hover here to view the answer!
Answer. C

Summary – C# Questions on 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

You Might Also Like

Learn Web Development with Engaging Quizzes!

20 Common .NET Coding Interview Questions with Answers

Generate Random Characters With JavaScript Math & Crypto Modules

Discover the Most Asked HTML Interview Questions

JavaScript Interview Questions for 2024

TAGGED:CSharp Interview Questions
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Essential Linux Commands Programmers Should Know 7 Programmer-friendly Linux Commands
Next Article Selenium testing interview questions and answers 10 Selenium Technical Interview Questions and Answers.

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x