Skip to content
Marlbridge

AP® Computer Science Principles · Unit 3 · Topic 3.14

Libraries: Practice Questions (Computer Science Principles 3.14)

Nine original Marlbridge practice questions on libraries, APIs, documentation and using a documented procedure correctly, with explanations and a suggested rubric. Uses an original fictional library.

These are original Marlbridge practice questions, not past exam questions. All scenarios are fictional. The code is Marlbridge’s rendering of the exam reference notation (← assignment, = equality test, lists start at index 1, DISPLAY shows a value). Mark points are a suggested Marlbridge rubric, not an official scoring guideline. No calculator is needed or allowed. Try each question before opening its answer.

Question 1 (multiple choice · foundation)

What is a software library?

  • (A) The area of memory where a program is stored.
  • (B) A program that can only be run once.
  • (C) A list of all the variables in a program.
  • (D) A collection of procedures that can be used to create new programs.
Answer and explanation

Answer: (D). A library supplies reusable, usually tested, procedures.

  • (A), (B) and (C) are unrelated.

Question 2 (multiple choice · foundation)

What is an application program interface (API)?

  • (A) A copy of a program’s source code.
  • (B) A specification of how the procedures in a library behave and can be used.
  • (C) The window that a program shows to the user.
  • (D) A programming language.
Answer and explanation

Answer: (B). An API describes the procedures, their parameters and their results.

  • (A) An API need not reveal source code.
  • (C) describes a user interface.
  • (D) A language is not an API.

Question 3 (multiple choice · core)

Why is documentation for a library necessary?

  • (A) It lets the programmer change the library code.
  • (B) It explains the behaviours the library provides and how to use them.
  • (C) It removes the need to test the program.
  • (D) It makes the library run faster.
Answer and explanation

Answer: (B). You normally cannot see the library code, so documentation is the source of truth about its behaviour.

  • (A), (C) and (D) are not effects of documentation.

Question 4 (multiple choice · core)

NumberKit documentation: clamp(value, low, high) returns low if value < low, high if value > high, otherwise value. What is displayed?

x ← 12
DISPLAY(clamp(x, 0, 5))
  • (A) 0
  • (B) 5
  • (C) 12
  • (D) 7
Answer and explanation

Answer: (B). clamp(12, 0, 5): 12 > 5, so it returns high (5). clamp returns a new value; x is unchanged but the call is displayed.

  • (A) is low.
  • (C) is the unchanged value.
  • (D) subtracts the limit (12 − 5) instead of applying the rule.

Question 5 (multiple choice · core)

average(aList) requires a non-empty list. Which code is the safest way to display the average of scores?

  • (A) IF(LENGTH(scores) > 0) { DISPLAY(average(scores)) }
  • (B) DISPLAY(average([]))
  • (C) DISPLAY(average(scores))
  • (D) DISPLAY(average(scores)) DISPLAY(average([]))
Answer and explanation

Answer: (A). The IF protects the call from an empty list, matching the documented condition.

  • (B) and (D) call the procedure with an empty list.
  • (C) breaks the condition when scores is empty.

Question 6 (multiple choice · core)

A student needs to draw a line chart of 200 values. Which approach is most sensible?

  • (A) Write all the drawing code from scratch.
  • (B) Use a text-formatting library.
  • (C) Look for a graphing library and read its documentation.
  • (D) Avoid drawing anything.
Answer and explanation

Answer: (C). Graphing libraries exist precisely to make complex tasks like charts simpler and more reliable.

  • (A) is slower and riskier.
  • (B) does not suit the task.
  • (D) does not solve the problem.

Question 7 (multiple select, choose two · core)

Which two are benefits of using a library of existing procedures?

  • (A) It can reduce development time.
  • (B) It can reduce the amount of testing of the new code.
  • (C) It makes the program independent of any computer.
  • (D) It means the programmer does not need to read any documentation.
  • (E) It guarantees that the new program has no errors.
Answer and explanation

Answers: (A) and (B). Ready-made, tested procedures save development time and testing.

  • (C) Programs still run on computers.
  • (D) Documentation is needed.
  • (E) Errors in your own code can still occur.

Question 8 (constructed response · core)

Using only the NumberKit documentation in this guide, state what each line displays: DISPLAY(largest([3, 9, 4])), DISPLAY(repeatText("ha", 3)), DISPLAY(clamp(average([2, 4, 6]) * 3, 0, 10)). Show your working for the third.

Answer and explanation

Line 1: largest([3, 9, 4]) returns the largest value, 9.

Line 2: repeatText("ha", 3) joins “ha” to itself 3 times: hahaha.

Line 3: average([2, 4, 6]) is (2 + 4 + 6) / 3 = 4. Then 4 × 3 = 12. clamp(12, 0, 10): 12 > high (10), so it returns 10.

Displayed: 9 hahaha 10.

Suggested Marlbridge rubric (4 marks)

Mark point Marks
Line 1: 9 1
Line 2: hahaha 1
Line 3: average evaluated as 4 first 1
Line 3: argument 12 then clamp returns high (10) 1

Question 9 (constructed response · core)

A game keeps health between 0 and 100. A hit reduces it by damage. (a) Write one statement using clamp that updates health after a hit. (b) The player has 30 health and takes 45 damage: what is health afterwards? (c) The player has 20 health and picks up a heal of 90: write the statement and state the result. (d) Explain one advantage of using clamp rather than writing two IF statements.

Answer and explanation

(a) health ← clamp(health - damage, 0, 100)

(b) 30 - 45 = -15; −15 < low (0), so clamp returns 0. health is 0.

(c) health ← clamp(health + 90, 0, 100): 20 + 90 = 110 > 100, so the result is 100.

(d) The intent is clear in one line, the procedure is already tested, and the same call handles both ends of the range, so there is less code to write and check.

Suggested Marlbridge rubric (5 marks)

Mark point Marks
(a) Uses clamp with health - damage and the range 0 to 100, assigned back to health 1
(b) 0 with the reason 1
(c) Correct statement for the heal 1
(c) Result 100 with the reason 1
(d) A valid advantage (clarity, already tested, less code) 1

How did you do?

Count the questions you got right first time, then read the explanation for every one you missed, including the wrong options you rejected correctly, because the distractors show you the typical slips. Anything you missed twice goes onto your revision list for the revision notes; re-read the matching section of the study guide before trying again.

AP® and Advanced Placement® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website. Marlbridge is an independent tuition provider. It is not an authorized AP course provider, an AP school or an exam centre, and these resources are not official College Board materials. Reading them or doing simulations does not meet the laboratory requirement of an AP science course.