Loops form the backbone of many algorithms and routines in the C language. They allow you to run a section of code repeatedly until a certain condition is met. If you’re eager to test your knowledge of C loops, you’re in the right place! Let’s dive into this multiple-choice quiz.
Each question is followed by the correct answer and an explanation to help reinforce your knowledge.
1. Which keyword is used to declare a loop that runs a set number of times?
Answer:
Explanation:
The ‘for’ loop is used to iterate a section of code a specified number of times.
2. How many times will the following loop run?
for(int i=0; i<5; i++) { ... }
Answer:
Explanation:
The loop starts with i=0 and runs until i<5 .="" 5="" hence="" it="" iterate="" p="" times.="" will=""> 5>
3. Which loop checks the condition before executing the code block?
Answer:
Explanation:
The ‘while’ loop checks its condition before executing its code block.
4. What will the following code segment print?
int i = 0;
do {
printf("%d ", i);
i++;
} while (i<3);
Answer:
Explanation:
The loop will print values of i from 0 to 2.
5. Which of the following is used to forcefully exit a loop?
Answer:
Explanation:
The ‘break’ statement is used to exit a loop forcefully.
6. Which statement skips the current iteration and jumps to the next iteration of the loop?
Answer:
Explanation:
The ‘continue’ statement skips the current iteration and moves to the next.
7. How can an infinite loop be created using a ‘for’ loop?
Answer:
Explanation:
Both given options will result in an infinite loop.
8. Which loop is best for iterating over an array when you know its size?
Answer:
Explanation:
The for loop is structured perfectly for iterating a known number of times.
9. Which of the following loops checks the condition before executing the loop body?
Answer:
Explanation:
The while loop checks its condition before executing the loop body. If the condition is false from the start, the body may not run even once.
10. In which loop, the condition is checked at the end of the loop body?
Answer:
Explanation:
In the do-while loop, the condition is checked after the loop body is executed.