Below is a mini-problem set that mirrors the College Board AP CSP reference sheet style (IF / ELSE, REPEAT, PROCEDURE, ← for assignment, etc.). Use it to sharpen tracing, debugging, and algorithm-design skills.
Feel free to solve them first and then check the answer key at the end (collapsed so you’re not tempted 🙂).
Part A – Trace the Code (What’s the Output?)
1. Even Counter
code
counter ← 0
REPEAT 5 TIMES
IF counter MOD 2 = 0
DISPLAY(counter)
END IF
counter ← counter + 1
END REPEAT
What gets displayed (and in what order)?
2. Mysterious Sum
code
sum ← 0
x ← 1
REPEAT UNTIL x > 10
sum ← sum + x
x ← x + 2
END REPEAT
DISPLAY(sum)
3. List Shuffle
code
myList ← [ 2, 4, 6, 8 ]
FOR EACH num IN myList
num ← num + 1
END FOR
DISPLAY(myList)
(Reminder: In AP CSP pseudocode, FOR EACH iterates over copies of list items, not the items themselves.)
4. Nested Mystery
code
value ← 0
FOR i ← 1 TO 3
FOR j ← 1 TO i
value ← value + j
END FOR
END FOR
DISPLAY(value)
Part B – Spot & Fix the Bug
Each snippet has exactly one logic or syntax flaw. Describe it and provide a corrected version.
5. Infinite Loop?
code
count ← 10
REPEAT UNTIL count = 0
DISPLAY(count)
count ← count + 1
END REPEAT
6. Out-of-Bounds Access
code
names ← [ "Ada", "Grace", "Alan" ]
index ← 1
WHILE index ≤ LENGTH(names)
DISPLAY(names[index])
index ← index + 1
END WHILE
Part C – Write-Your-Own Algorithm
7. First Vowel Position
Write a PROCEDURE FirstVowel(word) that returns the index (1-based) of the first vowel ("a","e","i","o","u") in word; if none, return 0.
8. Average Without Lowest
Given a list of integers scores, write pseudocode that removes the lowest score from the list and then displays the average of the remaining scores (assume at least two scores are given).
9. Palindrome Tester (Case-Insensitive)
Create a user-defined PROCEDURE IsPalindrome(str) that returns true if str reads the same forward and backward when case is ignored (e.g., "MadAm"), otherwise false.
Part D – Big Picture / Short FR-Style Prompts
10. Image Filter Mini-Task
You are given a 2-D list pixels where each element is an integer 0-255 representing grayscale brightness.
a) Write pseudocode for Darken(pixels, amount) that subtracts amount from every pixel value but never lets anything go below 0.
b) Explain in 1-2 sentences why this is an example of data abstraction.
Part A Answers
0 2 4
(counter counts 0→4, displaying only evens)
25
Adds 1+3+5+7+9 = 25
[ 2, 4, 6, 8 ] FOR EACH changes the copy, original list unchanged.
Bug: count is incremented instead of decremented ⇒ infinite loop.
code
count ← 10
REPEAT UNTIL count = 0
DISPLAY(count)
count ← count - 1 ◀️
END REPEAT
Bug: List indices are 1…LENGTH; loop currently goes 1 to LENGTH, inclusive, so last iteration tries index 4 (out-of-bounds).
code
names ← [ "Ada", "Grace", "Alan" ]
index ← 1
WHILE index ≤ LENGTH(names)
DISPLAY(names[index])
index ← index + 1
END WHILE
Fix in two common ways:
• Change condition to < (strict) OR
• Start index ← 0 and use 0-based if you adjust throughout.
Part C Sample Solutions
code
PROCEDURE FirstVowel(word)
vowels ← [ "a","e","i","o","u" ]
FOR i ← 1 TO LENGTH(word)
IF LOWER(word[i]) IN vowels
RETURN i
END IF
END FOR
RETURN 0
END PROCEDURE
code
min ← scores[1]
FOR EACH s IN scores
IF s < min
min ← s
END IF
END FOR
REMOVE(scores, INDEX_OF(scores,min)) // removes first occurrence
sum ← 0
FOR EACH s IN scores
sum ← sum + s
END FOR
DISPLAY( sum / LENGTH(scores) )
code
PROCEDURE IsPalindrome(str)
clean ← LOWER(str)
left ← 1
right ← LENGTH(clean)
WHILE left < right
IF clean[left] ≠ clean[right]
RETURN false
END IF
left ← left + 1
right ← right - 1
END WHILE
RETURN true
END PROCEDURE
Part D Sketch
10a.
code
PROCEDURE Darken(pixels, amount)
FOR row ← 1 TO LENGTH(pixels)
FOR col ← 1 TO LENGTH(pixels[row])
newVal ← pixels[row][col] - amount
IF newVal < 0
newVal ← 0
END IF
pixels[row][col] ← newVal
END FOR
END FOR
RETURN pixels
END PROCEDURE
10b. pixels is a higher-level data abstraction for a 2-D image; the procedure manipulates it without caring about how pixels are stored in memory. Users can call Darken without knowing low-level details, demonstrating abstraction of both data and behavior.