C++ Loops MCQ Quiz

Test your knowledge of C++ loops with this interactive quiz. Select the correct answer for each question and see immediate feedback.

Medium Level Questions Medium

1

Which loop is guaranteed to execute at least once?

Correct Answer: C) do-while loop

The do-while loop checks its condition at the end of the loop, which guarantees that the loop body will execute at least once regardless of the condition.

2

What is the output of this code?
for(int i=0; i<5; i++) { if(i==2) continue; cout << i << " "; }

Correct Answer: A) 0 1 3 4

The continue statement skips the rest of the loop body when i equals 2, so the number 2 is not printed. The loop runs for i values 0, 1, 3, and 4.

3

Which statement is used to terminate a loop immediately?

Correct Answer: B) break

The break statement is used to terminate a loop immediately and transfer control to the statement following the loop.

4

How many times will this loop execute?
int i = 10; while(i < 10) { cout << i; i++; }

Correct Answer: A) 0 times

The while loop checks the condition before executing the loop body. Since i is initialized to 10 and the condition is i < 10 (which is false), the loop body never executes.

5

What is the output of this code?
for(int i=0; i<3; i++) { for(int j=0; j<2; j++) { cout << i << j << " "; } }

Correct Answer: A) 00 01 10 11 20 21

This is a nested loop. The outer loop runs 3 times (i=0,1,2) and for each iteration of the outer loop, the inner loop runs 2 times (j=0,1). So the output shows all combinations of i and j values.

6

Which loop is best when the number of iterations is known in advance?

Correct Answer: C) for loop

The for loop is specifically designed for situations where the number of iterations is known in advance, as it allows initialization, condition checking, and increment/decrement in a single line.

7

What does the range-based for loop in C++ require for the container it iterates over?

Correct Answer: A) begin() and end() methods

The range-based for loop requires the container to have begin() and end() methods that return iterators, or for the container to be an array.

8

What is the output of this code?
int x = 5; while(x--) { cout << x << " "; }

Correct Answer: B) 4 3 2 1 0

The post-decrement operator returns the original value before decrementing. So when x is 5, the condition evaluates to true (5), then x becomes 4 which is printed. This continues until x becomes 0, the condition evaluates to false (0), and the loop stops.

9

Which of these is a valid way to create an infinite loop?

Correct Answer: A) for(;;)

The for(;;) loop is a common way to create an infinite loop in C++ as it has no initialization, condition, or increment expressions, so it will run forever.

10

What is the purpose of the continue statement?

Correct Answer: B) Skip the current iteration and continue with the next

The continue statement skips the remaining code in the current iteration of a loop and proceeds to the next iteration.

Advanced Level Questions Advanced

11

What will be the output of this code?
int i = 0; for(; i < 5; i++) { if(i == 3) break; } cout << i;

Correct Answer: A) 3

The loop breaks when i equals 3, so the value of i after the loop is 3. Note that the increment (i++) doesn't execute after the break.

12

Which optimization technique can significantly improve performance in tight loops?

Correct Answer: D) All of the above

All these techniques can improve loop performance: Loop unrolling reduces loop overhead, loop inversion changes while to do-while to reduce jumps, and loop fusion combines multiple loops into one to improve cache performance.

13

What is the time complexity of a nested loop where the outer loop runs n times and the inner loop runs log(n) times?

Correct Answer: B) O(n log n)

The total number of iterations is n multiplied by log(n), so the time complexity is O(n log n), which is common in efficient sorting algorithms like merge sort and heap sort.

14

Which C++17 feature allows more efficient range-based for loops with structured binding?

Correct Answer: B) Structured bindings

Structured bindings, introduced in C++17, allow unpacking tuple-like objects directly in range-based for loops, making the code more readable and efficient.

15

What is the potential issue with this loop?
for(unsigned int i = 10; i >= 0; i--) { cout << i << " "; }

Correct Answer: A) Infinite loop

Since i is unsigned, when it reaches 0 and then decrements, it wraps around to the maximum value for unsigned int, causing an infinite loop.

16

Which algorithm from the STL can often replace explicit loops for better readability and performance?

Correct Answer: D) All of the above

All these STL algorithms can replace explicit loops: std::transform for mapping, std::accumulate for reduction, and std::for_each for applying a function to each element, often resulting in cleaner and more optimized code.

17

What C++20 feature introduces a new kind of loop for working with ranges and views?

Correct Answer: A) Ranges

The Ranges library introduced in C++20 provides a new way to work with sequences of elements, including range-based algorithms and views that can be composed, often eliminating the need for explicit loops.

18

In a multithreaded application, which loop construct is generally safest to parallelize?

Correct Answer: A) Loops with no loop-carried dependencies

Loops without loop-carried dependencies (where iterations don't depend on previous iterations) are easiest to parallelize safely, as there's no need for synchronization between iterations.

19

What is the output of this code?
int x = 0; for(int i = 0; i < 5; i++) { for(int j = 0; j < i; j++) { x++; } } cout << x;

Correct Answer: A) 10

The inner loop runs j from 0 to i-1 for each value of i. So for i=1: 1 iteration, i=2: 2 iterations, i=3: 3 iterations, i=4: 4 iterations. Total = 0+1+2+3+4 = 10.

20

Which technique can help avoid branch prediction penalties in tight loops?

Correct Answer: D) All of the above

All these techniques can help with branch prediction: Loop tiling improves cache locality, loop unswitching moves conditionals outside loops, and loop peeling handles initial iterations separately to make remaining iterations more predictable.