- Analyze the following code. How many times will the loop execute?
// Scenario: processing an array until a target is found
int[] values = {3, 6, 9, 12, 15};
int i = 0; // initialize index
while (i < values.length && values[i] != 12) { // stop when 12 is found
i++; // update index to iterate
} Answer: 3 times
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop searches through an array {3, 6, 9, 12, 15} starting at index 0, continuing while i < values.length AND values[i] != 12. Choice A is correct because the loop executes exactly 3 times: when i=0 (values[0]=3), i=1 (values[1]=6), and i=2 (values[2]=9), then stops when i=3 because values[3]=12. Choice C is incorrect because it counts all array elements, not recognizing that the loop terminates early when finding 12. To help students: Create trace tables showing i, values[i], and condition evaluation at each step. Emphasize how compound conditions with && require both parts to be true for continuation.
- Analyze the following code. What condition causes the loop to terminate?
// Scenario: error-checking loop over simulated attempts
int[] attempts = {3, 0, 6, 5};
int i = 0; // initialize
int value = attempts[i];
while ((value < 1 || value > 5) && i < attempts.length - 1) {
i++; // update
value = attempts[i]; // refresh attempt
}
System.out.println(value); Answer: When value is within 1..5 or i reaches attempts.length - 1
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop validates values from array {3, 0, 6, 5}, continuing while the value is invalid (outside 1-5) AND i hasn't reached the last element. Choice B is correct because the loop terminates when EITHER condition becomes false: when value falls within the valid range 1-5 (like attempts[0]=3) OR when i reaches attempts.length-1, preventing array bounds errors. Choice D is incorrect because it ignores the compound nature of the condition with &&, which allows early termination when finding a valid value. To help students: Practice evaluating compound conditions with both && and || operators. Trace through examples showing how each part of the condition affects loop continuation.
- Analyze the following code. Identify the error in the loop logic.
// Scenario: validating input until it is within 1..5
int[] attempts = {0, 7, 4};
int i = 0; // initialize
int rating = attempts[i]; // current attempt
while (rating < 1 || rating > 5) { // repeat until valid
System.out.println("Invalid: " + rating);
i++; // update index
// BUG: rating is never refreshed from attempts[i]
} Answer: The variable rating must be updated inside the loop.
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop attempts to validate input by checking if rating is outside the range 1-5, but rating is initialized to attempts[0]=0 and never updated inside the loop despite incrementing i. Choice B is correct because the critical bug is that rating must be refreshed with attempts[i] inside the loop after incrementing i; otherwise, it always checks the same initial value, causing an infinite loop. Choice A is incorrect because the || operator is correct for checking if a value is outside a range (less than 1 OR greater than 5). To help students: Emphasize the importance of updating all relevant variables in loops. Use debugging techniques like printing variable values to identify when variables aren't changing as expected.
- Analyze the following code.
// Scenario: search an array until a target is found
int[] data = {2, 4, 6, 8, 10};
int index = 0; // initialization
while (index < data.length && data[index] != 8) { // stop when found
index++; // update
}
System.out.println(index);What will be the output of the given code? Answer: Prints 3
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop searches through the array for the value 8, using a compound condition that checks both array bounds and whether the target has been found. Choice B is correct because the loop increments index from 0 to 3, where data[3] equals 8, causing the condition to become false and the loop to terminate with index = 3. Choice C is incorrect because it reflects counting the number of iterations rather than the final index value. To help students: Practice linear search algorithms and trace the index value at each iteration. Emphasize understanding when compound conditions with && become false - when either part is false.
- Analyze the following code.
// Scenario: find the first value greater than 10
int[] values = {5, 9, 10, 12, 3};
int index = 0; // initialization
while (index < values.length && values[index] <= 10) { // condition
index++; // update
}
System.out.println(values[index]);What will be the output of the given code? Answer: Prints 12
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop searches for the first value greater than 10, using a compound condition to ensure array bounds are respected while checking values. Choice B is correct because the loop increments index past values 5, 9, and 10 (all <= 10), stopping at index = 3 where values[3] = 12, which is greater than 10. Choice A is incorrect because it reflects stopping at 10, misunderstanding the <= condition. To help students: Practice compound conditions with short-circuit evaluation. Trace through showing why 10 doesn't satisfy > 10 and the loop continues.
- Analyze the following code.
// Scenario: input-driven loop totals values until sentinel -1
int[] stream = {2, 4, 6, -1};
int i = 0; // initialization
int total = 0;
while (stream[i] != -1) { // stop at sentinel
total += stream[i]; // process sequence
i++; // update
}
System.out.println(total);Which variable is affected by the loop iteration? Answer: Both i and total are modified
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop accumulates values from the stream array until encountering the sentinel value -1, modifying both the accumulator total and the index i. Choice C is correct because within the loop body, total is modified by the += operation and i is modified by the ++ operation, making both variables change during iteration. Choice B is incorrect because it overlooks that i must be incremented to progress through the array. To help students: Identify all variables that change within the loop body. Practice distinguishing between loop control variables and computation variables.
- Analyze the following code.
// Scenario: counting odd indices in an array with a while loop
int[] nums = {4, 7, 1, 9, 2};
int index = 1; // initialization
while (index < nums.length) { // condition
System.out.println(nums[index]); // iterate through the sequence
index += 2; // update
}How many times will the loop execute? Answer: 2 times
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop starts with index = 1 and increments by 2 each iteration (index += 2), accessing odd indices of the array until index >= nums.length. Choice A is correct because the loop executes exactly 2 times: first when index = 1 (printing 7), then when index = 3 (printing 9), and terminates when index becomes 5 which equals nums.length. Choice C is incorrect because it reflects confusion about how many elements are accessed versus how many times the loop executes. To help students: Encourage manually tracing the index values (1, 3, 5) and checking the condition at each step. Practice distinguishing between loop iterations and array accesses, especially with non-standard increments.
- Analyze the following code.
// Counting loop with an incorrect update
int counter = 1; // initialization
while (counter <= 10) { // condition
System.out.println(counter);
counter--; // update
}Identify the error in the loop logic. Answer: The update should increment, not decrement
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop has a logical error where counter is decremented (counter--) instead of incremented, creating an infinite loop since counter starts at 1 and keeps getting smaller while remaining <= 10. Choice B is correct because the update should increment to eventually make the condition false, not decrement which moves counter further from the termination condition. Choice A is incorrect because initializing to 0 wouldn't fix the fundamental problem of decrementing instead of incrementing. To help students: Emphasize the importance of ensuring loop variables move toward termination conditions. Practice identifying infinite loops by tracing a few iterations and checking if progress is made toward loop exit.
- Analyze the following code.
// Conditional loop: stop when target is found
int[] data = {2, 4, 6, 8, 10}; // sequence
int i = 0; // initialization
int target = 8;
while (i < data.length && data[i] != target) { // condition
i++; // update
}
System.out.println(i);What will be the output of the given code? Answer: 3
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop searches through an array for a target value (8), using a compound condition to ensure both array bounds are respected and the target hasn't been found. Choice B is correct because the loop increments i for values at indices 0 (2), 1 (4), and 2 (6), then stops when it finds 8 at index 3, printing i = 3. Choice C is incorrect because it assumes the loop continues past finding the target, misunderstanding how the != condition works. To help students: Practice tracing loops with compound conditions, evaluating each part separately. Use debugging techniques to understand short-circuit evaluation in && conditions.
- Analyze the following code.
// Scenario: process an array until a target is found
int[] a = {1, 3, 5, 7, 9};
int index = 0; // initialization
while (index < a.length && a[index] != 7) { // condition
System.out.println(a[index]); // iterate through sequence
index++; // update
}What will be the output of the given code? Answer: Prints 1, 3, 5 each on a new line
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop prints array elements until finding the target value 7, using a compound condition that ensures both array bounds safety and target checking. Choice A is correct because the loop prints a[0] = 1, a[1] = 3, and a[2] = 5, then stops when index = 3 because a[3] = 7, making the condition false before printing 7. Choice B is incorrect because it assumes 7 gets printed, not recognizing that the loop terminates before printing when the target is found. To help students: Trace execution carefully noting when printing occurs relative to condition checking. Emphasize that the loop body doesn't execute when the condition is false.
- Analyze the following code.
// Scenario: counting loop with an incorrect update
int count = 1; // initialization
while (count <= 10) { // condition
System.out.println(count);
count--; // update (intended to increment)
}Identify the error in the loop logic. Answer: The update decrements, preventing termination
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop intends to count from 1 to 10 but contains a logic error where count is decremented (count--) instead of incremented. Choice A is correct because the decrement operation causes count to become 0, -1, -2, etc., always satisfying count <= 10, creating an infinite loop that never terminates. Choice B is incorrect because changing the condition wouldn't fix the fundamental issue of decrementing when incrementing is needed. To help students: Always verify that loop updates move toward the termination condition. Practice identifying infinite loops by checking if the update brings the variable closer to making the condition false.
- Analyze the following code.
// Conditional loop: search for a value and report if found
int[] values = {3, 6, 9}; // sequence
int i = 0; // initialization
int target = 5;
while (i < values.length && values[i] != target) { // condition
i++; // update
}
System.out.println(i == values.length);What will be the output of the given code? Answer: true
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop searches for a target value (5) in an array, and after the loop, checks if the search was unsuccessful by comparing i to the array length. Choice A is correct because the loop searches through all three elements (3, 6, 9) without finding 5, so i becomes 3 (equal to values.length), making the expression i == values.length evaluate to true. Choice B is incorrect because it assumes the target was found, misunderstanding that when a linear search fails, the index equals the array length. To help students: Practice post-loop analysis to understand what loop variable values indicate about search success or failure. Emphasize the pattern of using index == array.length to detect unsuccessful searches.
- Analyze the following code.
// Input-driven sentinel loop over a predefined sequence
int[] inputs = {1, 2, 3, -1}; // simulated user inputs
int i = 0; // initialization
while (i < inputs.length && inputs[i] != -1) { // condition
System.out.println(inputs[i]);
i++; // update
}How many times will the loop execute? Answer: 3 times
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop processes array elements with a compound condition that checks both array bounds and a sentinel value (-1), ensuring safe array access while looking for a termination signal. Choice B is correct because the loop executes for i = 0 (prints 1), i = 1 (prints 2), and i = 2 (prints 3), then stops when it encounters -1 at index 3, executing exactly 3 times. Choice C is incorrect because it counts the sentinel value iteration, not recognizing that the loop terminates before processing -1. To help students: Practice with compound conditions and understand short-circuit evaluation. Create trace tables showing both parts of the condition and when each causes loop termination.
- Analyze the following code.
// Error-checking loop: keep advancing until a valid score is found
int[] attempts = {-3, 12, 7}; // simulated user entries
int index = 0; // initialization
int score = attempts[index]; // current entry
while (score < 0 || score > 10) { // validate range 0..10
index++; // update
score = attempts[index]; // next entry in sequence
}
System.out.println(score);What will be the output of the given code? Answer: 7
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop validates input values, continuing while the score is outside the valid range of 0 to 10, simulating an error-checking mechanism. Choice C is correct because the loop skips -3 (invalid), skips 12 (invalid), then finds 7 (valid) and exits, printing 7. Choice A is incorrect because it assumes the loop stops at the first out-of-range value rather than continuing to search for a valid one. To help students: Practice with validation loops and compound OR conditions. Trace through each iteration showing why values are rejected or accepted based on the range criteria.
- Analyze the following code.
// Counting loop with a strict bound
int counter = 1; // initialization
while (counter < 10) { // condition
System.out.println(counter);
counter++; // update
}How many times will the loop execute? Answer: 9 times
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop initializes counter to 1 and iterates while counter < 10, printing values and incrementing counter until the condition becomes false. Choice B is correct because the loop executes for counter values 1, 2, 3, 4, 5, 6, 7, 8, and 9 (exactly 9 times), stopping when counter reaches 10 since 10 < 10 is false. Choice C is incorrect because it reflects confusion between < and <= operators, which is a common off-by-one error. To help students: Create iteration tables showing the counter value before and after each loop execution. Emphasize the importance of testing boundary conditions and understanding when loops terminate.
- Analyze the following code. What will be the output of the given code?
// Scenario: counting from 1 to 10 using a while loop
int counter = 1; // initialize
while (counter <= 10) { // loop condition controls termination
System.out.println(counter); // print current value
counter++; // update to progress through the sequence
} Answer: It prints 1 through 10, each on a new line.
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop initializes counter to 1 and iterates while counter <= 10, printing the current value and incrementing counter each time. Choice C is correct because the loop executes exactly 10 times: when counter is 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10, printing each value on a new line before incrementing. Choice A is incorrect because it suggests the loop stops at 9, missing that the condition allows counter to equal 10. To help students: Encourage tracing loop iterations step-by-step with a table showing counter values and outputs. Practice identifying loop boundaries and understanding <= versus < conditions.
- Analyze the following code. What condition causes the loop to terminate?
// Scenario: input-driven loop with a sentinel value
int[] inputs = {5, 2, 7, -1, 9}; // simulated user entries
int idx = 0; // initialize
while (idx < inputs.length && inputs[idx] != -1) {
System.out.println(inputs[idx]); // process current entry
idx++; // update
} Answer: When inputs[idx] equals -1 or idx reaches inputs.length
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop processes an array {5, 2, 7, -1, 9} with a compound condition: idx < inputs.length && inputs[idx] != -1, which continues while both conditions are true. Choice B is correct because the loop terminates when EITHER condition becomes false: when inputs[idx] equals -1 (the sentinel value) OR when idx reaches inputs.length (array boundary). Choice A is incorrect because it only considers one termination condition and misunderstands that idx itself never becomes -1. To help students: Draw flowcharts showing how compound conditions work with && operators. Practice identifying all possible termination scenarios in loops with multiple conditions.
- Analyze the following code. How many times will the loop execute?
// Scenario: counting loop prints values from 1 up to 10
int count = 1; // initialize
while (count < 10) { // condition
System.out.println(count);
count++; // update
} Answer: 9 times
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop starts with count=1 and continues while count < 10, printing count and incrementing it each iteration. Choice B is correct because the loop executes exactly 9 times: when count equals 1, 2, 3, 4, 5, 6, 7, 8, and 9, stopping when count becomes 10 since 10 is not less than 10. Choice C is incorrect because it miscounts by assuming the loop includes count=10, but the condition count < 10 excludes this value. To help students: Create tables showing loop variable values before and after each iteration. Emphasize the difference between < and <= conditions and their impact on iteration count.
- Analyze the following code.
// Counting loop that accumulates a running total
int counter = 1; // initialization
int sum = 0;
while (counter <= 4) { // condition
sum += counter; // update sum each iteration
counter++; // update counter
}
System.out.println(sum);Which variable is affected by the loop iteration? Answer: Both counter and sum change each iteration
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop maintains two variables: counter for loop control and sum for accumulation, demonstrating a common pattern where multiple variables are updated within a single loop. Choice C is correct because both counter (incremented from 1 to 5) and sum (accumulating 1+2+3+4 = 10) change during each iteration of the loop. Choice A is incorrect because it overlooks that sum is also modified with the += operation in each iteration. To help students: Use trace tables with columns for all variables to visualize how each changes. Practice identifying accumulator patterns and distinguishing between loop control variables and computation variables.
- Analyze the following code. What will be the output of the given code?
// Scenario: conditional loop finds a value, then reports index
int[] data = {4, 7, 7, 9};
int pos = 0; // initialize
while (pos < data.length && data[pos] != 7) {
pos++; // update
}
System.out.println(pos); Answer: 1
This question tests AP Computer Science A skills, specifically understanding and predicting the behavior of while loops in Java. A while loop repeatedly executes a block of code as long as its condition evaluates to true, allowing for iteration through a sequence until a specific state is reached. In this code snippet, the while loop searches array {4, 7, 7, 9} for the first occurrence of 7, starting at pos=0 and incrementing pos while the current element isn't 7. Choice B is correct because the loop executes once (pos=0, data[0]=4≠7, so pos becomes 1), then stops because data[1]=7, and the final print statement outputs 1. Choice C is incorrect because it might assume the loop finds the second 7, but the loop terminates upon finding the first occurrence. To help students: Use step-by-step tracing to show how loops terminate immediately when conditions become false. Practice with linear search patterns to understand index values after loop termination.