3) FOR EACH item IN list { … } — iterates over each element of a list.
Q: Which of the following best describes the result of running this program? number ← RANDOM(1, 6)
REPEAT UNTIL (number > 100) { number ← number * 2 } IF (number = 128) { DISPLAY(true) } ELSE {
DISPLAY(false) }
✔ If number starts as 1, 2, or 4, then the program will display TRUE.
• Starting at 1: 1→ 2→ 4→ 8→ 16→ 32→ 64→ 128 ✔
• Starting at 2: 2→ 4→ 8→ 16→ 32→ 64→ 128 ✔
• Starting at 4: 4→ 8→ 16→ 32→ 64→ 128 ✔
• Starting at 3: 3→ 6→ 12→ 24→ 48→ 96→ 192 ✗ (192 ≠ 128)
• Starting at 5 or 6: also skip 128.
Q: A program has been developed to compute the sum of all elements with a value > 10 in a list. Which
statement best describes how iteration is used?
✔ The program will iterate (loop) through each element of the list, check if the element is greater than 10, and
add it to a running total if so. Iteration allows this check to be performed on every element without writing
separate code for each one.
Q: Consider two equivalent code segments that display the first 12 positive even integers. Segment A
uses 12 separate DISPLAY statements. Segment B uses a loop. Which of the following is NOT an
advantage of Segment B over Segment A?
✔ Segment B will execute in less time than Segment A.
(Both segments produce the same output in roughly the same execution time — the advantage of Segment B
is readability and maintainability, not speed.)
Q: When is a REPEAT UNTIL loop preferred over a REPEAT n TIMES loop?
✔ When it is unknown how many times the loop will need to iterate — usually when we are waiting for an
event to occur (e.g., 'repeat until the user enters a valid password').
Q: A study of beech trees shows 40% may be susceptible to a disease. A program counts susceptible
trees in a forest of k trees. Which condition replaces <MISSING CONDITION>? total ← 1 ; REPEAT k
TIMES { IF () { total ← total + 1 } } DISPLAY(total) — Select TWO answers.
✔ ✔ RANDOM(1, 10) ≤ 4 (40% probability — 4 out of 10)
✔ RANDOM(1, 100) ≤ 40 (40% probability — 40 out of 100)
Both correctly simulate a 40% chance for each tree.
Section 4: Random Numbers & Simulation
Q: What does RANDOM(a, b) return in AP CSP pseudocode?
✔ A random integer from a to b, inclusive. Each integer in the range has an equal probability of being selected.
Q: How do you simulate rolling a 6-sided die in pseudocode?
✔ result ← RANDOM(1, 6) — returns 1, 2, 3, 4, 5, or 6 with equal probability.
Q: A string 'Value' has 5 characters (indices 1–5). The code below generates a random number: String
word = new String('Value') int random = (int)(Math.random() * word.length() + 1) What is the range of
numbers that could be printed?
✔ 1 to 5. (Math.random() * 5 gives [0.0, 5.0), casting to int gives 0–4, +1 gives 1–5.)
Q: Why are random number generators used in simulations?
✔ To model real-world uncertainty and probability. Random values allow programs to simulate unpredictable
events (weather, disease spread, dice rolls) without requiring actual randomness.
Section 5: Algorithm Design & Tracing