1. What is the output of the following C code snippet?
int i = 0;
for (i = 0; i < 5; i++) {
printf("%d ", i);
}
Answer:
Explanation:
The loop runs from i = 0 to i < 5, so it prints numbers from 0 to 4.
2. Which of the following loop types is not supported in C?
Answer:
Explanation:
C supports for, while, and do-while loops, but not foreach, which is found in languages like PHP and JavaScript.
3. What does the 'continue' statement do in a loop in C?
Answer:
Explanation:
'continue' causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
4. In a 'do-while' loop, when is the condition tested?
Answer:
Explanation:
In a 'do-while' loop, the condition is tested after the body of the loop has been executed, so the loop always runs at least once.
5. Which of the following is the correct way to write an infinite loop in C?
Answer:
Explanation:
All of these are valid ways to write an infinite loop in C. The conditions never become false.
6. What will be the output of the following C code?
int a = 10;
while(a > 5) {
printf("%d ", a);
a--;
}
Answer:
Explanation:
The loop starts with a = 10 and decrements a in each iteration. It stops when a is no longer greater than 5.
7. How many times is the loop executed in the following code?
for(int i = 0; i <= 10; i += 2) {
printf("%d ", i);
}
Answer:
Explanation:
The loop starts at 0 and increments by 2 each time until it reaches 10, inclusive. The values of i would be 0, 2, 4, 6, 8, 10.
8. Which of the following statements is true about a 'break' statement in C?
Answer:
Explanation:
The 'break' statement is used to exit from a loop or a switch statement immediately.
9. What will be the output of the following C code?
int i = 1;
while(i <= 5) {
if(i == 3) {
break;
}
printf("%d ", i);
i++;
}
Answer:
Explanation:
The loop breaks when i is equal to 3, so only 1 and 2 are printed.
10. Which looping construct is best when the number of iterations is known beforehand?
Answer:
Explanation:
The for loop is generally used when the number of iterations is known, as it allows initialization, condition, and increment/decrement in one line.
11. What is the output of the following C code snippet?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
break;
}
printf("(%d, %d) ", i, j);
}
}
Answer:
Explanation:
The inner loop breaks when i equals j, so pairs where i and j are different are printed.
12. What is the main difference between 'while' and 'do-while' loops in C?
Answer:
Explanation:
The key difference is that 'do-while' loop will execute its body at least once even if the condition is initially false.
13. What will be the output of the following C code?
int i = 0;
do {
printf("%d ", i);
i++;
} while(i < 5);
Answer:
Explanation:
The loop executes with i starting from 0 and increments in each iteration until it is less than 5.
14. What is the purpose of the 'continue' statement in a loop?
Answer:
Explanation:
The 'continue' statement causes the loop to skip the remainder of its body and immediately retest its condition for the next iteration.
15. In a nested loop, if a 'break' statement is executed in the inner loop, what happens?
Answer:
Explanation:
A 'break' statement in an inner loop causes exit only from that specific loop, not from the outer loop.
16. How can a 'for' loop be converted into an equivalent 'while' loop?
Answer:
Explanation:
In converting a 'for' loop to a 'while' loop, the initialization is done outside the loop, and the increment/decrement is done at the end of the loop body.
17. What is the result of using a 'break' statement in a nested loop?
Answer:
Explanation:
A 'break' statement inside a nested loop will only exit the innermost loop in which it is placed.
18. What will be the output of the following C code?
int i;
for (i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
printf("%d ", i);
}
Answer:
Explanation:
The 'continue' statement skips the current iteration when i is 2. Therefore, 2 is not printed.
19. In a 'while' loop, if the condition is false at the first iteration, how many times will the loop body be executed?
Answer:
Explanation:
If the condition in a 'while' loop is false initially, the loop body will not execute even once.
20. What is the output of the following C code?
int i = 10;
while(i --> 0) {
printf("%d ", i);
}
Answer:
Explanation:
The 'i --> 0' is a clever way of writing 'i-- > 0'. The loop prints i after it has been decremented, starting from 9 to 1.
21. How does the 'break' statement in a switch inside a loop affect the loop?
Answer:
Explanation:
A 'break' statement in a switch statement will exit the switch but not affect the enclosing loop.
22. Which of the following is true about an empty 'for' loop, for(;;)?
Answer:
Explanation:
An empty 'for' loop like for(;;) is a common way to write an infinite loop in C.
23. What will be the output of the following C code snippet?
int i = 0;
while(++i < 5) {
printf("%d ", i);
}
Answer:
Explanation:
The prefix increment operator (++i) increments i and then evaluates the condition. So, the values from 1 to 4 are printed.
24. What happens if the increment statement in a 'for' loop is omitted?
Answer:
Explanation:
Omitting the increment statement in a 'for' loop can lead to an infinite loop if the loop condition remains true indefinitely.
25. How can a nested loop be terminated completely using a 'break' statement?
Answer:
Explanation:
In C, you can use a 'break' statement with a label to exit a nested loop completely. This is also known as a 'goto' label.
Comments
Post a Comment
Leave Comment