Big Idea 3: Algorithms and Programming · Topic 3.4 · Coding Practice
Strings — Code It Yourself
Write real code and check it against the expected output. Pick Python or JavaScript; the AP pseudocode reference is there when you need it.
You warmed these up on paper in the String Builder and Build the Badge exercises — joining pieces with concatenation and taking substrings by position. Now type and run them for real, hardest last. Predict each output first, then check it: the first builds a badge from two substrings, and the last is an off-by-one index bug for you to fix.
Problem 1 of 4
Given name = "Computer", print the first 4 characters followed by the last 4 characters, separated by a space.
Expected output: Comp uter
Show AP pseudocode reference
first ← first 4 characters of name
last ← last 4 characters of name
DISPLAY(first + " " + last)
(the exam describes substrings in words — there is no SUBSTRING procedure on the reference sheet)
name[0:4] = 'Comp', name[4:8] = 'uter'. Concatenate them with a space in the middle.
Problem 2 of 4
Build the string AP: 5 from course = "AP" and score = 5.
Expected output: AP: 5
Show AP pseudocode reference
DISPLAY(course + ": " + score)
In Python, str(score) converts the integer to a string before you can join it with +.
Problem 3 of 4
Print the number of vowels (a, e, i, o, u) in "programming".
Expected output: 3
Show AP pseudocode reference
count <- 0
FOR EACH ch IN word
{
IF(ch is a vowel) { count <- count + 1 }
}
DISPLAY(count)
Loop over each character; check whether "aeiou" contains it, and add 1 when it does.
Problem 4 of 4
Spot the error: this should print the 3rd character of "science" (which is i) but prints the wrong one. Fix it.
Expected output: i
Show AP pseudocode reference
DISPLAY(s[3])
(exam lists are 1-indexed, so the 3rd item is index 3)
In Python and JavaScript, string positions start at 0 — so the 3rd character is index 2, not 3.
Your code runs on a secure external service. Answers are checked automatically — nothing is stored.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed.
Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Typically responds within 24 hours
✓
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.