Skip to main content

AP Computer Science Principles Unit 3: Algorithms and Programming

Ready When You Are

Select a course and unit, then start practicing.

This page is set up for AP Computer Science Principles Algorithms and Programming. Click Practice, answer it, and read the explanation before moving on.

Algorithms and Programming covers core AP Computer Science Principles concepts tested on the AP exam. On the AP Computer Science Principles exam, items from Algorithms and Programming often ask you to apply course concepts to a scenario you have not memorized verbatim rather than recall isolated terms.

Review your class notes for Algorithms and Programming and list the ideas your teacher repeated most—those recurring themes are usually what AP stems disguise in new contexts.

Algorithms and Programming is a core thread in AP Computer Science Principles. Even without a single posted weight, you will see these ideas recur on MCQs and free-response tasks that blend multiple units.

If two answers feel half-right, Algorithms and Programming questions usually reward the option tied to scale, evidence, or mechanism—not the more dramatic storyline.

Try explaining this unit's core idea to someone else in under 60 seconds. If the explanation drifts, you found a gap worth fixing before test day.

Pair Algorithms and Programming MCQs with one sketch or outline per session. AP Computer Science Principles rewards multiple representations; translating a stem into a diagram or timeline catches gaps MCQs alone can hide.

Click Practice above for fresh MCQs tied to Algorithms and Programming. Answer, read the explanation, and log one sentence about why the correct choice works—that habit compounds faster than grinding endless worksheets.

Sample questions

