Libraries

Help Questions

AP Computer Science Principles › Libraries

Questions 1 - 10
1

A student is writing a program that simply adds two user-provided numbers and displays the result. The student considers importing a large, advanced scientific computing library that contains thousands of procedures. Which of the following is the most appropriate evaluation of this choice?

This is a good choice because it allows the program to be easily expanded with scientific features later.

This is a poor choice because libraries cannot be used for basic arithmetic operations like addition.

This is a good choice because the library may add the numbers more accurately than the language's built-in operators.

This is a poor choice because the library is unnecessarily complex and large for such a simple task.

Explanation

Part of selecting appropriate libraries is choosing one that fits the scale of the problem. Using a massive library for a task that can be accomplished with a single line of basic code is inefficient, adding unnecessary size and complexity to the program.

2

A programmer needs to sort a large list of names alphabetically. The programmer can either write a custom sorting algorithm or use a sorting procedure from a standard, built-in library. Why is using the library procedure often a better choice?

Library procedures allow the programmer to see and modify the underlying sorting logic easily.

Library procedures are typically less efficient but are easier to read than custom algorithms.

Library procedures require significantly more memory but can sort data types that custom algorithms cannot.

Library procedures are often highly optimized and rigorously tested, making them more reliable and efficient.

Explanation

Standard library functions, especially for common tasks like sorting, have been developed by experts, optimized for performance, and tested across a wide range of scenarios. This makes them generally more reliable and faster than a typical custom-written algorithm.

3

A software company releases a new version of its application. The company states that the new version is more reliable because many parts of the program were rewritten using industry-standard libraries. Why would this change likely increase the program's reliability?

Standard libraries are used and tested by a large community of developers, which helps to find and fix bugs.

Standard libraries are always open source, allowing the company to customize every aspect of their behavior.

Standard libraries are smaller in size, which reduces the chances of a hardware failure while running.

Standard libraries are encrypted, preventing users from introducing errors into the application.

Explanation

Widely used standard libraries benefit from the collective testing of thousands of developers and users. This extensive use under varied conditions makes them more robust and reliable than code written and tested by a small, internal team.

4

A student is working on a programming project and incorporates a code segment that was previously written for a different project. This is an example of reusing code. How does the concept of a software library relate to this practice?

Libraries formalize code reuse by organizing existing code segments into a distributable and documented package.

Libraries discourage code reuse by requiring programmers to request permission for each use.

Libraries prevent code reuse by encrypting code segments so they cannot be copied.

Libraries are programs that automatically find and reuse code segments from other projects on a computer.

Explanation

Software libraries are a formal, structured way to facilitate code reuse. They package useful, existing code into a module that can be easily shared, integrated into new projects, and used via its documented API.

5

A programmer is building an application that needs to read data from a specific file format (e.g., a PDF or an Excel spreadsheet). What is the most significant advantage of using an existing library designed for that file format?

The library allows the application to read data from any file format, not just the specified one.

The library handles the complex details of the file format's structure, allowing the programmer to access the data easily.

The library will convert the file into a plain text file, which is easier to work with.

The library guarantees that any data read from the file will be free of errors or inaccuracies.

Explanation

File formats have complex, specific rules about how data is stored. A library abstracts these details away, providing simple procedures to read the data without the programmer needing to understand the format's internal structure.

6

A programmer is building an application that needs to generate and display complex charts and graphs based on user-provided data. Which of the following is the most significant benefit of using a data visualization library for this task?

It reduces the amount of data the user needs to input into the application.

It simplifies the development process by providing pre-built, tested procedures for creating charts.

It ensures the application will run faster than any other application that performs similar tasks.

It automatically cleans and validates the user-provided data to prevent all possible errors.

Explanation

The primary benefit of using a library is simplification and efficiency in development. A data visualization library contains complex, pre-written procedures for chart generation, saving the programmer from having to write them from scratch and allowing them to focus on other parts of the application.

7

A programmer is developing a complex video game and needs to implement realistic physics for moving objects. Instead of writing the physics calculations from scratch, the programmer decides to use a pre-written collection of code. What is this collection of pre-written, reusable code known as?

A compiler

A software library

A conditional statement

An event handler

Explanation

A software library is a collection of pre-written, reusable code, procedures, and data that can be used by programmers to develop software. A compiler translates code, an event handler responds to actions, and a conditional statement makes decisions.

8

A programmer writes a useful procedure for converting temperatures from Celsius to Fahrenheit. The programmer then groups this procedure with other temperature-related procedures into a file that can be easily included in future projects. How does this practice relate to the concept of software libraries?

It is the fundamental process of creating a small, personal library for code reuse.

It is a method of debugging that only works on procedures that are not in a library.

It is a form of procedural abstraction that prevents the code from being reused.

It is an example of data compression, which is the opposite of creating a library.

Explanation

Creating a library begins with identifying reusable code (like conversion procedures), and then organizing and packaging that code in a way that makes it easy to incorporate into other programs. This promotes the key principle of code reusability.

9

In a data analysis project, Maya uses a library (prewritten code) called Pandas to load a large school survey CSV and summarize responses. She writes import pandas as pd then df = pd.read_csv("survey.csv") to create a DataFrame, which lets her filter rows and compute averages efficiently. Based on the scenario described, how does read_csv from Pandas improve efficiency?

It loads CSV data into a DataFrame in one call

It draws a line graph directly from the file path

It automatically trains a model to predict missing values

It requires manual parsing of each character first

Explanation

This question tests understanding of programming libraries and their application in computational tasks, a key concept in AP Computer Science Principles. Libraries provide pre-written code that programmers can use to perform common tasks efficiently, such as data manipulation, visualization, and complex calculations. In this scenario, the Pandas library is used to load and analyze survey data, leveraging its read_csv function to simplify and optimize the process. Choice B is correct because it accurately describes how read_csv from Pandas fulfills the task requirements by loading CSV data into a DataFrame structure in a single function call, eliminating the need for manual file parsing. Choice D is incorrect because it suggests manual parsing is required, a common error when students don't understand that libraries abstract away low-level operations. To help students: Demonstrate loading CSV files both with and without libraries to show the efficiency gain. Encourage hands-on practice with real datasets to reinforce the convenience of library functions. Watch for: Students confusing library functions with built-in Python functions, or assuming libraries perform tasks beyond their scope.

10

A physics student uses NumPy, a library that supports arrays and math functions, to compute distances: import numpy as np; x = np.array(<u>3,4</u>); d = np.sqrt(x<u>0</u>**2 + x<u>1</u>**2). Based on the scenario described, which NumPy function is used to compute the square root?

np.show()

np.sqrt[x]

np.read_csv()

np.sqrt()

Explanation

This question tests understanding of programming libraries and their application in computational tasks, a key concept in AP Computer Science Principles. Libraries provide pre-written code that programmers can use to perform common tasks efficiently, such as data manipulation, visualization, and complex calculations. In this scenario, the NumPy library is used to compute distances using mathematical functions, leveraging its sqrt function to simplify and optimize the calculation process. Choice A is correct because it accurately describes how np.sqrt() from NumPy fulfills the task requirements by computing the square root needed for the distance formula. Choice D is incorrect because it uses square brackets instead of parentheses, a common error when students confuse array indexing syntax with function call syntax. To help students: Emphasize the difference between function calls with parentheses and array indexing with brackets. Encourage practice with various NumPy mathematical functions to reinforce proper syntax. Watch for: Syntax confusion between different operations, particularly mixing up indexing and function calling conventions.

Page 1 of 3