1. What is the primary characteristic of a 'while' loop in C?
Answer:
Explanation:
A 'while' loop in C checks the condition before the loop body is executed. If the condition is false initially, the loop body does not execute at all.
2. How does a 'do…while' loop differ from a 'while' loop in C?
Answer:
Explanation:
The 'do…while' loop in C checks the condition after the loop body has executed, ensuring that the loop body is executed at least once.
3. What is the correct syntax for a 'while' loop in C?
Answer:
Explanation:
The correct syntax for a 'while' loop includes the 'while' keyword followed by the condition in parentheses and the loop body either in braces or as a single statement.
4. What is the correct syntax for a 'do…while' loop in C?
Answer:
Explanation:
The 'do…while' loop begins with 'do', followed by the loop body in braces, and ends with 'while' and the condition in parentheses, followed by a semicolon.
5. Which type of loop is ideal when the number of iterations is not known in advance?
Answer:
Explanation:
While and do/while loops are ideal for situations where the number of iterations is not predetermined, as they depend on a condition rather than a counter.
6. In a 'while' loop, if the condition never becomes false, what happens?
Answer:
Explanation:
If the condition in a 'while' loop never becomes false, the loop will continue to execute indefinitely, resulting in an infinite loop.
7. What will the following C code snippet print?
int i = 0;
while (i < 3) {
printf("%d", i);
i++;
}
Answer:
Explanation:
The loop prints the values of 'i' from 0 to 2 and then exits when 'i' becomes 3, resulting in the output "012".
8. Which of the following is a valid 'do…while' loop in C?
Answer:
Explanation:
A valid 'do…while' loop in C must end with a semicolon after the condition enclosed in parentheses.
9. How can you exit a 'while' loop prematurely in C?
Answer:
Explanation:
The 'break' statement is used to exit a loop prematurely, immediately stopping the loop and moving to the next statement after the loop.
10. What is the role of the 'continue' statement in a 'while' loop in C?
Answer:
Explanation:
The 'continue' statement skips the remaining statements in the current loop iteration and proceeds with the next iteration of the loop.
11. What is the minimum number of times a 'do…while' loop is guaranteed to execute?
Answer:
Explanation:
A 'do…while' loop is guaranteed to execute at least once, regardless of the condition, as the condition is checked after the first execution.
12. Which of the following is an infinite loop in C?
Answer:
Explanation:
'while (1)' creates an infinite loop because the condition '1' (which is always true) never becomes false.
13. What happens if the condition in a 'while' loop is always true?
Answer:
Explanation:
If the condition in a 'while' loop is always true, the loop will continue to execute indefinitely, creating an infinite loop.
14. Can a 'while' loop be used to iterate over an array in C?
Answer:
Explanation:
A 'while' loop can iterate over an array by using an index variable to access each element in turn.
15. In a 'do…while' loop, where should the increment/decrement statement typically be placed?
Answer:
Explanation:
The increment or decrement statement in a 'do…while' loop is typically placed at the end of the loop body to ensure that the loop variable is updated after each iteration.