AlgoMaster Logo

Control Flow - Quiz

Last Updated: January 3, 2026

Control Flow Quiz

30 quizzes

1
Multiple Choice

Which statement best describes an if-else statement in C++?

2
Multiple Choice

When is a switch statement generally preferred over multiple if-else statements?

3
Multiple Choice

What happens if the condition of a while loop is initially false?

4
Multiple Choice

In a for loop, when is the increment expression executed?

5
Multiple Choice

Which loop guarantees its body executes at least once?

6
Multiple Choice

Which range-based for loop correctly modifies all elements of a std::vector<int> v?

7
Multiple Choice

What is the main risk of forgetting a break statement in a switch case?

8
Multiple Choice

Which for loop header correctly iterates over indices 0 to n-1 for a std::vector<int> v?

9
Multiple Choice

Which construct is best for repeatedly asking the user for a menu choice until they choose 'Exit'?

10
Multiple Choice

In modern C++, which is usually the safest way to iterate over all elements of a std::vector<std::string> v for read-only access?

11
Code Completion

Complete the if statement to check if a std::unique_ptr<int> p is not null before dereferencing.

cpp
1
if () std::cout << *p << std::endl;

Click an option to fill the blank:

12
Code Completion

Complete the for loop header to iterate 10 times using i from 0 to 9.

cpp
1
for (int i = 0; ; ++i) { std::cout << i << std::endl; }

Click an option to fill the blank:

13
Code Completion

Use a range-based for loop to print all values in a std::vector<int> v.

cpp
1
for (const auto& value : ) std::cout << value << std::endl;

Click an option to fill the blank:

14
Output Prediction

What is the output of this code?

1#include <iostream>
2
3int main() {
4    int x = 3;
5    if (x > 5)
6        std::cout << "A";
7    else if (x > 2)
8        std::cout << "B";
9    else
10        std::cout << "C";
11    return 0;
12}
15
Output Prediction

What does this switch statement print?

1#include <iostream>
2
3int main() {
4    int grade = 85;
5    switch (grade / 10) {
6        case 10:
7        case 9:
8            std::cout << "A";
9            break;
10        case 8:
11            std::cout << "B";
12            break;
13        case 7:
14            std::cout << "C";
15            break;
16        default:
17            std::cout << "D";
18    }
19    return 0;
20}
16
Output Prediction

What is the output of this for loop?

1#include <iostream>
2
3int main() {
4    for (int i = 0; i < 3; ++i) {
5        std::cout << i << " ";
6    }
7    return 0;
8}
17
Output Prediction

What does this while loop print?

1#include <iostream>
2
3int main() {
4    int n = 5;
5    while (n > 0) {
6        std::cout << n << " ";
7        --n;
8    }
9    return 0;
10}
18
Output Prediction

What is the output of this range-based for loop?

1#include <iostream>
2#include <vector>
3
4int main() {
5    std::vector<int> v{1, 2, 3};
6    for (auto& x : v) {
7        x *= 2;
8    }
9    for (const auto& x : v) {
10        std::cout << x << " ";
11    }
12    return 0;
13}
19
Bug Spotting

Find the bug in this input loop that should stop when the user enters 0.

Click on the line(s) that contain the bug.

cpp
1
#include <iostream>
2
 
3
int main() {
4
    int value;
5
    while (value != 0) {
6
        std::cin >> value;
7
        std::cout << "You entered: " << value << std::endl;
8
    }
9
    return 0;
10
}
20
Bug Spotting

Find the bug related to loop control and memory management.

Click on the line(s) that contain the bug.

cpp
1
#include <iostream>
2
 
3
void fillArray(int n) {
4
    int* data = new int[n];
5
    int i = 0;
6
    while (i <= n) {
7
        data[i] = i;
8
        ++i;
9
    }
10
    delete[] data;
11
}
12
 
13
int main() {
14
    fillArray(5);
15
    return 0;
16
}
21
Hotspot Selection

Click the line that causes a potential infinite loop when input fails.

Click on the line to select.

cpp
1
#include <iostream>
2
#include <limits>
3
 
4
int main() {
5
    int number;
6
    std::cout << "Enter a positive number: ";
7
    while (number <= 0) {
8
        std::cin >> number;
9
        if (std::cin.fail()) {
10
            std::cout << "Invalid input\n";
11
        }
12
    }
13
    std::cout << "You entered: " << number << std::endl;
14
    return 0;
15
}
22
Hotspot Selection

Click the line where the control-flow decision for exit is made in this menu loop.

Click on the line to select.

cpp
1
#include <iostream>
2
 
3
int main() {
4
    int choice = 0;
5
    do {
6
        std::cout << "1. Start\n2. Settings\n3. Exit\n";
7
        std::cin >> choice;
8
        switch (choice) {
9
            case 1:
10
                std::cout << "Starting...\n";
11
                break;
12
            case 2:
13
                std::cout << "Opening settings...\n";
14
                break;
15
            case 3:
16
                std::cout << "Exiting...\n";
17
                break;
18
            default:
19
                std::cout << "Invalid choice\n";
20
        }
21
    } while (choice != 3);
22
    return 0;
23
}
23
Fill in the Blanks

Complete the if-else chain to categorize an HTTP status code.

cpp
1
#include <iostream>
2
3
int main() {
4
int code = 404;
5
if () {
6
std::cout << "Success";
7
} else if () {
8
std::cout << "Client error";
9
} else {
10
std::cout << "Other";
11
}
12
return 0;
13
}

Click an option to fill blank 1:

24
Fill in the Blanks

Complete the for loop that sums elements of a std::vector<int> v.

cpp
1
#include <vector>
2
3
int main() {
4
std::vector<int> v{1, 2, 3};
5
int sum = 0;
6
for ( i = 0; ; ++i) {
7
sum += v[i];
8
}
9
return sum;
10
}

Click an option to fill blank 1:

25
Fill in the Blanks

Complete the while loop that reads integers until -1 is entered.

cpp
1
#include <iostream>
2
3
int main() {
4
int value = 0;
5
std::cin >> value;
6
while () {
7
std::cout << value << std::endl;
8
std::cin >> value;
9
}
10
return 0;
11
}

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the range-based for loop to print all characters in a std::string s.

cpp
1
#include <iostream>
2
#include <string>
3
4
int main() {
5
std::string s = "Hi";
6
for ( ch : ) {
7
std::cout << ch << std::endl;
8
}
9
return 0;
10
}

Click an option to fill blank 1:

27
Matching

Match the control-flow construct with its typical use case.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

28
Matching

Match the C++ feature with the most idiomatic loop structure.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

29
Sequencing

Order the steps for processing items from a std::vector<int> using a range-based for loop.

Drag and drop to reorder, or use the arrows.

30
Sequencing

Order the steps to implement a robust input-validation loop using while.

Drag and drop to reorder, or use the arrows.