Application Program Interface (API) and Libraries
Help Questions
AP Computer Science A › Application Program Interface (API) and Libraries
Based on the documentation, which of the following are considered behaviors of a Robot object?
The data field representing the current direction.
The grid on which the robot operates, which is external to the robot object.
The actions moveForward(), turnLeft(), and getCoordinates().
The data fields representing the x and y coordinates.
Explanation
The correct answer is A. Behaviors are the actions that an object can perform, which are defined by its methods. The methods moveForward(), turnLeft(), and getCoordinates() represent the robot's behaviors. Choices B and C are attributes (data). Choice D describes the environment, not a behavior of the Robot object itself.
A programmer is building an application that needs to interact with a remote database over a network. What is the most appropriate and common practice for the programmer to take?
Write new, low-level networking code to handle the specific communication protocol of the database.
Store all database information in local text files to avoid the complexities of network programming.
Request that the user manually enter database commands into the console for the program to execute.
Utilize a pre-existing library, such as a JDBC driver, that provides classes and methods for database connectivity.
Explanation
The correct answer is B. Interacting with complex systems like databases is a standard problem for which robust libraries exist. Using a library like a JDBC (Java Database Connectivity) driver provides a reliable, tested API for this task, saving immense time and effort compared to writing the functionality from scratch (A). Choices C and D are not viable solutions for interacting with a remote database.
What is a primary advantage of utilizing pre-existing classes from a well-established library?
The program's overall file size is guaranteed to be smaller than if the functionality were custom-written.
It gives the programmer the freedom to modify the library's source code to fix any bugs they find.
It ensures the program will use less memory, as library code is always more memory-efficient.
It saves development time and leverages reliable, previously tested code for common problems.
Explanation
The correct answer is C. The main benefits of using libraries are code reuse, which saves significant development time, and reliability, as the code has likely been thoroughly tested and used by many others. Choices A and B are not guaranteed; a powerful library might add significant size or memory overhead. Choice D is often not possible, as libraries are frequently distributed without their source code.
A programmer needs to develop a feature that calculates the absolute value of an integer and generates a random decimal number between 0.0 and 1.0. What is the most efficient approach using the standard Java API?
Utilize the abs() and random() static methods from the built-in java.lang.Math class.
Write the necessary mathematical logic directly inside the main method where the calculations are needed.
Search for and download a third-party mathematics library that performs these specific calculations.
Create a new class named CustomMath with custom methods for absolute value and random number generation.
Explanation
The correct answer is C. The standard Java API provides the Math class specifically for common mathematical operations. Using its pre-existing, tested methods is the most efficient and reliable approach. Choices A and D involve unnecessarily reinventing existing functionality. Choice B is also unnecessary because these functions are already part of the standard, built-in library.
Which statement best describes the primary purpose of an Application Programming Interface (API) in Java?
It is the collection of all Java source code files that make up a program, organized into a single directory or project folder.
It is a security feature that prevents unauthorized access to the methods and variables within a class by encrypting the source code.
It is a tool that automatically compiles Java source code into bytecode that can be executed by the Java Virtual Machine.
It provides a specification of how to interact with a set of classes, detailing their methods and attributes without exposing the implementation.
Explanation
The correct answer is A. An API serves as a contract or specification that defines how different software components should interact. It exposes the necessary methods and attributes for a programmer to use a class or library without needing to know the underlying implementation details. Choice B describes a compiler. Choice C describes a program's source tree. Choice D incorrectly describes the role of access modifiers like private.
When an API provides a class, such as the ArrayList class, what does this class define for the programmer?
A new hardware requirement for running the program.
A new set of compiler rules for creating lists of variables.
A new primitive data type for storing a dynamic list of items.
A new reference type that can be used to declare variables and create objects.
Explanation
The correct answer is D. In Java, a class is a blueprint for objects and defines a reference type. A programmer can declare a variable of type ArrayList and instantiate an ArrayList object. Choices A and B are incorrect. Choice C is incorrect because ArrayList is a reference type, not a primitive type.
What is the primary role of the FileReader class, based on its documented API?
To represent the content of an entire file as a single String in memory.
To provide a user interface for browsing and selecting files from the computer's file system.
To define an object that can open a file and read its contents sequentially, line by line.
To store metadata about a file, such as its size and modification date, without reading its content.
Explanation
The correct answer is D. The combination of behaviors like open, readLine, and close strongly indicates that the class is designed for sequential file access. It manages the process of reading data from a file rather than just storing metadata (A), providing a GUI (B), or loading the entire file at once (C).
In the context of Java programming, what is a software library?
A set of rules that must be followed for Java code to be considered syntactically correct and compile without errors.
A collection of pre-written classes and methods that can be utilized by programmers to perform common tasks.
A text file that lists all the variables and methods used in a program, along with their data types and return types.
A program that allows a developer to write, compile, and execute code within a single integrated environment.
Explanation
The correct answer is B. A library is a collection of reusable code, typically in the form of classes and methods, designed to be used by other programs. This allows programmers to avoid rewriting code for common functionalities. Choice A describes an Integrated Development Environment (IDE). Choice C is a form of documentation. Choice D describes Java syntax rules.
Based on the documentation, which of the following are considered attributes of a Robot object?
The methods moveForward() and turnLeft() which alter the robot's position.
The Robot class itself, which serves as the blueprint for all robot objects.
The x and y coordinates and the current direction of the robot.
The getCoordinates() method, which reports the robot's current location.
Explanation
The correct answer is C. Attributes refer to the data or state that an object maintains. In this case, the robot's state is defined by its position (x and y coordinates) and its direction. Choices A and B describe behaviors, which are actions implemented as methods. Choice D refers to the class definition, not the attributes of a specific instance.
A programmer wants to use a class from a third-party library but does not have access to its source code. Which resource is essential for the programmer to effectively use the class?
The original design documents and flowcharts used by the library's development team.
A decompiler to reverse-engineer the bytecode of the class and reveal its implementation.
A report of all syntax errors found when the library was last compiled by its creators.
The Application Programming Interface (API) documentation that describes the class's public methods and attributes.
Explanation
The correct answer is D. The API documentation serves as the official guide for programmers, explaining what a class does and how to use its public members (methods, constructors, attributes) without needing to see the internal source code. The other options are either unnecessary, impractical, or irrelevant for the task of using a pre-compiled library class.