Text Files Practice Questions (AP CSA)

Java MCQ with Answers


<-- Back to ArrayLists Next to MCQs-->



Text Files in Java

Reading a Text File with File and Scanner

In Java, a text file is read by first creating a File object that wraps the file path, and then passing it to a Scanner object.

The two most common patterns are:

File f = new File("name.txt");

Scanner sc = new Scanner(f);

and the shorthand

Scanner sc = new Scanner(new File("name.txt"));

Passing System.in instead of a File object creates a keyboard scanner, not a file scanner — a common source of errors.

Always call scanner.close() after reading to release the file resource.

Key Scanner Methods for File Reading

hasNext() returns true as long as there is another token in the file, making it the standard loop condition. nextLine() reads and returns the entire next line as a String, while next() reads only the next whitespace-delimited token. nextInt() reads the next token and parses it as an int.

A critical pitfall: calling nextInt() (or any nextXxx() method) more than once per loop iteration advances the scanner past additional tokens, causing incorrect reads or an InputMismatchException. Always store the result in a local variable before using it.

Parsing Lines with split() and substring()

String.split(delimiter) breaks a string into an array of tokens wherever the delimiter appears. The delimiter is treated as a regular expression, so split("-") splits on a hyphen, while split(" - ") splits only when a hyphen is surrounded by spaces. If the file data does not contain spaces around the delimiter, using split(" - ") will not split the line at all. substring(from, to) returns the characters from index from up to but not including index to.

Exception Handling

Because file operations can fail at runtime, code that reads files must be wrapped in a try-catch block. IOException is thrown when the file cannot be found or opened. InputMismatchException is thrown when a nextInt() call encounters a token that is not an integer. Both exceptions should be caught separately so the program can report a meaningful error message to the user.

Tips for AP-CSA Exam

In the AP Computer Science A (AP CSA) exam, questions on this topic test the ability to identify correct and incorrect ways to initialize a file Scanner, trace code that reads and processes file data line by line, detect bugs caused by calling nextInt() or next() multiple times in a single iteration, and choose the correct split() delimiter to match the actual file format.



Q1. Consider the file named rivers.txt which contains data related to rivers and the country in which they flow:

rivers.txt

Nile-United States
Amazon-Brazil
Ganges-India
Mississippi-United States

What is printed as a result of executing the following code segment?

File riverFile = new File("rivers.txt");
Scanner fileScanner = new Scanner(riverFile);
ArrayList result = new ArrayList();

while (fileScanner.hasNext())
{
    String[] temp = fileScanner.next().split(" - ");
    result.add(temp[0].substring(0, 2));
}
System.out.println(result);
fileScanner.close();

(A) [Uni, Bra, Ind, Uni]

(B) [Un, Br, In, Un]

(C) [Ni, Am, Ga, Mi]

(D) [Nil, Ama, Gan, Mis]

Answer:

View Output

(C) [Ni, Am, Ga, Mi]

Explanation:

String[] temp = fileScanner.next().split(" - ");

In the above code, the split() method uses hyphen along with space characters on both sides of it. However, in the file, rivers.txt, there exists no spaces before or after the hyphen. As a result, the split() method will not split each line into two words. The contents of the temp array will be:

temp[0] = Nile-United States

temp[1] = Amazon-Brazil

temp[2] = Ganges-India

temp[3] = Mississippi-United States

Hence, on execution of the code:

result.add(temp[0].substring(0, 2));

The result arraylist will contain the following:

[Ni, Am, Ga, Mi]

Note: The substring(int from, int to) method returns the substring beginning at index from and ending at index to-1.



Q2. Which one of the following is an incorrect way to initialize a Scanner object to read contents of a file named studentNames.txt:

(A) File inputFile = new File("studentNames.txt");
	Scanner scan = new Scanner(inputFile);

(B) Scanner scan = new Scanner(new File("studentNames.txt"));

(C) File scan = new File("studentNames.txt");
	Scanner inputFile = new Scanner(scan);

(D) File inputFile = new File("studentNames.txt");
	Scanner scan = new Scanner(System.in);

Answer:

View Output

(D) File inputFile = new File("studentNames.txt");
	Scanner scan = new Scanner(System.in);

Explanation:

Option (D) is incorrect because the Scanner object (scan) is creating an object on the System.in stream and not on the inputFile stream. As a result, scan object will not read data from the file; instead, it will read data from the user.




Q3. Consider the code below to read first and last names of students from a file named studentNames.txt, and calculate and print only the first names which are palindrome:

