- 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?
Answer: Repetition, because rounds of kicks are repeated based on the condition of a tied score.
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.
- 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?
Answer: Selection, because a decision is made to perform an action based on a specific condition.
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.
- 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?
Answer: Repetition and selection.
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.
- A manufacturing robot's algorithm is to pick up a part, check it for defects, and if it has no defects, place it on a conveyor belt. The robot is instructed to perform this entire process for a batch of 100 parts. Which statement accurately describes the algorithm's structure?
Answer: It is a selection nested within a repetition.
The overall process is repeated 100 times, making repetition the outer structure. Within each repetition, the robot performs a check ("check it for defects") and makes a decision, which is selection. Therefore, the algorithm is best described as a selection nested within a repetition.
- 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?
Answer: Repetition to examine each score in the list, and selection to determine if a particular score is 90 or above.
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.
- Consider an algorithm for withdrawing money from an ATM: 1. Insert card. 2. Enter PIN. 3. If PIN is correct, display transaction options. 4. Select withdrawal amount. 5. If account balance is sufficient, dispense cash. 6. Eject card. The step "If account balance is sufficient, dispense cash" is an example of what?
Answer: Selection, as an action is performed only if a certain condition about the balance is met.
This step describes a decision point. The action of dispensing cash is conditional upon the account balance. This is a clear example of selection. While it is part of a sequence (B) and uses data that could be considered a form of input (D), the core algorithmic concept being illustrated by the "if...then" structure is selection.
- An algorithm is designed to check if a password meets complexity requirements. It must perform three checks: "Does it have at least 8 characters?", "Does it contain a number?", and "Does it contain a special character?". The password is valid only if all three checks pass. Which statement best describes the logical structure required?
Answer: A selection structure that evaluates if all three distinct conditions are met.
The core of this algorithm is making decisions. It must check three conditions and act based on whether all are true. This is a selection structure, likely implemented with if statements and logical AND operators. Choice (A) is incorrect as there is no looping or repetition. Choice (B) is incorrect because decisions are central to the process. Choice (D) describes the wrong logic (an OR condition instead of an AND condition).
- 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?
Answer: Repetition that contains selection.
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.
- Which part of this algorithm best demonstrates the concept of repetition?
Answer: Processing the car by performing a function at each of the three washing stations.
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).
- 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?
Answer: Repetition, because an action is performed continuously based on a condition.
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.
- A school stores grades in an int[] gradeList; the algorithm iterates to compute average and count grades >= threshold. Inputs are gradeList and threshold; outputs are average and countAbove. Example input: [80, 90, 70], threshold 85; output: average 80.0, countAbove 1. Example input: [100, 60], threshold 70; output: average 80.0, countAbove 1.
// Compute average and count grades at or above threshold
int sum = 0;
int countAbove = 0;
for (int i = 0; i < gradeList.length; i++) {
sum += gradeList[i];
if (gradeList[i] >= threshold) {
countAbove++;
}
}
double average = (double) sum / gradeList.length;
System.out.println(average + "," + countAbove);
Consider the algorithm for processing grades; which part ensures every grade is compared to threshold exactly once? Answer: The for-loop header iterates once per index in gradeList
This question tests AP Computer Science A skills: understanding algorithms with selection and repetition, focusing on loop control and iteration mechanics. The for-loop structure ensures systematic traversal through arrays by using an index variable that increments from 0 to the array length minus one. In this problem, the for-loop header 'for (int i = 0; i < gradeList.length; i++)' controls the iteration, visiting each array element exactly once. Choice B is correct because the loop header specifically iterates once per index in gradeList, ensuring every grade is accessed through gradeList[i] and compared to the threshold exactly once. Choice A is incorrect because the cast to double only affects the average calculation, not the iteration count. To help students: Emphasize that for-loop headers control iteration count, not the loop body. Practice tracing through loops with index variables to see how each element is accessed exactly once.
- A weather station stores daily temperatures in an int[] temps; the algorithm finds hottest and coldest days. Inputs are temps; outputs are minTemp and maxTemp. Example input: [72, 68, 75]; output: min 68, max 75. Example input: [50]; output: min 50, max 50.
int minTemp = temps[0];
int maxTemp = temps[0];
for (int i = 1; i < temps.length; i++) {
if (temps[i] < minTemp) minTemp = temps[i];
if (temps[i] > maxTemp) maxTemp = temps[i];
}
System.out.println(minTemp + "," + maxTemp);
Consider the algorithm for weather analysis; which part ensures correctness when temps has exactly one element? Answer: Initializing minTemp and maxTemp to temps[0] handles length 1
This question tests AP Computer Science A skills: understanding algorithms with selection and repetition, focusing on edge case handling and initialization. Proper initialization ensures algorithms work correctly even with minimal input, such as single-element arrays. In this weather analysis algorithm, both minTemp and maxTemp are initialized to temps[0], which handles the edge case where the array has only one element. Choice B is correct because initializing minTemp and maxTemp to temps[0] ensures that when temps has length 1, both variables already contain the only value, and the loop body never executes (since i starts at 1). Choice A is incorrect because starting i at 1 is intentional to avoid redundant comparison of temps[0] with itself. To help students: Teach the importance of initialization values that handle edge cases. Practice tracing algorithms with minimal inputs like single-element or empty arrays.
- 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?
Answer: The algorithm uses selection (Step 1) and repetition (Step 3) within an overall sequence.
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.
- 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? Answer: GREEN printed 5 times, then the state changes after printing
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.
- A guessing game picks a secret integer; the player repeatedly guesses until correct. Inputs are secret and a sequence of guesses; output is the number of attempts and feedback messages. Example input: secret 7, guesses 3,9,7; output: "Too low", "Too high", attempts 3. Example input: secret 2, guesses 2; output: attempts 1.
int attempts = 0;
int guess = -1;
while (guess != secret) {
guess = getNextGuess();
attempts++;
if (guess < secret) {
System.out.println("Too low");
} else if (guess > secret) {
System.out.println("Too high");
}
}
System.out.println("Attempts: " + attempts);
Consider the algorithm for the guessing game; why is a loop necessary in this part of the algorithm? Answer: It allows repeated guesses until the conditional becomes false
This question tests AP Computer Science A skills: understanding algorithms with selection and repetition, specifically while loops for indefinite iteration. While loops continue executing their body as long as the condition remains true, making them ideal for situations where the number of iterations is unknown beforehand. In this guessing game, the while loop continues as long as 'guess != secret', allowing the player to make multiple attempts until they guess correctly. Choice B is correct because the while loop allows repeated guesses until the conditional (guess != secret) becomes false, which happens when the correct guess is made. Choice A is incorrect because loops don't guarantee correctness of guesses, they just allow repetition. To help students: Use flowcharts to visualize how while loops check conditions before each iteration. Practice identifying when to use while loops versus for loops based on whether iteration count is known.
- Which of the following is an example of an algorithm where the order of operations between sequencing and selection is critical to its correctness?
Answer: An algorithm that first calculates a user's total purchase amount, and then applies a discount if the total is over $100.
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.
- How does the order of the algorithmic components potentially cause these algorithms to have different outcomes or efficiencies?
Answer: The algorithms are different sequences of selection steps; one may reject an applicant earlier than the other.
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.
- 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?
Answer: Sequencing followed by selection.
"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.
- 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? Answer: Add an if inside the existing loop to increment countAbove
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.
- In algorithm design, what is the primary function of selection?
Answer: To allow the algorithm to follow different paths of execution based on a true-or-false condition.
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.