Home

Tutoring

Subjects

Live Classes

Study Coach

Essay Review

On-Demand Courses

Colleges

Games


Log in

Opening subject page...

Loading your content

← Back to quizzes

AP Computer Science a Quiz

AP Computer Science a Quiz: Algorithms With Selection And Repetition

Practice Algorithms With Selection And Repetition in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.

Question 1 / 20

0 of 20 answered

A mail-sorting machine processes a stack of letters. For each letter, it reads the ZIP code. If the ZIP code is for the local area, the letter is placed in Bin A. Otherwise, it is placed in Bin B. This process continues until the stack is empty. This algorithm combines which two fundamental concepts?

Select an answer to continue

What this quiz covers

This quiz focuses on Algorithms With Selection And Repetition, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.

How to use this quiz

Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.

All questions

Question 1

A mail-sorting machine processes a stack of letters. For each letter, it reads the ZIP code. If the ZIP code is for the local area, the letter is placed in Bin A. Otherwise, it is placed in Bin B. This process continues until the stack is empty. This algorithm combines which two fundamental concepts?

  1. Sequencing and selection.
  2. Repetition and selection. (correct answer)
  3. Repetition and sequencing only.
  4. Selection and abstraction.

Explanation: The process is performed "for each letter" until the stack is empty, which is repetition. Inside that repetition, a decision is made based on the ZIP code ("If the ZIP code is..."), which is selection. Therefore, the algorithm's main structure combines repetition and selection.

Question 2

Which of the following real-world scenarios is best modeled using only sequencing, without selection or repetition?

  1. Repeatedly entering a password until it is accepted by a system.
  2. Following the fixed, step-by-step instructions to assemble a piece of furniture. (correct answer)
  3. Choosing between taking a bus or a train based on which one arrives first.
  4. Checking every apple in a basket to find all the ones that are bruised.

Explanation: Assembling furniture from a manual typically involves a fixed number of steps that must be performed in a specific order with no decisions or loops. This is a pure sequence. Choice (A) is repetition. Choice (C) is selection. Choice (D) involves repetition (checking every apple) and selection (is it bruised?).

Question 3

A process for grading an essay is described as: "Read the entire essay from beginning to end. If the essay contains any spelling errors, deduct 5 points from the total score." Which combination of algorithmic concepts is represented?

  1. Sequencing followed by selection. (correct answer)
  2. Repetition followed by sequencing.
  3. Selection followed by repetition.
  4. Sequencing followed by repetition.

Explanation: "Read the entire essay" is the first step in a sequence. "If the essay contains any spelling errors, deduct 5 points" is a single decision based on a condition, which is selection. The process does not describe repeating any action in a loop structure. Therefore, it is a sequence followed by a selection.

Question 4

An algorithm is represented by a diagram starting with a rectangular box labeled "Get user input". An arrow points to a diamond-shaped box labeled "Is input valid?". The "No" arrow from the diamond points back to the "Get user input" box. The "Yes" arrow points to a box labeled "Process data". This diagram illustrates which fundamental algorithmic structure?

  1. A sequence of three independent steps.
  2. A selection statement that chooses one of two final actions.
  3. A repetition loop that continues until valid input is received. (correct answer)
  4. Two separate sequences determined by an initial choice.

Explanation: The diagram shows a loop. If the input is not valid, the process returns to getting input again. This is repetition. The diamond shape represents the decision that controls the loop. Therefore, the structure is a repetition loop designed to validate user input.

Question 5

An algorithm for a thermostat is described as follows: "Repeatedly check the room temperature. As long as the temperature is below 68 degrees, the heat should remain on." Which algorithmic building block is primarily used in this description?

  1. Repetition, because an action is performed continuously based on a condition. (correct answer)
  2. Selection, because a single decision is made about whether to turn the heat on.
  3. Sequencing, because checking the temperature must happen before running the heat.
  4. Data Abstraction, because the specific temperature is represented as a single value.

Explanation: The process involves "repeatedly" checking and performing an action "as long as" a condition is true. This is the definition of repetition, or a loop. Choice (B) is incorrect because the check is continuous, not a one-time decision. While sequencing (C) is a component of the process, the overarching structure that defines the algorithm is repetition. Data abstraction (D) is a valid concept but does not describe the algorithmic structure.

Question 6

A traffic light cycles through GREEN, YELLOW, RED for a fixed number of cycles. Inputs are cycles and starting state; output is the printed sequence of states. Example input: cycles 1, start GREEN; output: GREEN, YELLOW, RED. Example input: cycles 2, start RED; output: RED, GREEN, YELLOW, RED, GREEN, YELLOW.

