AP CSP Day 27: Data Cleaning

Big Idea 3: Algorithms & Programming
Cycle 1 • Day 27 Practice • Medium Difficulty
Focus: List Traversal & Filtering

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))
Why This Answer?

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.

Why Not the Others?

B) Only two words exceed length 3, not three. C) Not all five words pass the filter. D) Two words qualify, not just one.

Common Mistake
Watch Out!

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.

AP Exam Tip

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
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.