C Loops Quiz - MCQ Questions and Answers

1. What is the output of the following C code snippet?

int i = 0;
for (i = 0; i < 5; i++) {
printf("%d ", i);
}
a) 0 1 2 3 4 5
b) 0 1 2 3 4
c) 1 2 3 4 5
d) None of the above

Answer:

b) 0 1 2 3 4

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?

a) for
b) while
c) do-while
d) foreach

Answer:

d) foreach

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?

a) Terminates the loop immediately
b) Skips the current iteration and proceeds to the next iteration
c) Pauses the loop temporarily
d) None of the above

Answer:

b) Skips the current iteration and proceeds to the next iteration

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?

a) Before the first iteration
b) After the first iteration
c) At the middle of each iteration
d) At the end of each iteration

Answer:

d) At the end of each iteration

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?

a) for(;;)
b) while(true)
c) do {} while(1);
d) All of the above

Answer:

d) All of the above

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--;
   }
a) 10 9 8 7 6 5
b) 10 9 8 7 6
c) 9 8 7 6 5
d) Infinite loop

Answer:

b) 10 9 8 7 6

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);
  }
a) 5 times
b) 6 times
c) 10 times
d) 11 times

Answer:

b) 6 times

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?

a) It causes the immediate termination of the enclosing loop.
b) It skips to the next iteration of the loop.
c) It is used to exit from a switch statement.
d) Both a) and c)

Answer:

d) Both a) and c)

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++;
 }
a) 1 2
b) 1 2 3
c) 1 2 3 4 5
d) No output

Answer:

a) 1 2

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?

a) while loop
b) do-while loop
c) for loop
d) All are equally good

Answer:

c) for loop

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);
       }
   }
a) (0, 0) (1, 1) (2, 2)
b) (1, 0) (2, 0) (2, 1)
c) (0, 1) (0, 2) (1, 0) (1, 2) (2, 0) (2, 1)
d) No output

Answer:

b) (1, 0) (2, 0) (2, 1)

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?

a) 'while' loop is faster than 'do-while' loop
b) 'do-while' loop executes at least once
c) 'while' loop can be used as an infinite loop
d) 'do-while' loop cannot use conditional expressions

Answer:

b) 'do-while' loop executes at least once

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);
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3
d) Infinite loop

Answer:

a) 0 1 2 3 4

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?

a) It forces the next iteration of the loop to be skipped
b) It immediately exits the loop
c) It skips the rest of the loop body and tests the loop condition again
d) It repeats the current iteration

Answer:

c) It skips the rest of the loop body and tests the loop condition again

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?

a) The entire nested loop is exited
b) Only the inner loop is exited
c) The outer loop is paused
d) The program is terminated

Answer:

b) Only the inner loop is exited

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?

a) By moving the initialization outside the loop and the increment inside the loop body
b) By removing the initialization and increment
c) By writing the condition at the end of the loop
d) 'for' loops cannot be converted to 'while' loops

Answer:

a) By moving the initialization outside the loop and the increment inside the loop body

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?

a) It exits only the innermost loop
b) It exits all loops
c) It restarts the innermost loop
d) It skips the next iteration of the innermost loop

Answer:

a) It exits only the innermost loop

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);
   }
a) 0 1 2 3 4
b) 0 1 3 4
c) 1 2 3 4
d) 0 1 2

Answer:

b) 0 1 3 4

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?

a) Not even once
b) Once
c) Twice
d) Cannot be determined

Answer:

a) Not even once

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);
   }
a) 9 8 7 6 5 4 3 2 1 0
b) 10 9 8 7 6 5 4 3 2 1
c) 9 8 7 6 5 4 3 2 1
d) Infinite loop

Answer:

c) 9 8 7 6 5 4 3 2 1

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?

a) It exits the loop
b) It exits the switch, and the loop continues
c) It restarts the loop
d) It has no effect on the loop

Answer:

b) It exits the switch, and the loop continues

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(;;)?

a) It causes a compile-time error
b) It is a valid infinite loop
c) It will execute once and stop
d) It will not compile

Answer:

b) It is a valid infinite loop

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);
   }
a) 0 1 2 3 4
b) 1 2 3 4
c) 1 2 3 4 5
d) 2 3 4 5

Answer:

b) 1 2 3 4

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?

a) The loop will not execute
b) The loop becomes an infinite loop
c) The loop executes once
d) The loop behaves like a 'while' loop

Answer:

b) The loop becomes an infinite loop

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?

a) Using a 'break' statement directly
b) Using a 'break' statement within a condition
c) Using a 'break' statement with a label
d) It is not possible to terminate a nested loop completely using a 'break' statement

Answer:

c) Using a 'break' statement with a label

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