PrepGo

AP Computer Science A Practice Quiz: Using Text Files

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 10 questions to check your progress.

Question 1 of 10

According to the provided content, what is the primary characteristic of data stored in a file?

All Questions (10)

According to the provided content, what is the primary characteristic of data stored in a file?

A) It is only accessible while the program is running.

B) It persists even when the program is not running.

C) It can only store integer values.

D) It is automatically deleted after being read once.

Correct Answer: B

The content states that a file is 'storage for data that persists when the program is not running,' meaning the data remains available after the program has terminated.

When writing a method that uses the `File` class to open a file, what is required in the method's header to handle potential opening errors?

A) throws FileNotFoundException

B) public static void

C) throws IOException

D) return null

Correct Answer: C

The content explicitly states that one way to handle the case where a file cannot be opened is to 'add throws IOException to the header of the method that uses the file.'

Which constructor from the Java Quick Reference is used to create a `Scanner` object to read data from a file?

A) Scanner(System.in)

B) Scanner(File f)

C) Scanner(String s)

D) Scanner()

Correct Answer: B

The provided content lists `Scanner(File f)` as the constructor included in the Java Quick Reference for reading from a file.

A text file contains the single line: `true 99.5`. If a `Scanner` object `sc` is reading from this file, what is the result of calling `sc.nextBoolean()` followed by `sc.nextDouble()`?

A) The code will throw an error because you cannot read two different data types consecutively.

B) It will read the value `true` and then the value `99.5`.

C) It will read the value `true` and then the entire rest of the line, ` 99.5`.

D) It will read the value `false` because the line contains more than just a boolean.

Correct Answer: B

The `Scanner` methods `nextBoolean()` and `nextDouble()` are designed to read the next token and parse it as the specified type. The first call reads 'true', and the second call reads the next token, '99.5', and parses it as a double.

To read all the data from a file, a `while` loop is often used. Which `Scanner` method is most appropriate to use as the condition for such a loop?

A) nextLine()

B) hasNext()

C) close()

D) nextInt()

Correct Answer: B

The `hasNext()` method returns `true` if there is more data to be read from the file and `false` otherwise. This makes it the ideal condition for a `while` loop that should continue as long as there is data left to process.

A file contains a single line of text: `Java Programming`. If a `Scanner` object `sc` is created for this file, what will be returned by the first call to `sc.next()`?

A) `Java Programming`

B) `Java`

C) `J`

D) An empty string

Correct Answer: B

The `next()` method reads the next token from the input, where tokens are separated by whitespace. In the string `Java Programming`, the first token is `Java`.

Consider the goal of developing code to read data from a text file. Based on the provided content, which of the following is a necessary step in the process?

A) Ensuring the file is empty before writing new data to it.

B) Indicating how to handle a potential `IOException` if the file cannot be opened.

C) Converting all data to strings before processing.

D) Creating a backup of the file before reading from it.

Correct Answer: B

The content explicitly states that 'When using the File class, it is required to indicate what to do if the file with the provided name cannot be opened,' for example, by adding `throws IOException` to the method header. This is a required part of developing the code.

Which `Scanner` method should be used to read an entire line of text, including spaces, from a file?

A) next()

B) nextInt()

C) nextLine()

D) nextDouble()

Correct Answer: C

The `nextLine()` method is designed to read all characters from the current position up to the next line separator, which allows it to capture entire lines of text that include spaces.

After a program has finished reading all the data it needs from a file, which `Scanner` method from the Java Quick Reference should be called to release the resources associated with the file?

A) hasNext()

B) next()

C) Scanner(File f)

D) close()

Correct Answer: D

The `close()` method is provided to close the scanner and release any system resources it holds. It is good practice to call this method when you are done with the `Scanner` object.

A programmer is writing a method to read a single integer value from a file named `scores.txt`. According to the provided content, which combination of elements is required to accomplish this task correctly?

A) Using the `Scanner(File f)` constructor, the `nextInt()` method, and adding `throws IOException` to the method header.

B) Using the `next()` method and casting the result to an integer.

C) Using the `hasNext()` method in a loop and the `close()` method.

D) Using the `Scanner(File f)` constructor and the `nextLine()` method without any error handling.

Correct Answer: A

This task requires three components mentioned in the text: 1) Creating a Scanner for the file using `Scanner(File f)`. 2) Reading the integer using `nextInt()`. 3) Handling the potential file-opening error, for which `throws IOException` is the specified mechanism.