Last Updated: January 3, 2026
30 quizzes
Which statement best describes an if-else statement in C++?
When is a switch statement generally preferred over multiple if-else statements?
What happens if the condition of a while loop is initially false?
In a for loop, when is the increment expression executed?
Which loop guarantees its body executes at least once?
Which range-based for loop correctly modifies all elements of a std::vector<int> v?
What is the main risk of forgetting a break statement in a switch case?
Which for loop header correctly iterates over indices 0 to n-1 for a std::vector<int> v?
Which construct is best for repeatedly asking the user for a menu choice until they choose 'Exit'?
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?
Complete the if statement to check if a std::unique_ptr<int> p is not null before dereferencing.
if () std::cout << *p << std::endl;Click an option to fill the blank:
Complete the for loop header to iterate 10 times using i from 0 to 9.
for (int i = 0; ; ++i) { std::cout << i << std::endl; }Click an option to fill the blank:
Use a range-based for loop to print all values in a std::vector<int> v.
for (const auto& value : ) std::cout << value << std::endl;Click an option to fill the blank:
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}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}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}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}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}Find the bug in this input loop that should stop when the user enters 0.
Click on the line(s) that contain the bug.
#include <iostream> int main() { int value; while (value != 0) { std::cin >> value; std::cout << "You entered: " << value << std::endl; } return 0;}Find the bug related to loop control and memory management.
Click on the line(s) that contain the bug.
#include <iostream> void fillArray(int n) { int* data = new int[n]; int i = 0; while (i <= n) { data[i] = i; ++i; } delete[] data;} int main() { fillArray(5); return 0;}Click the line that causes a potential infinite loop when input fails.
Click on the line to select.
#include <iostream>#include <limits> int main() { int number; std::cout << "Enter a positive number: "; while (number <= 0) { std::cin >> number; if (std::cin.fail()) { std::cout << "Invalid input\n"; } } std::cout << "You entered: " << number << std::endl; return 0;}Click the line where the control-flow decision for exit is made in this menu loop.
Click on the line to select.
#include <iostream> int main() { int choice = 0; do { std::cout << "1. Start\n2. Settings\n3. Exit\n"; std::cin >> choice; switch (choice) { case 1: std::cout << "Starting...\n"; break; case 2: std::cout << "Opening settings...\n"; break; case 3: std::cout << "Exiting...\n"; break; default: std::cout << "Invalid choice\n"; } } while (choice != 3); return 0;}Complete the if-else chain to categorize an HTTP status code.
#include <iostream>int main() { int code = 404; if () { std::cout << "Success"; } else if () { std::cout << "Client error"; } else { std::cout << "Other"; } return 0;}Click an option to fill blank 1:
Complete the for loop that sums elements of a std::vector<int> v.
#include <vector>int main() { std::vector<int> v{1, 2, 3}; int sum = 0; for ( i = 0; ; ++i) { sum += v[i]; } return sum;}Click an option to fill blank 1:
Complete the while loop that reads integers until -1 is entered.
#include <iostream>int main() { int value = 0; std::cin >> value; while () { std::cout << value << std::endl; std::cin >> value; } return 0;}Click an option to fill blank 1:
Complete the range-based for loop to print all characters in a std::string s.
#include <iostream>#include <string>int main() { std::string s = "Hi"; for ( ch : ) { std::cout << ch << std::endl; } return 0;}Click an option to fill blank 1:
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.
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.
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.
Order the steps to implement a robust input-validation loop using while.
Drag and drop to reorder, or use the arrows.