Lesson 1.7: Application Program Interface (API) and Libraries | AP CSA

Unit 1 · Lesson 1.7 · Conceptual

Lesson 1.7: Application Program Interface (API) and Libraries

Reading time: 6–8 min · Practice: 6 exercises · Mastery: applied scenario

What you'll learn in this lesson

  • 1.7.A. Explain what a library is, what an API specification does, and how classes are organized into packages.
  • 1.7.A. Distinguish between a class’s attributes (data stored in variables) and its behaviors (actions defined by methods).

This lesson is part of the AP CSA Course and Exam Description (effective May 2027).

AP exam weight: API vocabulary appears in context: the exam gives you a class diagram or partial documentation and asks you to identify attributes, behaviors, or how to use a class from its specification. You will not be asked to recall specific API details from memory.

Key Vocabulary

Term Definition
library A collection of pre-written classes that programmers can use without implementing the underlying code.
API Application Program Interface; documentation specifying how to use a library's classes and their methods.
package A named grouping of related classes in Java, e.g. java.util contains Scanner.
attribute Data stored in a class, defined by a variable (field). Describes what an object has.
behavior An action a class instance can perform, defined by a method. Describes what an object does.
procedural abstraction Using a method by knowing what it does from the API, without knowing how it is internally implemented.

Libraries: organized collections of classes

A library is a collection of pre-written classes that programmers can use without writing the code themselves. Java’s standard library includes hundreds of classes covering math, input/output, data structures, networking (see the Java SE 17 API documentation), and more.

How Libraries Are Organized

Classes in Java libraries are grouped into packages. A package is a named namespace that keeps related classes together and avoids naming conflicts. For example, Scanner lives in java.util, and Math lives in java.lang. Classes in java.lang are available by default; classes in other packages typically require an import statement.

API: the specification for how to use a library

An Application Program Interface (API) specification documents how to use the classes in a library. It tells you what a class does, what methods it provides, what parameters each method requires, and what each method returns. You do not need to know how a method is implemented internally to use it correctly; you only need to read its API documentation.

Why API Documentation Matters

A class defines a specific reference type. The API specification tells the programmer how to create instances of that type and what they can do with those instances. AP CSA provides a Java Quick Reference during the exam precisely because programmers use API documentation rather than memorizing implementation details.

Attributes and behaviors

Every class has two kinds of members:

The Distinction

Attributes refer to the data related to the class. They are stored in variables (also called fields or instance variables). They describe what an object has.

Behaviors refer to what instances of the class can do. They are defined by methods. They describe what an object does.

Example: a BankAccount class

Attributes: balance (double), accountNumber (String), ownerName (String)
Behaviors: deposit(amount), withdraw(amount), getBalance()

From the API, a programmer knows what deposit does (adds money to the balance) and what parameter it takes (an amount). The programmer does not need to know whether the method uses addition or some other mechanism internally.

AP Trap: attributes vs behaviors

On the exam, a question may show you a class description and ask whether something is an attribute or a behavior. Variables are always attributes; methods are always behaviors. If a question describes something a class “stores” or “tracks,” that is an attribute. If it describes something a class “can do” or “returns,” that is a behavior.

Tier 2 · AP Practice

Practice Questions

A Student class has the following members: a String variable name, an int variable gradeLevel, and a method getGPA() that returns a double. Which of the following correctly classifies these members?
A. name and gradeLevel are behaviors; getGPA() is an attribute.
B. name and gradeLevel are attributes; getGPA() is a behavior.
C. All three are attributes because they belong to the same class.
D. All three are behaviors because they can all be accessed by a programmer using the class.
B. Attributes are data stored in variables. name and gradeLevel are variables, so they are attributes. Behaviors are defined by methods. getGPA() is a method, so it is a behavior.
A programmer wants to use the ArrayList class from Java’s standard library. According to the API specification, ArrayList has an add(element) method. Which of the following does the API specification allow the programmer to know without reading the implementation code?
A. The internal data structure used to store the elements.
B. The memory address where the ArrayList object is stored.
C. How many CPU instructions the add method executes.
D. What parameter add requires and what effect calling it has on the list.
D. An API specification documents what a method does, its parameter types, and its return type. That is exactly what a programmer needs to use the method correctly. The internal implementation (data structure, memory layout, CPU instructions) is hidden by abstraction and not in the API.
Which of the following best describes the purpose of packages in Java?
A. Packages group related classes together and prevent naming conflicts between classes in different libraries.
B. Packages store the compiled bytecode of Java programs for faster execution.
C. Packages restrict which methods a class can call on another class.
D. Packages automatically import all classes so no import statement is needed.
A. Packages organize related classes under a named namespace. Two different libraries can each define a class named List without conflict as long as they are in different packages. B is wrong: packages are organizational, not a bytecode cache. C is wrong: access is controlled by access modifiers, not packages. D is wrong: only java.lang classes are available by default; others require import.
A Car class has the following members:

