C++ Loops MCQ

Looping structures are fundamental to any programming language, enabling the repetitive execution of a block of code as long as the specified condition remains true. How well do you know loops in C++? Test your knowledge with this beginner-friendly quiz!

1. Which of the following is not a type of loop in C++?

a) for
b) while
c) do-while
d) repeat-until

Answer:

d) repeat-until

Explanation:

repeat-until is not a type of loop in C++. The standard loops are for, while, and do-while.

2. What will be the output of the following code snippet?

for(int i=0; i<5; i++) {
    cout << i << " ";
}
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3 4 5
d) 1 2 3 4

Answer:

a) 0 1 2 3 4

Explanation:

The loop starts with i=0 and runs until i<5 0="" 4.="" from="" it="" numbers="" p="" print="" so="" to="" will="">

3. How many times will the loop body execute in a do-while loop?

a) Zero or more times
b) One or more times
c) Exactly once
d) It’s unpredictable

Answer:

b) One or more times

Explanation:

The do-while loop ensures that the loop body is executed at least once before the condition is checked.

4. Which loop is best if you want to execute the block of code at least once?

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

Answer:

c) do-while loop

Explanation:

The do-while loop checks the condition after executing the loop body, so the body will always run at least once.

5. What will be the output of the following code?

int i = 5;
while(i > 0) {
    cout << i << " ";
    i--;
}
a) 5 4 3 2 1
b) 1 2 3 4 5
c) 5 4 3
d) No output

Answer:

a) 5 4 3 2 1

Explanation:

The loop starts with i=5 and runs as long as i is greater than 0, decrementing i in each iteration.

6. In a for loop, which part is optional?

a) Initialization
b) Condition
c) Update
d) All of the above

Answer:

d) All of the above

Explanation:

In a for loop, all parts (initialization, condition, and update) are optional.

7. Infinite loops are generally considered:

a) Useful
b) An error
c) Required
d) Desirable

Answer:

b) An error

Explanation:

Infinite loops generally indicate that something has gone wrong in the code, causing the loop to run indefinitely.

8. Which statement is used to exit a loop prematurely?

a) break
b) exit
c) stop
d) return

Answer:

a) break

Explanation:

The break statement is used to exit a loop prematurely.

9. Which statement allows the next iteration of the loop to run prematurely?

a) next
b) continue
c) skip
d) move

Answer:

b) continue

Explanation:

The continue statement skips the rest of the current iteration and moves to the next iteration of the loop.

10. In which loop is the condition checked after executing the loop body?

a) for loop
b) while loop
c) do-while loop
d) None of the above

Answer:

c) do-while loop

Explanation:

In the do-while loop, the condition is checked after executing the loop body.


Leave a Comment

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

Scroll to Top