C While and Do/While Loop MCQ

1. What is the primary characteristic of a 'while' loop in C?

a) The loop executes at least once
b) The condition is checked at the end of the loop
c) The loop may not execute at all if the condition is false initially
d) The loop executes an indefinite number of times

Answer:

c) The loop may not execute at all if the condition is false initially

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?

a) The 'do…while' loop checks the condition at the beginning
b) The 'do…while' loop executes at least once regardless of the condition
c) The 'do…while' loop cannot use a break statement
d) The 'do…while' loop is faster than the 'while' loop

Answer:

b) The 'do…while' loop executes at least once regardless of the condition

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?

a) while condition { statements; }
b) while (condition) { statements; }
c) while (condition) statements;
d) Both b) and c) are correct

Answer:

d) Both b) and c) are correct

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?

a) do { statements; } while (condition);
b) do (condition) { statements; };
c) do { statements; } until (condition);
d) do while (condition) { statements; };

Answer:

a) do { statements; } while (condition);

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?

a) For loop
b) While loop
c) Do/while loop
d) Both b) and c) are suitable

Answer:

d) Both b) and c) are suitable

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?

a) The loop exits after a certain number of iterations
b) The loop continues indefinitely, creating an infinite loop
c) The loop skips to the next section of code
d) An error is thrown by the compiler

Answer:

b) The loop continues indefinitely, creating an infinite loop

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++;
}
a) 012
b) 123
c) 0123
d) An infinite number of 0s

Answer:

a) 012

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?

a) do { statements; } while condition;
b) do { statements; } while (condition)
c) do (condition) { statements; } while;
d) do { statements; } while (condition);

Answer:

d) do { statements; } while (condition);

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?

a) Using the 'stop' statement
b) Using the 'exit' statement
c) Using the 'break' statement
d) By setting the condition to false

Answer:

c) Using the 'break' statement

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?

a) To exit the loop
b) To skip the current iteration and proceed to the next iteration
c) To pause the loop temporarily
d) To restart the loop from the beginning

Answer:

b) To skip the current iteration and proceed to the next iteration

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?

a) 0 times
b) 1 time
c) 2 times
d) Depends on the condition

Answer:

b) 1 time

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?

a) while (1) { }
b) while (0) { }
c) do { } while (0);
d) for (;;) { }

Answer:

a) while (1) { }

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?

a) The loop executes a fixed number of times
b) The loop executes only once
c) The loop executes indefinitely
d) The program terminates

Answer:

c) The loop executes indefinitely

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?

a) Yes, using an index variable
b) No, 'while' loops cannot be used with arrays
c) Only if the array has a known fixed size
d) Only in combination with a 'for' loop

Answer:

a) Yes, using an index variable

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?

a) Before the loop body
b) At the beginning of the loop body
c) At the end of the loop body
d) After the loop condition

Answer:

c) At the end of the loop body

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top