Application Program Interface (API) and Libraries - AP Computer Science A

  1. Which statement accurately describes the organizational hierarchy of classes, packages, and libraries in Java?
    Answer: A library may contain multiple packages, and a package contains multiple related classes. The correct answer is D. The standard hierarchy is that a library (a collection of reusable code) is organized into packages, which are used as namespaces to group related classes. Therefore, a library contains packages, and packages contain classes. The other options incorrectly describe this relationship.
  2. What does the boolean keyword in the method signature indicate about the play method?
    Answer: The method returns a value of either true or false after it completes its execution. The correct answer is D. In a method signature, the type listed before the method name is the return type. boolean indicates that the method will return a Boolean value, which is typically used to signal success (true) or failure (false). Choice A describes a static method. Choice B is a misinterpretation of the return type as a parameter. Choice C describes abstraction, which is a general concept not specific to the boolean keyword.
  3. Which of the following statements about the relationship between APIs, libraries, and methods is most accurate?
    Answer: An API defines the interface for accessing library functionality, while methods are the actual implementations within the library The correct answer is A. An API (Application Programming Interface) specifies how to interact with library components, defining method signatures and expected behaviors, while the library contains the actual method implementations. Choice B reverses the relationship - libraries contain methods that are accessed through APIs. Choice C incorrectly suggests methods contain APIs rather than the reverse. Choice D incorrectly treats APIs and libraries as the same thing, when APIs are specifications for using libraries.
  4. When an API provides a class, such as the ArrayList class, what does this class define for the programmer?
    Answer: A new reference type that can be used to declare variables and create objects. 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.
  5. In the context of Java programming, what is a software library?
    Answer: A collection of pre-written classes and methods that can be utilized by programmers to perform common tasks. 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.
  6. 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?
    Answer: The Application Programming Interface (API) documentation that describes the class's public methods and attributes. 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.
  7. 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?
    Answer: Utilize a pre-existing library, such as a JDBC driver, that provides classes and methods for database connectivity. 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.
  8. After all operations are completed, what is the final state of the ArrayList and what value does list.indexOf("cherry") return?
    Answer: Final list: ["apricot", "cherry"], indexOf("cherry") returns 1 When you encounter ArrayList manipulation problems, you need to trace through each operation step-by-step, keeping track of how the list changes after each method call. Let's walk through each operation: 1. Start with an empty ArrayList 2. add("apple") → ["apple"] 3. add("banana") → ["apple", "banana"] 4. add(1, "cherry") → ["apple", "cherry", "banana"] (inserts "cherry" at index 1, shifting "banana" right) 5. set(0, "apricot") → ["apricot", "cherry", "banana"] (replaces element at index 0) 6. remove("banana") → ["apricot", "cherry"] (removes first occurrence of "banana") The final list is ["apricot", "cherry"], and indexOf("cherry") returns 1 since "cherry" is at index 1. Looking at the wrong answers: Choice A incorrectly shows "banana" still in the list, missing that it was removed. Choice B correctly identifies the final list but claims indexOf("cherry") returns 0, which would mean "cherry" is at the first position. Choice C has the elements in reverse order, suggesting confusion about how add(index, element) works—it doesn't replace elements, it inserts them. Study tip: Practice tracing ArrayList operations by writing down the list state after each method call. Pay special attention to add(index, element) which inserts (shifts elements right) versus set(index, element) which replaces. Also remember that indexOf() returns the first occurrence's index, or -1 if not found.
  9. 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?
    Answer: Utilize the abs() and random() static methods from the built-in java.lang.Math class. 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.
  10. Based on this description, which of the following programming constructs most likely represents this component?
    Answer: A class, which encapsulates both data (the collection) and behaviors (add, remove, count). The correct answer is B. The description outlines an entity with both state (the collection of players) and defined behaviors (adding, removing, counting). This combination of data and related operations is the definition of a class. An attribute is only data, a method is only a single behavior, and a package is a grouping of classes.
  11. A programmer wants to use the ArrayList class to store Integer objects and needs to remove all elements that are greater than 50. Which of the following approaches will correctly accomplish this task without causing runtime errors?
    Answer: Use a standard for loop iterating backward through the list and call remove() when an element is greater than 50 The correct answer is B. When removing elements from an ArrayList during iteration, iterating backward prevents index shifting issues that occur when elements are removed. Choice A will cause IndexOutOfBoundsException because removing elements shifts subsequent elements to lower indices. Choice C will throw ConcurrentModificationException because you cannot modify a collection during an enhanced for loop. Choice D is incorrect because enhanced for loops don't provide direct access to the Iterator's remove() method.
  12. When using the Random class in Java, which of the following statements about the nextInt() method overloads is correct?
    Answer: nextInt() returns values from Integer.MIN_VALUE to Integer.MAX_VALUE, while nextInt(n) returns values from 0 to n-1 inclusive The correct answer is B. The parameterless nextInt() method returns any int value including negative values (Integer.MIN_VALUE to Integer.MAX_VALUE), while nextInt(n) returns values from 0 to n-1 inclusive (n is exclusive). Choice A incorrectly states that nextInt() only returns non-negative values and that nextInt(n) includes n. Choice C makes the same error about nextInt() range and incorrectly states nextInt(n) starts from 1. Choice D correctly identifies nextInt() range but incorrectly describes nextInt(n) as starting from 1.
  13. Based on the documented attributes and behaviors, what is the most likely purpose of this Color class?
    Answer: To represent and manipulate a single color value within a graphics application. The correct answer is A. The attributes (red, green, blue components) and behaviors (brighter, darker) are all characteristic of an object designed to represent a single, specific color and allow for its modification. The other choices describe different functionalities: a color palette manager (B), a drawing tool (C), or a simple string representation (D).
  14. A programmer wishes to use the Scanner class, which is part of the java.util package. Which of the following must be done to make this class available in their program?
    Answer: The programmer must include an import statement, such as import java.util.Scanner;, at the beginning of the source file. The correct answer is B. With the exception of the java.lang package, classes from other packages in the Java standard library must be explicitly imported using an import statement. Choice A is incorrect; only java.lang is imported automatically. Choices C and D are factually incorrect regarding the use of standard Java libraries.
  15. Why can a programmer use classes like String and Math without an import statement?
    Answer: They are part of the java.lang package, which is automatically imported into every Java program by the compiler. The correct answer is C. The Java language specification dictates that the java.lang package, which contains fundamental classes like String, Math, and Object, is implicitly imported into every Java source file. Choice A is incorrect; String is a reference type. Choice B is incorrect; they are part of the standard library. Choice D is incorrect; this behavior is a feature of the Java language itself, independent of any IDE.
  16. Based on the documentation, which of the following are considered behaviors of a Robot object?
    Answer: The actions moveForward(), turnLeft(), and getCoordinates(). 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.
  17. If the method is called with the list ["Alice", "Robert", "Jo", "Elizabeth"], what will be the contents of the returned ArrayList?
    Answer: ["alice", "ROBER", "jo", "ELIZA"] This question tests your understanding of string manipulation methods and conditional logic in Java. When tracing through code that processes collections, you need to carefully follow each step and apply the correct methods based on the conditions. Let's trace through the method with the input ["Alice", "Robert", "Jo", "Elizabeth"]: For "Alice" (length = 5): Since 5 is not greater than 5, the condition name.length() > 5 is false, so we execute name.toLowerCase(), giving us "alice". For "Robert" (length = 6): Since 6 > 5, the condition is true, so we execute name.substring(0, 5).toUpperCase(). The substring from index 0 to 5 gives us "Rober", and converting to uppercase yields "ROBER". For "Jo" (length = 2): Since 2 is not greater than 5, we use toLowerCase(), resulting in "jo". For "Elizabeth" (length = 9): Since 9 > 5, we take the first 5 characters with substring(0, 5) to get "Eliza", then convert to uppercase: "ELIZA". The final result is ["alice", "ROBER", "jo", "ELIZA"], which matches answer C. Answer A incorrectly assumes all names get converted to uppercase. Answer B incorrectly assumes no transformations occur. Answer D incorrectly assumes all names get converted to lowercase, ignoring the substring operation for longer names. When tracing through string methods, remember that substring(0, n) extracts exactly n characters starting from index 0, and always check your boundary conditions carefully—"greater than" versus "greater than or equal to" makes a crucial difference.
  18. Based on this method signature from the API, what can a programmer conclude about this behavior?
    Answer: The method requires two integer parameters and returns a new String object. The correct answer is C. The signature clearly shows two int parameters (from, to) inside the parentheses and a return type of String before the method name. This indicates the method takes two integers and returns a String. Choice A is incorrect as String objects are immutable. Choice B is incorrect because the return type is String, not void. Choice D is incorrect because the return type is String, not int.
  19. What are the values of result1 and result2 respectively?
    Answer: result1 = 6.7, result2 = 5.0 The correct answer is A. Math.abs(-3.7) = 3.7, Math.ceil(2.3) = 3.0, so result1 = 3.7 + 3.0 = 6.7. Math.floor(3.7) = 3.0, Math.round(2.3) = 2, so result2 = 3.0 + 2.0 = 5.0. Choice B incorrectly calculates Math.round(2.3) as 3 instead of 2. Choice C incorrectly calculates Math.ceil(2.3) as 2.0 instead of 3.0. Choice D makes both errors from choices B and C.
  20. Which of the following best distinguishes between the attributes and behaviors of a class as represented in its API documentation?
    Answer: Attributes represent the data or state of an object, while behaviors represent the actions an object can perform. The correct answer is C. This is the fundamental definition in object-oriented programming. Attributes (often implemented as instance variables) hold the data that defines an object's state. Behaviors (implemented as methods) define the operations that can be performed by or on that object. The other options present incorrect or incomplete distinctions.

Start this quiz