try {
		File inputFile = new File("studentNames.txt");
		Scanner input = new Scanner(inputFile);
		boolean found = false;
		while (input.hasNext()) {
			String line = input.nextLine();
			String rev = "";

            /* missing code */

			if (rev.equalsIgnoreCase(names[0])) {
				found = true;
				System.out.println(names[0]);
			}
		}

		if (!found)
			System.out.println("No names are palindrome.");
		input.close();

	} catch (IOException e) {
		System.out.println("File not found: " + e.getMessage());
	}

Contents of studentNames.txt:

Meera-Sharma
Jack-Oberoi
Sarah-Lee
Bob-Chan
Michael-Chang
Keerthana-Sri

Which one of the following can be used to replace /* missing code */ so that the code segment will work as intended?

(A) String[] names= line.split(" - ");
	for (int i = names[0].length() - 1; i >= 0; i--) {
			rev = rev + (names[0]).substring(i+1);
	}

(B) String[] names= line.split("-");
	for (int i = names[0].length() - 1; i >= 0; i--) {
			rev = rev + (names[0]).substring(i+1);
	}

(C) String[] names= line.split("-");
	for (int i = names[1].length() - 1; i >= 0; i--) {
			rev = rev + (names[1]).substring(i+1);
	}

(D) String[] names= line.split("-");
	for (int i = names[0].length() - 1; i >= 0; i--) {
			rev = (names[0]).substring(i+1) + rev;
	}

Answer:

View Output

(B)	String[] names= line.split("-");
	for (int i = names[0].length() - 1; i >= 0; i--) {
			rev = rev + (names[0]).substring(i+1);
	}

Explanation:

Option (A) is incorrect because the split() method is using hyphen (-) along with space characters on both sides of it. However, in the file, studentNames.txt, there exists no spaces before or after the hyphen. As a result, the split() function will not split each line into two names.

Option (C) is incorrect because it is checking whether the last name of the student (stored in names[1]) is a palindrome or not; whereas the code requires to check the first name of the students (which is stored in names[0]).

Option (D) is incorrect because it does not correctly reverse the name. The string rev will contain the original name itself, instead of the reversed name.

Option (B) is correct, because it correctly splits the names on each line, to separate only the first names in the names array. It then correctly iterates over the characters of each name and creates the rev string which stores the name in reverse order.



Q4. Consider the code below to read numbers from a file and separate them out into even and odd numbers and put them into respective ArrayLists. The program does NOT work as intended.

try {
File inputFile = new File("numbers.txt");
Scanner input = new Scanner(inputFile);

ArrayList evenList = new ArrayList();
ArrayList oddList = new ArrayList();

while (input.hasNext()) {
	if (input.nextInt() % 2 == 0)
evenList.add(Integer.valueOf(input.nextInt()));
	else
oddList.add(Integer.valueOf(input.nextInt()));
	}
System.out.println("Even numbers: " + evenList);
System.out.println("Odd numbers: " + oddList);
input.close();

} catch (IOException e) {
	System.out.println("File not found: " + e.getMessage());
}
catch (InputMismatchException e) {
	System.out.println("Incompatible data in file ");
}


Which one of the following can be used to replace the if-else block so that the code segment will work as intended?

(A) if (input.nextInt() % 2 == 0)
        oddList.add(Integer.valueOf(input.nextInt()));
	else
        evenList.add(Integer.valueOf(input.nextInt()));

(B) int num = input.nextInt();
    if (num % 2 == 0)
		evenList.add(Integer.valueOf(num));
	else
		oddList.add(Integer.valueOf(num));

(C) int num = input.nextInt();
    if (num % 2 != 0)
		evenList.add(Integer.valueOf(num));
	else
		oddList.add(Integer.valueOf(num));

(D) if (input.nextInt() % 2 != 0)
        evenList.add(Integer.valueOf(input.nextInt()));
	else
        oddList.add(Integer.valueOf(input.nextInt()));

Answer:

View Output

(B)	int num = input.nextInt();
    if (num % 2 == 0)
		evenList.add(Integer.valueOf(num));
	else
		oddList.add(Integer.valueOf(num));

Explanation:

Option (A) is incorrect because it repeatedly invokes the nextInt() method. Every time this method is invoked, the scanner reads the next integer from the file. As a result, the numbers are incorrectly segregated.

Option (C) is incorrect because, in the if-condition, it compares (num %2 !=0), which is true for odd numbers, but puts these numbers in the evenList. Hence the numbers are incorrectly segregated.

Option (D) is incorrect because it repeatedly invokes the nextInt() method and also because, as per the if-condition, puts odd numbers in the evenList and vice-versa.

Finally, option (B) is correct. It reads the nextInt() only once and uses the correct condition in if statement to separate the numbers.


<-- Back to ArrayLists Next to MCQs-->