All questions
Question 1
A game loop updates a player's score each time a coin is collected. Examine the following code snippet:
coins <- [true, false, true, true] score <- 0 index <- 0 // initialization WHILE (index < LENGTH(coins)) { IF (coins[index] = true) { score <- score + 10 // update game state } index <- index + 1 // advance to next coin event } PRINT(score) // prints final score
What is the output of the following loop?
- 20
- 30 (correct answer)
- 40
- 10
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a WHILE loop used to accumulate points based on boolean conditions in an array. Choice B (30) is correct because it accurately reflects the loop's intended function and outcome, as three true values (at indices 0, 2, 3) each add 10 points, resulting in 30. Choice C (40) is incorrect because it might result from counting all four array elements regardless of their boolean value. To help students: Emphasize understanding conditional logic within loops. Practice tracing through loops that combine iteration with conditional accumulation.
Question 2
A club filters a list of ages to count members who can attend an 18+ event. Examine the following code snippet:
ages <- [16, 18, 21, 17, 19] count <- 0 // accumulator for qualifying ages FOR (index <- 0; index < LENGTH(ages); index <- index + 1) { IF (ages[index] >= 18) { count <- count + 1 // increment when criteria met } } PRINT(count) // prints number of qualifying ages
What is the output of the following loop?
- 2
- 3 (correct answer)
- 4
- 5
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a FOR loop used to count elements in an array that meet a specific criterion (age >= 18). Choice B (3) is correct because it accurately reflects the loop's intended function and outcome, as three ages (18, 21, 19) satisfy the condition ages[index] >= 18. Choice C (4) is incorrect because it might result from misunderstanding the >= operator or counting all non-zero ages. To help students: Emphasize understanding conditional statements within loops. Practice tracing through loops with conditional logic to verify which elements meet the criteria.
Question 3
A student tries to calculate n! with a loop, but the result is incorrect. Examine the following code snippet, where iteration should multiply integers from 1 through n into an accumulator.
n <- 4
product <- 0 // should store running product
FOR i <- 1 TO n
product <- product * i // multiply by i each iteration
END FOR
DISPLAY(product)
The loop runs from 1 to n, updating product each iteration, and then prints the accumulator. The purpose is to compute factorial iteratively, but an error prevents it from functioning as intended.
Examine the following code snippet: Identify the error in the loop structure that prevents it from functioning as intended.
- The accumulator should start at 1, not 0 (correct answer)
- The loop must start with i <- 0, not i <- 1
- The loop never terminates because i is not updated
- DISPLAY(product) must be inside the loop to work
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a FOR loop attempting to calculate factorial, but with an initialization error in the accumulator variable. Choice A is correct because multiplying by 0 always yields 0, so product remains 0 throughout all iterations (0×1=0, 0×2=0, etc.), preventing the factorial calculation from working. Choice C is incorrect because the FOR loop structure inherently updates i each iteration, showing a misunderstanding of how FOR loops automatically handle counter incrementation. To help students: Emphasize proper accumulator initialization - sum accumulators start at 0, product accumulators start at 1. Practice debugging by tracing values through each iteration to identify where calculations go wrong.
Question 4
A student generates a numeric pattern for a display by iterating a fixed number of steps. Examine the following code snippet:
result <- "" FOR (i <- 1; i <= 4; i <- i + 1) // initialization, condition, update { result <- result + (i * 2) + " " // append even number and a space } PRINT(result) // prints the built sequence
What is the output of the following loop?
- 2 4 6 8 (correct answer)
- 0 2 4 6
- 2 4 6
- 2 4 6 8 10
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a FOR loop used to generate a sequence of even numbers by multiplying the loop variable by 2. Choice A (2 4 6 8 ) is correct because the loop runs with i values 1, 2, 3, 4, producing 2, 4, 6, 8 with spaces after each number. Choice C (2 4 6 ) is incorrect because it misinterprets the loop condition i <= 4, thinking it stops at i < 4. To help students: Emphasize understanding loop bounds and string concatenation. Practice building output strings step-by-step to visualize the final result.
Question 5
A student searches a list of usernames to find the first match. Examine the following code snippet:
names <- ["Ava", "Mia", "Noah", "Liam"] target <- "Noah" index <- 0 // initialization foundIndex <- -1 WHILE (index < LENGTH(names)) { IF (names[index] = target) { foundIndex <- index // store where it was found index <- LENGTH(names) // force termination } index <- index + 1 // advance each iteration } PRINT(foundIndex) // prints index or -1
What condition causes the loop to terminate?
- When foundIndex becomes -1
- When index is no longer less than LENGTH(names) (correct answer)
- When target is not in the list
- When index equals foundIndex at any time
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a WHILE loop used to search for a target value in an array, with early termination when found. Choice B is correct because the loop's primary termination condition is when index is no longer less than LENGTH(names), which occurs either naturally or when forced by setting index to LENGTH(names) after finding the target. Choice A is incorrect because foundIndex becoming -1 doesn't control loop termination - it's just a flag value. To help students: Emphasize understanding loop conditions and how they control execution. Practice identifying all ways a loop can terminate, including both natural progression and forced exits.
Question 6
A teacher totals quiz points stored in an array using iteration. Examine the following code snippet:
points <- [4, 6, 3, 7, 5] total <- 0 // accumulator starts at 0 index <- 0 // loop initialization WHILE (index < LENGTH(points)) { total <- total + points[index] // add current element index <- index + 1 // move to next index } PRINT(total) // prints the sum
What is the output of the following loop?
- 25 (correct answer)
- 20
- 24
- 26
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a WHILE loop used to sum all elements in an array by visiting each index sequentially. Choice A (25) is correct because it accurately reflects the loop's intended function and outcome, as demonstrated by adding 4+6+3+7+5 = 25. Choice B (20) is incorrect because it might result from skipping an element or miscalculating the sum. To help students: Emphasize understanding loop initialization, condition, and iteration process. Practice tracing through loops step-by-step with concrete values to verify the accumulator's final value.
Question 7
A developer searches a list of error codes and returns the index of a match. Examine the following code snippet:
codes <- [101, 205, 310, 404] target <- 404 index <- 0 WHILE (index < LENGTH(codes) AND codes[index] != target) { index <- index + 1 // moves forward until match or end } PRINT(index) // prints the index after loop ends
What condition causes the loop to terminate?
- When index equals LENGTH(codes) - 1
- When codes[index] becomes less than target
- When index is not less than LENGTH(codes) or codes[index] equals target (correct answer)
- When target is assigned the value at codes[index]
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a WHILE loop with a compound condition using AND logic to search for a target value. Choice C is correct because the loop terminates when either condition becomes false: when index is not less than LENGTH(codes) OR when codes[index] equals target (making the != comparison false). Choice A is incorrect because it only considers one possible termination scenario. To help students: Emphasize understanding compound boolean conditions with AND/OR logic. Practice evaluating when compound conditions become false to predict loop termination.
Question 8
A program filters survey responses by iterating through a list, but it fails to progress. Examine the following code snippet:
responses <- ["yes", "no", "yes"] countYes <- 0 index <- 0 WHILE (index < LENGTH(responses)) { IF (responses[index] = "yes") { countYes <- countYes + 1 } // missing update to index here } PRINT(countYes)
Identify the error in the loop structure that prevents it from functioning as intended.
- The condition should use
<= instead of < - The accumulator must start at 1, not 0
- The loop is missing
index <- index + 1 (correct answer) - The IF statement must compare with
!=
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a WHILE loop that lacks the crucial index update statement, causing an infinite loop. Choice C is correct because the loop is missing 'index <- index + 1', which prevents the index from advancing and the loop condition from ever becoming false. Choice A is incorrect because using '<=' instead of '<' wouldn't fix the infinite loop problem. To help students: Emphasize the importance of loop variable updates to ensure termination. Practice debugging infinite loops by checking that all loop variables progress toward the termination condition.
Question 9
A student sums daily temperatures to compute a weekly total using iteration. Examine the following code snippet:
temps <- [70, 72, 68, 75] total <- 0 FOR (i <- 0; i < LENGTH(temps); i <- i + 1) { total <- total + temps[i] // accumulator adds each temperature } PRINT(total) // prints total of all entries
Which line of code demonstrates the loop's initialization?
i < LENGTH(temps)i <- i + 1FOR (i <- 0; i < LENGTH(temps); i <- i + 1) (correct answer)total <- total + temps[i]
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a FOR loop used to sum temperature values, highlighting the three parts of a FOR loop structure. Choice C is correct because the entire FOR statement 'FOR (i <- 0; i < LENGTH(temps); i <- i + 1)' demonstrates the loop's initialization, including setting i to 0, establishing the condition, and defining the update expression. Choice A is incorrect because it only shows the condition part, not the initialization. To help students: Emphasize understanding the three components of a FOR loop: initialization, condition, and update. Practice identifying each component in various loop structures.
Question 10
A program computes a factorial by multiplying sequential integers. Examine the following code snippet:
n <- 5 product <- 1 // accumulator for multiplication i <- 1 // initialization WHILE (i <= n) { product <- product * i // multiply by current i i <- i + 1 // update i to approach termination } PRINT(product) // prints n!
How many times does the loop execute?
- 4 times
- 5 times (correct answer)
- 6 times
- It executes indefinitely
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a WHILE loop used to calculate factorial by multiplying consecutive integers from 1 to n. Choice B (5 times) is correct because with n=5, the loop executes when i equals 1, 2, 3, 4, and 5, after which i becomes 6 and the condition i <= 5 becomes false. Choice A (4 times) is incorrect because it misinterprets the loop condition, forgetting that i <= n includes the case when i equals n. To help students: Emphasize understanding loop initialization, condition, and iteration process. Practice debugging to identify off-by-one errors and condition misinterpretations.
Question 11
A simple game simulation updates a player’s score each frame until it reaches a goal. Examine the following code snippet, where a WHILE loop iterates to update game state repeatedly.
score <- 0
goal <- 10
WHILE score < goal
score <- score + 2 // earn points each update
END WHILE
DISPLAY(score)
The loop checks score < goal before every iteration, increases score by 2 each time, and terminates once score is no longer less than goal. The purpose is to simulate repeated updates until a stopping condition is met.
Examine the following code snippet: How many times does the loop execute?
- The loop executes 4 times
- The loop executes 5 times (correct answer)
- The loop executes 6 times
- The loop executes until score equals 12
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a WHILE loop used to simulate game updates, incrementing score by 2 until it reaches or exceeds the goal. Choice B is correct because starting at score=0, the loop adds 2 each iteration (0→2→4→6→8→10), executing exactly 5 times before score=10 fails the condition score<10. Choice A is incorrect because it might come from dividing the goal by the increment (10/2=5) but forgetting to count the actual iterations, or miscounting by starting at 1. To help students: Create tables showing variable values before and after each iteration. Practice determining loop counts for different starting values and increments to build intuition about termination conditions.
Question 12
A music app stores song lengths in seconds and needs the total playlist time. Examine the following code snippet, where a FOR loop iterates through the list and adds each value to totalSeconds so the program can display the overall duration.
totalSeconds <- 0 // accumulator
FOR index <- 0 TO LENGTH(times) - 1
totalSeconds <- totalSeconds + times[index] // add each song length
END FOR
DISPLAY(totalSeconds)
The loop initializes index at 0, checks the condition index up to LENGTH(times) - 1, updates totalSeconds each iteration, and stops after the last valid index. If times is [120, 180, 240], the purpose is to sum all song lengths.
Examine the following code snippet: How many times does the loop execute?
- The loop executes 2 times
- The loop executes 3 times (correct answer)
- The loop executes 4 times
- The loop executes until totalSeconds is 0
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a FOR loop with index-based iteration used to sum song lengths, where the loop runs from index 0 to LENGTH(times)-1. Choice B is correct because with times=[120,180,240], LENGTH(times)=3, so the loop runs for indices 0,1,2, executing exactly 3 times. Choice C is incorrect because it counts the number of elements rather than understanding that array indices start at 0, making the last valid index LENGTH-1. To help students: Draw diagrams showing array indices starting at 0 and practice converting between element count and valid index ranges. Encourage students to trace loops with small concrete examples to verify iteration counts.
Question 13
A teacher wants to total quiz points stored in a list. Examine the following code snippet, which uses iteration to add each score into an accumulator so one final total can be printed.
total <- 0 // accumulator starts at 0
FOR EACH score IN quizScores
total <- total + score // add current score
END FOR
DISPLAY(total) // print final sum
The loop mechanics are: the accumulator total is initialized once before the loop, then the loop visits each element in quizScores exactly once, updating total each iteration, and terminates after the last element is processed. Suppose quizScores is [5, 8, 10, 7] and the purpose is to compute the class’s combined points for these four quizzes.
Examine the following code snippet: What is the output of the following loop?
- The output is 30 (correct answer)
- The output is 25
- The output is 20
- The output is 10
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a FOR EACH loop used to calculate the sum of all quiz scores by visiting each element in the list exactly once. Choice A is correct because it accurately reflects the loop's intended function and outcome, as the accumulator starts at 0 and adds each score (5+8+10+7=30). Choice D is incorrect because it might represent a misunderstanding that only one value is processed, a common mistake when students don't trace through all iterations. To help students: Emphasize tracing through each iteration step-by-step with concrete values. Practice using accumulators and understanding how they maintain state across iterations.
Question 14
A student wants to find where a target word appears in a vocabulary list. Examine the following code snippet, which uses iteration to search the list one item at a time and returns the index when a match is found.
target <- "loop"
foundIndex <- -1 // -1 means not found
index <- 0
WHILE index < LENGTH(words) AND foundIndex = -1
IF words[index] = target
foundIndex <- index // record match
END IF
index <- index + 1 // move to next item
END WHILE
DISPLAY(foundIndex)
This WHILE loop continues as long as the index is in range and the target has not been found; it checks one element per iteration and terminates when either condition fails. If words is ["for", "while", "loop", "if"], the purpose is to locate the first occurrence of target.
Examine the following code snippet: What condition causes the loop to terminate?
- When index equals LENGTH(words) or foundIndex changes (correct answer)
- When target is greater than the current word alphabetically
- When foundIndex stays at -1 for two iterations
- When index becomes negative after incrementing
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a WHILE loop used for linear search, which continues while two conditions are both true: index is within bounds AND the target hasn't been found. Choice A is correct because the loop terminates when either condition becomes false - when index reaches LENGTH(words) (out of bounds) OR when foundIndex changes from -1 (indicating target was found). Choice B is incorrect because it misinterprets the search logic, confusing string comparison with alphabetical ordering, which isn't relevant to this equality-based search. To help students: Emphasize understanding compound conditions with AND/OR operators in loops. Practice tracing searches that find targets early versus those that search the entire list without finding the target.
Question 15
A library app searches a list of book IDs to find a requested ID. Examine the following code snippet, which iterates through the list using a counter and stops early when it finds a match.
requested <- 42
index <- 0
WHILE index < LENGTH(bookIDs) AND bookIDs[index] != requested
index <- index + 1 // check next ID
END WHILE
DISPLAY(index)
The loop checks the condition before each iteration, compares the current element to requested, increments index to move forward, and terminates when it finds the ID or reaches the end. If bookIDs is [10, 42, 55], the purpose is to find the index of requested.
Examine the following code snippet: How many times does the loop execute?
- The loop executes 0 times
- The loop executes 1 time (correct answer)
- The loop executes 2 times
- The loop executes 3 times
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a WHILE loop used for searching, which continues while the current element doesn't match the requested ID and the index is in bounds. Choice B is correct because with bookIDs=[10,42,55] and requested=42, the loop checks index 0 (value 10≠42), increments to index 1, then the condition fails because bookIDs[1]=42=requested, so the loop executes exactly 1 time. Choice D is incorrect because it might assume the loop checks all elements, not understanding that the loop terminates early when a match is found. To help students: Trace through loops that exit early versus those that complete all iterations. Emphasize reading loop conditions carefully to understand when termination occurs.
Question 16
A math program computes n! (factorial) by multiplying sequential integers. Examine the following code snippet, where iteration updates an accumulator product to build the factorial result.
n <- 5
product <- 1 // accumulator for multiplication
FOR i <- 1 TO n
product <- product * i // multiply by next integer
END FOR
DISPLAY(product)
The FOR loop initializes i at 1, repeats through n inclusive, updates product each iteration, and terminates after multiplying by n. The purpose is to compute the factorial without recursion.
Examine the following code snippet: What is the output of the following loop?
- The output is 24
- The output is 60
- The output is 120 (correct answer)
- The output is 125
Explanation: This question tests understanding of iteration and loop mechanics in programming, a key concept in AP Computer Science Principles. Iteration involves executing a set of instructions repeatedly until a condition is met, crucial for tasks like summing, searching, or generating sequences. The provided code snippet demonstrates a FOR loop used to calculate factorial by multiplying consecutive integers from 1 to n. Choice C is correct because it accurately reflects the loop's intended function: starting with product=1, the loop multiplies 1×1×2×3×4×5=120 for n=5. Choice B is incorrect because it might represent confusion with sum operations (1+2+3+4+5=15, then ×4=60), a common mistake when students mix up accumulation patterns. To help students: Practice distinguishing between sum accumulators (start at 0, use +) and product accumulators (start at 1, use ×). Trace through factorial calculations step-by-step to reinforce the multiplication pattern.