π Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
π Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
1. What is the basic structure of a while loop in C#?
Answer:
Explanation:
The while loop in C# starts with the 'while' keyword followed by a condition in parentheses and a block of code in curly braces.
2. How many times does the code inside a while loop execute if the condition is initially false?
Answer:
Explanation:
If the condition of a while loop is false initially, the loop's code block will not execute at all.
3. Which of the following is essential to prevent a while loop from running indefinitely?
Answer:
Explanation:
To prevent an infinite loop, the condition of the while loop must eventually become false, often achieved by modifying a variable within the loop.
4. What is an infinite loop in C#?
Answer:
Explanation:
An infinite loop is a loop where the condition never becomes false, causing it to run indefinitely.
5. What will be the output of the following code?
int i = 1;
while (i <= 3)
{
Console.WriteLine(i);
i++;
}
Answer:
Explanation:
The while loop prints the numbers 1, 2, and 3 as it increments 'i' each time until 'i' is greater than 3.
6. Is it possible to exit a while loop without meeting the condition in C#?
Answer:
Explanation:
The break statement can be used to exit a while loop immediately, regardless of the loop's condition.
7. What happens if the increment statement in a while loop is accidentally omitted?
Answer:
Explanation:
If the increment (or decrement) statement is omitted, the condition of the loop may never become false, leading to an infinite loop.
8. Which of the following statements is true about the while loop in C#?
Answer:
Explanation:
In a while loop, the condition is tested before the execution of the loop's body, making it a pre-test loop.
9. How can you skip the current iteration in a while loop in C#?
Answer:
Explanation:
The continue statement is used to skip the remaining code in the current iteration and proceeds to the next iteration of the loop.
10. Which of the following is a valid while loop in C#?
Answer:
Explanation:
The correct syntax for a while loop includes the condition in parentheses.
11. What is the role of the condition in a while loop?
Answer:
Explanation:
The condition in a while loop determines whether or not the loop's code block should be executed.
12. Can a while loop contain multiple conditions in C#?
Answer:
Explanation:
Multiple conditions in a while loop can be combined using logical operators such as && (and) and || (or).
13. What happens when a return statement is executed inside a while loop?
Answer:
Explanation:
When a return statement is executed inside a while loop, the function containing the loop returns immediately, exiting the loop.
14. In C#, what is the difference between a while loop and a do-while loop?
Answer:
Explanation:
The main difference is that a while loop checks its condition before the first iteration (pre-test), whereas a do-while loop checks its condition after the first iteration (post-test).
15. How can you ensure a while loop executes at least once in C#?
Answer:
Explanation:
To ensure a loop executes at least once regardless of the condition, a do-while loop should be used, as it checks the condition after executing the loop's body.
Comments
Post a Comment
Leave Comment