String state = startState;
for (int c = 0; c < cycles; c++) {
    for (int t = 0; t < 3; t++) {
        System.out.println(state);
        if (state.equals("GREEN")) state = "YELLOW";
        else if (state.equals("YELLOW")) state = "RED";
        else state = "GREEN";
    }
}

Consider the algorithm for the traffic light; what is the time complexity in terms of cycles ccc?

  1. O(1)O(1)O(1) because there are only three states
  2. O(c)O(c)O(c) because the inner loop is constant size (correct answer)
  3. O(c2)O(c^2)O(c2) because two loops always imply quadratic time
  4. O(3c)O(3^c)O(3c) because the state changes exponentially

Explanation: This question tests AP Computer Science A skills: understanding algorithms with selection and repetition, specifically analyzing time complexity of nested loops. Time complexity measures how execution time grows with input size, and nested loops often multiply their iteration counts. In this traffic light algorithm, the outer loop runs 'cycles' times, and the inner loop always runs exactly 3 times (once for each light state). Choice B is correct because the inner loop has constant size (3 iterations), making the total iterations 3c, which simplifies to O(c) linear time complexity. Choice C is incorrect because two loops don't always imply quadratic time - it depends on whether both loops scale with input size. To help students: Emphasize that constant-size inner loops don't change the order of growth. Practice calculating iterations by multiplying loop counts and identifying which variables affect runtime.

Question 7

A traffic light simulation prints states for a total duration in seconds, changing every 5 seconds. Inputs are totalSeconds and startState; output is the printed state at each second. Example input: total 6, start GREEN; output: GREEN printed 5 times, then YELLOW once. Example input: total 5, start RED; output: RED printed 5 times.

String state = startState;
for (int s = 1; s <= totalSeconds; s++) {
    System.out.println(state);
    if (s % 5 == 0) {
        if (state.equals("GREEN")) state = "YELLOW";
        else if (state.equals("YELLOW")) state = "RED";
        else state = "GREEN";
    }
}

Consider the algorithm for the traffic light; what is the output after totalSeconds = 5, startState = GREEN?

  1. GREEN printed 4 times, then YELLOW once
  2. GREEN printed 5 times, then the state changes after printing (correct answer)
  3. GREEN printed once, then YELLOW printed 4 times
  4. YELLOW printed 5 times because s % 5 triggers immediately

Explanation: This question tests AP Computer Science A skills: understanding algorithms with selection and repetition, focusing on modulo operations for periodic events. The modulo operator (%) helps detect when counters reach specific intervals, useful for implementing timed state changes. In this traffic light simulation, the condition 's % 5 == 0' triggers state changes every 5 seconds, but the state change occurs after printing the current state. Choice B is correct because GREEN is printed 5 times (when s = 1, 2, 3, 4, 5), then the state changes to YELLOW after the 5th print when s % 5 == 0 evaluates to true. Choice A is incorrect because it miscounts - GREEN is printed 5 times, not 4. To help students: Emphasize that loop body execution order matters - printing happens before the state change check. Practice tracing loops with modulo conditions to understand periodic behavior.

Question 8

A school processes grades to compute average, but must ignore any negative entries as invalid. Inputs are gradeList; outputs are average of valid grades and validCount. Example input: [90, -1, 80]; output: average 85.0, validCount 2. Example input: [-5, -2]; output: validCount 0.

int sum = 0;
int validCount = 0;
for (int i = 0; i < gradeList.length; i++) {
    if (gradeList[i] >= 0) {
        sum += gradeList[i];
        validCount++;
    }
}
double average = (validCount == 0) ? 0.0 : (double) sum / validCount;
System.out.println(average + "," + validCount);

Consider the algorithm for grade processing; how would you modify the algorithm to also count grades above a threshold efficiently?

  1. Add a second full loop after averaging to count above-threshold
  2. Add an if inside the existing loop to increment countAbove (correct answer)
  3. Replace the loop with a single if to check one grade only
  4. Move sum and validCount inside the loop to reset each iteration

Explanation: This question tests AP Computer Science A skills: understanding algorithms with selection and repetition, focusing on efficient algorithm modification. Efficient algorithms minimize redundant operations by combining related tasks within existing loops rather than creating additional passes through data. To count grades above a threshold while already iterating through the array, the most efficient approach is to add the counting logic within the existing loop. Choice B is correct because adding an if statement inside the existing loop to increment countAbove allows both tasks (summing valid grades and counting above-threshold grades) to be accomplished in a single pass through the data. Choice A is incorrect because adding a second full loop would double the time complexity unnecessarily. To help students: Emphasize the efficiency gained by combining related operations in a single loop. Practice identifying opportunities to merge similar tasks that process the same data.

Question 9

