AlgoMaster Logo

Control Flow - Quiz

Last Updated: January 3, 2026

Control Flow Exercises

30 quizzes

1
Code Completion

Choose the correct method to check if an age allows entry when using an if statement condition.

java
1
boolean canEnter = age.(18);

Click an option to fill the blank:

2
Code Completion

Complete the switch statement to handle all unmatched values with a default branch.

java
1
: System.out.println("Unknown option");

Click an option to fill the blank:

3
Code Completion

Select the correct keyword to immediately exit a for loop when a match is found.

java
1
if (id == targetId) ;

Click an option to fill the blank:

4
Multiple Choice

Which control flow construct is most appropriate for choosing one of many constant integer options such as menu selections 1-5?

5
Multiple Choice

You need to validate user age and apply different discounts for age <18, 18-64, and 65+. What structure best fits this?

6
Multiple Choice

Which loop is best when you must execute a block at least once, then repeat while a condition stays true?

7
Multiple Choice

You must process each element of an int[] without modifying the array or using indices. What is the most concise option?

8
Multiple Choice

In a while loop that reads user input until "quit" is entered, what is the main risk if you forget to update the input variable inside the loop?

9
Multiple Choice

In a nested for loop over rows and columns, which statement lets you skip only the rest of the current inner loop iteration when a cell is empty?

10
Multiple Choice

What happens if you omit break in a non-last case of a switch statement?

11
Multiple Choice

You want to exit both inner and outer loops when a value is found in a matrix. Which feature helps you target the outer loop directly?

12
Multiple Choice

Which loop construct automatically stops once it has visited each element in a List<String>?

13
Multiple Choice

Where can a label be applied in Java to use with break or continue?

14
Sequencing

Order the steps to read commands in a loop until the user types "exit" using a while loop.

Drag and drop to reorder, or use the arrows.

15
Sequencing

Order the steps to search a 2D array and stop both loops when a target is found using a labeled break.

Drag and drop to reorder, or use the arrows.

16
Output Prediction

What is the output of this conditional discount logic?

1int price = 100;
2int age = 70;
3if (age >= 65) {
4    price -= 20;
5} else if (age >= 18) {
6    price -= 10;
7} else {
8    price -= 5;
9}
10System.out.println(price);
17
Output Prediction

What does this switch statement print for the given day code?

1int dayCode = 6;
2String type;
3switch (dayCode) {
4    case 1:
5    case 7:
6        type = "Weekend";
7        break;
8    case 2:
9    case 3:
10    case 4:
11    case 5:
12    case 6:
13        type = "Weekday";
14        break;
15    default:
16        type = "Invalid";
17}
18System.out.println(type);
18
Output Prediction

What is printed after this for loop with continue?

1int sum = 0;
2for (int i = 1; i <= 5; i++) {
3    if (i % 2 == 0) {
4        continue;
5    }
6    sum += i;
7}
8System.out.println(sum);
19
Output Prediction

Predict the output of this do-while loop that validates a counter.

1int count = 3;
2int printed = 0;
3do {
4    printed++;
5    count--;
6} while (count > 3);
7System.out.println(printed);
20
Output Prediction

What is the single line of output of the enhanced for loop with break?

1int[] values = {2, 4, 6, 8};
2int found = -1;
3for (int v : values) {
4    if (v > 5) {
5        found = v;
6        break;
7    }
8}
9System.out.println(found);
21
Bug Spotting

Find the bug in this while loop that reads until "stop" is entered.

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

java
1
Scanner scanner = new Scanner(System.in);
2
String cmd = scanner.nextLine();
3
while (!cmd.equalsIgnoreCase("stop")) {
4
    System.out.println("Command: " + cmd);
5
}
6
scanner.close();
22
Bug Spotting

Identify the bug in this enhanced for loop that tries to skip negative scores.

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

java
1
int[] scores = {10, -1, 20};
2
for (int s : scores) {
3
    if (s < 0)
4
        break;
5
    System.out.println("Valid: " + s);
6
}
23
Matching

Match each control flow construct with its best description.

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

24
Matching

Match each keyword with how it affects loop execution.

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

25
Fill in the Blanks

Complete the code to iterate over an array of tasks and stop the loop when a "DONE" marker is found.

java
1
String[] tasks = {"START", "LOAD", "DONE", "SAVE"};
2
for ( task : tasks) {
3
if (.equals("DONE")) {
4
;
5
}
6
System.out.println(task);
7
}

Click an option to fill blank 1:

26
Fill in the Blanks

Fill in the blanks to repeatedly prompt for a PIN using a do-while loop until the correct value is entered.

java
1
int correctPin = 1234;
2
int entered;
3
do {
4
entered = .nextInt();
5
} while ( != correctPin);

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the while loop that sums positive numbers in an array using an index.

java
1
int[] nums = {3, -2, 5};
2
int i = 0;
3
int total = 0;
4
while (i nums.length) {
5
if (nums[i] < 0) {
6
i++;
7
;
8
}
9
total += nums[i];
10
i++;
11
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the switch statement that categorizes a status code, using a default branch for unknown values.

java
1
int status = 404;
2
String message;
3
switch (status) {
4
case 200:
5
message = "OK";
6
break;
7
case 500:
8
message = "Error";
9
break;
10
:
11
message = ;
12
}
13
System.out.println(message);

Click an option to fill blank 1:

29
Hotspot Selection

Click the line that causes only the inner loop to exit when a zero is found.

Click on the line to select.

java
1
int[][] grid = {{1, 2}, {3, 0}};
2
outer: for (int i = 0; i < grid.length; i++) {
3
    for (int j = 0; j < grid[i].length; j++) {
4
        if (grid[i][j] == 0) {
5
            break;
6
        }
7
    }
8
}
9
System.out.println("Done");
30
Hotspot Selection

Click the line where a labeled continue sends control to the next iteration of the outer loop.

Click on the line to select.

java
1
outer: for (int i = 0; i < 3; i++) {
2
    for (int j = 0; j < 3; j++) {
3
        if (j > i) {
4
            continue outer;
5
        }
6
    }
7
}
8
System.out.println("Finished");