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
All Questions (10)
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.
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.'
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) 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.
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) `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`.
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.
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.
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) 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.