A few exam-style multiple-choice questions from this unit, with answers and explanations.

  1. Question 1

    A game awards 33 points for a round with a score of at least 88 and 11 point otherwise. The following procedures are intended to calculate the total points for a list of round scores.

    PROCEDURE pointsForRound(score)
      IF score >= 8
        RETURN 3
      ELSE
        RETURN 1
      END IF
    END PROCEDURE
    
    PROCEDURE totalPoints(scores)
      total <- 0
      FOR EACH score IN scores
        pointsForRound(score)
      RETURN total
    END PROCEDURE

    For the list [6, 9, 8, 2], which replacement for the line pointsForRound(score) correctly makes totalPoints return the total points earned?

    • A

      total <- pointsForRound(score)

    • B

      total <- total + pointsForRound(score)

      Correct answer
    • C

      total <- total + score

    • D

      DISPLAY pointsForRound(score)

    Explanation

    B is correct because pointsForRound(score) returns the points earned in one round, and adding that returned value to total accumulates the award for each round. The list earns 1+3+3+1=81 + 3 + 3 + 1 = 8 points.

    A replaces the running total on each iteration, so it keeps only the award from the last round.

    C adds each raw score rather than the points returned by the helper procedure.

    D displays each round's award but does not update total, so the procedure still returns 00.

  2. Question 2

    A procedure checks whether a list contains any duplicate values by comparing every pair of values. Assume the list contains no duplicates, so the procedure makes all possible comparisons before returning false.

    PROCEDURE hasDuplicate(values)
    {
      n ← LENGTH(values)
      FOR i ← 1 TO n
      {
        FOR j ← i + 1 TO n
        {
          IF (values[i] = values[j])
          {
            RETURN(true)
          }
        }
      }
      RETURN(false)
    }

    If the list length increases from 5050 to 100100, approximately how does the number of value comparisons change?

    • A

      It becomes about 44 times as large.

      Correct answer
    • B

      It stays about the same.

    • C

      It becomes about 22 times as large.

    • D

      It becomes about 100100 times as large.

    Explanation

    With no duplicates, the procedure compares each pair exactly once. For a list of length nn, the number of comparisons is 1+2+⋯+(n−1)=n(n−1)21 + 2 + \cdots + (n-1) = \frac{n(n-1)}{2}. Doubling nn therefore makes the comparison count approximately 44 times as large.

    A: Correct. The number of comparisons grows approximately with the square of the list length.

    C: Incorrect. This would describe work that grows approximately in direct proportion to the list length.

    D: Incorrect. Doubling the input does not multiply the number of comparisons by 100100; the nested loops produce approximately quadratic growth.

    B: Incorrect. The loops perform many more comparisons when the list is longer.

  3. Question 3

    A student is building a school-event flyer generator in AP CSP pseudocode. The student writes one procedure that creates a flyer using a title, date, and room number.

    PROCEDURE MakeFlyer(title, date, room)
    {
        DISPLAY(title)
        DISPLAY(date)
        DISPLAY(room)
    }

    The student plans to use the procedure for clubs, tutoring sessions, and dance rehearsals. Which statement best explains why using parameters in the procedure is helpful?

    • A

      It removes the need to check whether the title, date, and room values are correct.

    • B

      It guarantees that every flyer will be displayed in the same order on every screen.

    • C

      It allows the same procedure to be used for different flyers without rewriting the algorithm each time.

      Correct answer
    • D

      It automatically stores all flyer information in a list for later use.

    Explanation

    The correct answer is C because parameters make the procedure general enough to work with different input values. The procedure can create many different flyers by passing in different titles, dates, and room numbers, which reduces duplicated code and makes the algorithm easier to reuse.

    B is incorrect because parameters do not guarantee screen order; they only let the procedure accept different values.

    D is incorrect because the procedure displays values, but it does not automatically create a list or store the data for later use.

    A is incorrect because parameters do not eliminate the need to verify that the inputs are correct; the procedure still depends on valid values.

  4. Question 4

    A procedure must rotate a list one position to the right, moving the last value to the first position and shifting every other value one position right. The list contains at least two values, uses 1-based indexing, and assignments take effect immediately. Which procedure correctly performs the rotation while preserving every original value?

    • A
      PROCEDURE rotateRight(items)
        n <- LENGTH(items)
        saved <- items[n]
        i <- n - 1
        REPEAT n - 1 TIMES
          items[i] <- items[i + 1]
          i <- i - 1
        items[1] <- saved
      END PROCEDURE
    • B
      PROCEDURE rotateRight(items)
        n <- LENGTH(items)
        saved <- items[n]
        i <- n
        REPEAT n - 1 TIMES
          items[i] <- items[i - 1]
          i <- i - 1
        items[1] <- saved
      END PROCEDURE
      Correct answer
    • C
      PROCEDURE rotateRight(items)
        n <- LENGTH(items)
        saved <- items[1]
        i <- 1
        REPEAT n - 1 TIMES
          items[i] <- items[i + 1]
          i <- i + 1
        items[n] <- saved
      END PROCEDURE
    • D
      PROCEDURE rotateRight(items)
        n <- LENGTH(items)
        saved <- items[n]
        i <- 2
        REPEAT n - 1 TIMES
          items[i] <- items[i - 1]
          i <- i + 1
        items[1] <- saved
      END PROCEDURE

    Explanation

    Option B is correct. It saves the last value before changing the list, then copies each earlier value one position to the right, starting at the end. Copying backward prevents a value from being overwritten before it is copied. Finally, the saved last value is placed at the first position.

    Option C performs a left rotation: it saves the first value, shifts later values left, and places the saved value at the end.

    Option D copies values from left to right. Each assignment overwrites a value that the next assignment needs, so the original values are not preserved.

    Option A copies values in the wrong direction, from each position to the position before it. This overwrites values and does not shift the original values right as required.

  5. Question 5

    A student team updates a food-ordering app by adding a feature that saves favorite meals. After the update, they rerun tests for placing an order, saving an order, and canceling an order that had already passed before the change. What development practice are they using, and why is it useful?

    • A

      Regression testing, because it helps determine whether new changes broke existing features

      Correct answer
    • B

      Version tracking, because it automatically restores earlier versions of the app after each update

    • C

      User-centered testing, because it collects opinions about whether the new feature is appealing

    • D

      Boundary testing, because it checks whether the app works only at its smallest and largest inputs

    Explanation

    Regression testing means rerunning previously successful tests after a change to make sure existing functionality still works. In this scenario, the team added a new feature and then retested older features to check for unintended side effects.

    A is correct because regression testing helps catch bugs introduced by updates.

    D is incorrect because boundary testing focuses on edge values, not on retesting old features after a change.

    C is incorrect because user-centered testing is about gathering feedback from users, not verifying that prior functions still work.

    B is incorrect because version tracking records changes over time; it does not automatically test or restore the app.

Frequently asked questions