A chef is following a recipe to bake a cake. One step in the recipe reads, "If the batter is too thick, add one tablespoon of milk." Which fundamental algorithmic concept does this step best illustrate?

  1. Sequencing, because it is one step in a specific order of instructions.
  2. Selection, because a decision is made to perform an action based on a specific condition. (correct answer)
  3. Repetition, because the instruction might need to be followed multiple times.
  4. Abstraction, because the details of how to add the milk are not specified.

Explanation: The step involves making a choice (to add milk or not) based on a condition (the batter's thickness). This is the definition of selection. While the step is part of a larger sequence (A), the core concept illustrated within the step is selection. The instruction is a one-time check, not a loop, so it is not repetition (C). Abstraction (D) is a broader concept; the most precise answer is selection.

Question 10

Consider an algorithm for finding a specific name in a printed, alphabetized phone book. The process is as follows: 1. Open the book to the middle. 2. If the desired name is on that page, stop. 3. If the name comes alphabetically before the names on the page, repeat the process with the first half of the book. 4. Otherwise, repeat the process with the second half of the book. This entire process is an example of which combination of algorithmic concepts?

  1. Sequencing followed by a single selection.
  2. Repetition that contains selection. (correct answer)
  3. Selection that contains repetition.
  4. Sequencing with no selection or repetition.

Explanation: The overall process is repeated ("repeat the process..."), making it a form of repetition. Within each repetition, a choice is made (go to the first half, second half, or stop) based on comparing the names, which is selection. Therefore, the algorithm is best described as a repetition structure that contains selection logic to guide the next step.

Question 11

Consider the following description of an algorithm for an automated car wash:

  1. The car enters the wash bay.
  2. The system checks if the customer selected the "undercarriage spray" option.
  3. If the option was selected, the undercarriage spray is activated.
  4. The car moves forward on the conveyor belt.
  5. For each of the three washing stations (soap, rinse, wax), the car pauses and the station's function is performed.

Which part of this algorithm best demonstrates the concept of repetition?

  1. The car entering the wash bay and moving forward on the conveyor belt.
  2. The system checking for the "undercarriage spray" option and activating it if selected.
  3. Processing the car by performing a function at each of the three washing stations. (correct answer)
  4. The entire car wash process from start to finish for a single car.

Explanation: Step 5 describes performing a similar action (pausing and applying a treatment) for a set of items (the three stations). This is a form of repetition, equivalent to a fixed loop that executes three times. Step 2 (and 3) describes selection (B). The other actions are part of the overall sequence (A, D).

Question 12

An algorithm is designed to process a list of exam scores. It is intended to count how many scores are 90 or above. Which of the following describes the necessary combination of algorithmic building blocks for this task?

  1. Repetition to examine each score in the list, and selection to determine if a particular score is 90 or above. (correct answer)
  2. Only selection is needed, to check each score and decide if it is 90 or above without requiring a loop.
  3. Only repetition is needed, to iterate through the list of scores from beginning to end without making decisions.
  4. Only sequencing is needed, to process the scores one after another in the order they appear in the list.

Explanation: To solve the problem, the algorithm must look at every score, which requires repetition (a loop). For each score, it must make a decision: is this score 90 or greater? This requires selection (an if-statement). Both are essential. The other options are incomplete because they omit one of the necessary components.

Question 13

An algorithm for managing an alarm clock is described: "1. If it is a weekday, set the alarm for 6:30 AM. 2. Then, wait until the alarm time is reached. 3. When the alarm sounds, repeatedly play the alarm sound until the snooze button is pressed." Which statement accurately identifies the algorithmic concepts used?

  1. The algorithm uses selection (Step 1) and repetition (Step 3) within an overall sequence. (correct answer)
  2. The algorithm uses repetition (Step 1) and selection (Step 3) within an overall sequence.
  3. The algorithm consists only of a sequence of steps without any selection or repetition.
  4. The algorithm consists only of repetition to determine when the alarm should be played.

Explanation: Step 1 ("If it is a weekday...") is a decision based on a condition, which is selection. Step 3 ("...repeatedly play the alarm sound until...") is an action that is repeated based on a condition, which is repetition. These steps occur in a specific order, which is sequencing. Therefore, the algorithm combines all three concepts.

Question 14

In algorithm design, what is the primary function of selection?

  1. To define a series of steps that are always executed in the same specific order.
  2. To allow the algorithm to follow different paths of execution based on a true-or-false condition. (correct answer)
  3. To repeat a specific set of instructions a certain number of times or until a condition is met.
  4. To group a complex set of operations under a single, simplified name for reuse.

Explanation: Selection is the process of making a decision. It allows an algorithm to choose between two or more different sets of instructions (paths) based on whether a given condition is true or false. Choice (A) describes sequencing. Choice (C) describes repetition. Choice (D) describes abstraction.

Question 15

An algorithm for a simple game is as follows: A player starts with 10 points. The player repeatedly rolls a die. If the die shows a 6, the player gains a point. If the die shows a 1, the player loses a point. The game ends when the player runs out of points. Which of the following building blocks are all present in this algorithm?

  1. Sequencing and selection only.
  2. Sequencing and repetition only.
  3. Repetition and selection only.
  4. Sequencing, selection, and repetition. (correct answer)

Explanation: The algorithm has a clear order of operations (sequencing). It involves repeatedly rolling a die until a condition is met (repetition). Within each roll, it makes a decision based on the number rolled (selection). Therefore, all three fundamental building blocks are present.

Question 16

Consider two algorithms for approving a loan application.

  • Algorithm X: First, check the applicant's credit score. Then, if the score is high enough, check the applicant's income.
  • Algorithm Y: First, check the applicant's income. Then, if the income is high enough, check the applicant's credit score.

How does the order of the algorithmic components potentially cause these algorithms to have different outcomes or efficiencies?

  1. The order is irrelevant because both algorithms use the same two selection steps.
  2. Algorithm X uses repetition then selection, while Algorithm Y uses selection then repetition.
  3. The algorithms are different sequences of selection steps; one may reject an applicant earlier than the other. (correct answer)
  4. Only one algorithm correctly uses sequencing, while the other is logically flawed.

Explanation: Both algorithms use two selection steps within a sequence. However, the order of these steps is different. An applicant could be rejected by the first check in Algorithm X (low credit score) but pass the first check in Algorithm Y (high income). This change in sequence can affect when an application is rejected, impacting efficiency. Both algorithms are logically valid, just structured differently.

Question 17

What is the defining characteristic of repetition in an algorithm?

  1. A set of instructions is executed one or more times based on a controlling condition. (correct answer)
  2. The algorithm makes a single, irreversible choice between two or more paths of execution.
  3. Instructions are always executed in a fixed, linear order from the first to the last.
  4. The physical order of instructions in the code must exactly match their execution order.

Explanation: Repetition (also known as iteration or looping) is defined by the execution of a block of code multiple times. This repetition is controlled by a condition that determines when the loop should continue or terminate. Choice (B) describes selection. Choices (C) and (D) describe sequencing.

Question 18

An algorithm for making a custom pizza is described:

  1. Start with a pizza base.
  2. Check if the customer wants tomato sauce. If yes, add it.
  3. Check if the customer wants pesto sauce. If yes, add it.
  4. Repeat the following for every topping the customer selected: add the topping to the pizza.
  5. Bake the pizza.

Which statement best describes the use of selection and repetition in this algorithm?

  1. Steps 2 and 3 are selection, and Step 4 is repetition. (correct answer)
  2. Steps 2, 3 and 4 are all examples of selection.
  3. Steps 2, 3 and 4 are all examples of repetition.
  4. Step 2 is selection, and Steps 3 and 4 are repetition.

Explanation: Steps 2 and 3 involve making a decision to perform a single action based on a condition ("if the customer wants..."), which is selection. Step 4 involves performing an action for a list of items ("for every topping..."), which is repetition.

Question 19

A student is writing an algorithm to simulate a soccer penalty shootout. The shootout continues with additional rounds as long as the score is tied after the initial five rounds. Which algorithmic concept is best represented by the rule for additional rounds?

  1. Selection, because the algorithm ultimately selects a winner based on the final score.
  2. Repetition, because rounds of kicks are repeated based on the condition of a tied score. (correct answer)
  3. Sequencing, because the additional rounds of kicks must happen in a specific order.
  4. Abstraction, because the details of each kick are hidden within the concept of a "round".

Explanation: The phrase "continues with additional rounds as long as" indicates a loop or repetition. The shootout process is repeated until the condition (a tied score) is no longer true. This is a clear example of repetition controlling the flow of the algorithm.

Question 20

Which of the following is an example of an algorithm where the order of operations between sequencing and selection is critical to its correctness?

  1. An algorithm that first calculates a user's total purchase amount, and then applies a discount if the total is over $100. (correct answer)
  2. An algorithm that counts the number of vowels in a word by checking each letter one by one from start to finish.
  3. An algorithm that displays a list of all files in a directory after sorting them into alphabetical order.
  4. An algorithm that adds a list of numbers together to find their sum, processing them in their given order.

Explanation: The selection (applying the discount) is dependent on the result of the initial sequence of steps (calculating the total). If the selection were performed before the total was calculated, the condition would be based on incomplete information, leading to an incorrect result. The order matters critically. In the other examples, the sequence is fundamental but does not have the same critical interplay with a subsequent selection.