AP CSP Day 27: Data Cleaning
Share
Practice Question
What is displayed after the following code runs?
words ← ["cat", "elephant", "dog", "butterfly", "ant"]
longWords ← []
FOR EACH word IN words
{
IF LENGTH(word) > 3
{
APPEND(longWords, word)
}
}
DISPLAY(LENGTH(longWords))Checking each word: "cat" has length 3 (not > 3, skip), "elephant" has length 8 (append), "dog" has length 3 (skip), "butterfly" has length 9 (append), "ant" has length 3 (skip). The longWords list contains ["elephant", "butterfly"], with length 2.
B) Only two words exceed length 3, not three. C) Not all five words pass the filter. D) Two words qualify, not just one.
Students include words with length exactly equal to 3, misreading > as ≥. "cat", "dog", and "ant" each have length 3, which does not satisfy LENGTH(word) > 3.
Count each element carefully and check the comparison operator. Write down the length of each string to avoid mental math errors.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help