I. A String variable model
II. A method accelerate(int speed)
III. A double variable fuelLevel

Which of the above are attributes?
A. II only
B. I and II only
C. I and III only
D. I, II, and III
C. Attributes are stored in variables. model (I) and fuelLevel (III) are variables—both are attributes. accelerate (II) is a method, so it is a behavior, not an attribute.
According to the AP CSA CED, “a class defines a specific reference type.” Which of the following best explains what this means?
A. A class is a variable that stores a reference to a primitive value.
B. Declaring a class creates a new type; variables of that type hold references to objects (instances) of the class, not the objects themselves.
C. Every class must contain exactly one reference variable as an attribute.
D. A class definition is stored as a reference in memory, unlike primitive types which are stored as values.
B. When you define a class Dog, you create a new type. A variable declared as Dog d holds a reference (memory address) to a Dog object, not the object’s data directly. This is how all reference types work in Java, in contrast to primitive types like int which store values directly.
A student uses a method Math.sqrt(x) from Java’s Math class without knowing how the square root is computed internally. This is an example of which programming concept?
A. Procedural abstraction, because the programmer can use the method by knowing what it does without knowing how it is implemented.
B. Integer overflow, because square root operations can exceed int range.
C. Encapsulation failure, because the internal algorithm is not exposed to the programmer.
D. Widening conversion, because the method converts the input to a double before computing.
A. Procedural abstraction means a programmer can use a method by knowing its name, parameters, and what it returns, without needing to understand the internal implementation. Using Math.sqrt from the API without seeing the source code is a direct example. B, C, and D describe unrelated concepts.
Tier 3 · AP Mastery Challenge

The Library Card System

A developer is designing a LibraryCard class. The class needs to track the cardholder’s name, the number of books currently checked out, and the card’s expiration year. It should also be able to check out a book, return a book, and report whether the card is expired.

Part A: Classify the members

Based on the description above, which of the following correctly identifies one attribute and one behavior of the LibraryCard class?
A. Attribute: checkOut() Behavior: cardholderName
B. Attribute: isExpired() Behavior: booksCheckedOut
C. Attribute: returnBook() Behavior: expirationYear
D. Attribute: booksCheckedOut Behavior: isExpired()
D. booksCheckedOut is a variable that stores data, making it an attribute. isExpired() is a method that performs an action and returns a result, making it a behavior. In A, B, and C the labels are reversed.

Part B: API usage

A programmer wants to use LibraryCard but has not seen its source code. The API documentation states: void checkOut(String title) — Adds one book to the checked-out count. Which of the following can the programmer conclude from this documentation alone?
A. The method uses an array internally to store book titles.
B. Calling the method twice will always throw an exception.
C. The method takes a String parameter and returns no value, and calling it increases the checked-out count.
D. The method can only be called once per LibraryCard object.
C. The API signature void checkOut(String title) tells us: return type is void (no return value), the parameter is a String named title, and the description says it increments the count. A is an implementation detail not in the API. B and D are restrictions the API does not mention.

Part C: Structured response

A teammate argues that since all the code in LibraryCard was written by someone else, using it without reading the implementation is risky. In three to five sentences, explain why API documentation makes it safe and efficient to use a class without reading its implementation, and what the key information in an API specification provides to a programmer.

Extension · Beyond the Exam

Javadoc: generating API documentation from code

Java’s javadoc tool reads /** */ comment blocks in source code and generates HTML API documentation automatically. The Java standard library documentation at docs.oracle.com is produced this way. Every public method, parameter, and return value you see in the official docs came from a /** */ comment in the Java source code. Lesson 1.8 covers the comment syntax.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]