AP CSP Unit 5: Iteration, Procedures, and Libraries

31 multiple-choice questions on AP Computer Science Principles Unit 5, Iteration, Procedures, and Libraries, organized by topic. They are not tied to any one class or teacher: they follow the College Board course framework. Learn the 8 key terms with the flashcards first, then test yourself. Every answer comes with a short explanation.

Take the practice test · Flashcards · All study tools

What it covers

  1. 5.1 Iteration
  2. 5.2 Procedures and parameters
  3. 5.3 Libraries, APIs, and random values

Key terms

5.3 Libraries, APIs, and random values

Iteration
Repeating a set of statements, as in REPEAT n TIMES or REPEAT UNTIL
Infinite loop
A loop whose ending condition is never met
Procedure
A named group of instructions that may take parameters and return a value
Parameter
An input variable of a procedure
Procedural abstraction
Using a procedure by what it does, not how
Library
A collection of reusable procedures
API
Application Program Interface: the specification for how to use a library's procedures
RANDOM(a, b)
Returns a random integer from a to b inclusive

Sample practice questions

  1. What is displayed? count ← 0; REPEAT 4 TIMES { count ← count + 3 }; DISPLAY(count)

    1. 12
    2. 7
    3. 3
    4. 4

    Answer: A. 12 The body runs 4 times, adding 3 each time.

  2. A procedure is

    1. a single variable that stores the result of a calculation in a program
    2. an error message displayed when a program stops running unexpectedly
    3. a type of list that can hold values of more than one data type at once
    4. a named group of instructions that may have parameters and return a value

    Answer: D. a named group of instructions that may have parameters and return a value Procedures (functions, methods) let code be reused and abstracted.

  3. A software library is

    1. a room or building where a company's computers are stored and run
    2. a list of all the programmers who have worked on a project
    3. a collection of procedures that can be used in creating new programs
    4. a type of error that occurs when a program uses too much memory

    Answer: C. a collection of procedures that can be used in creating new programs Libraries let programmers reuse tested code.

  4. What is displayed? n ← 1; REPEAT UNTIL (n > 20) { n ← n * 2 }; DISPLAY(n)

    1. 32
    2. 16
    3. 20
    4. 64

    Answer: A. 32 n goes 1, 2, 4, 8, 16, 32; the loop stops when n > 20.

  5. A parameter is

    1. a variable in a procedure definition that gets a value when it's called
    2. the value a procedure sends back to the code that called it
    3. the name used to call a procedure from elsewhere in a program
    4. a loop counter that tracks how many times a procedure has run

    Answer: A. a variable in a procedure definition that gets a value when it's called The values passed in a call are arguments; parameters are the placeholders that receive them.

  6. An Application Program Interface (API) is

    1. the physical hardware that connects a computer to a network
    2. the specification of how a library's procedures behave and can be used
    3. a programming language used to write applications for phones
    4. a network protocol that sends data between web servers

    Answer: B. the specification of how a library's procedures behave and can be used APIs document the procedures available and how to call them.

  7. A REPEAT UNTIL (condition) loop stops

    1. when the condition becomes false
    2. when the condition becomes true
    3. after exactly 10 iterations
    4. never

    Answer: B. when the condition becomes true The loop repeats until the condition evaluates to true. If it is true at the start, the body does not run.

  8. What is displayed? PROCEDURE double(x) { RETURN(x * 2) }; DISPLAY(double(double(3)))

    1. 6
    2. 12
    3. 9
    4. 3

    Answer: B. 12 double(3) = 6, then double(6) = 12.

  9. RANDOM(1, 6) in AP pseudocode returns

    1. a random integer from 1 to 6, inclusive, each equally likely
    2. a random decimal number between 1 and 6, inclusive
    3. a random integer from 1 to 5, excluding the upper bound
    4. either 1 or 6, each with a probability of one half

    Answer: A. a random integer from 1 to 6, inclusive, each equally likely RANDOM(a, b) produces an integer between a and b, including both.

  10. Which loop never ends?

    1. x ← 5; REPEAT UNTIL (x = 0) { x ← x - 1 }
    2. x ← 0; REPEAT UNTIL (x > 3) { x ← x + 1 }
    3. REPEAT 10 TIMES { x ← x + 1 }
    4. x ← 1; REPEAT UNTIL (x = 0) { x ← x + 1 }

    Answer: D. x ← 1; REPEAT UNTIL (x = 0) { x ← x + 1 } x only increases from 1, so it never equals 0, giving an infinite loop.

Practice all 31 questions

More AP Computer Science Principles

Studying for a test in your own class? Make a practice test from your class notes.