Using Text Files MCQ Practice

Question 1 of 10

Hearthwick Bakery's program closed yesterday after reading yeast.txt. No program is running now. What is true of yeast.txt?

  1. A. The file still exists on disk.
  2. B. The file was erased when close ran.
  3. C. The file exists only inside the finished program's variables.
  4. D. The file cannot be opened until it is typed again on the keyboard.

Question 2 of 10

Ridgeline Weather must connect a Scanner to gusts.txt. Which line is valid?

  1. A. Scanner in = new Scanner("gusts.txt");
  2. B. Scanner in = new Scanner(new File("gusts.txt"));
  3. C. File in = new File(); in.nextInt();
  4. D. Scanner in = new Scanner(System.in); in = new File("gusts.txt");

Question 3 of 10

Seaglass Lab stores integer pH readings in vial.txt. What text does this Java code display?

Token order Value
first 7
second 8
third 6
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class VialRead {
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(new File("vial.txt"));
        int count = 0;
        int sum = 0;
        while (in.hasNext()) {
            sum += in.nextInt();
            count++;
        }
        in.close();
        System.out.print(sum + ":" + count);
    }
}
  1. A. 21:3
  2. B. 7:3
  3. C. 21:1
  4. D. 786:3

Question 4 of 10

A method is written without throws IOException: public static void load() { Scanner in = new Scanner(new File("map.txt")); }. What is the compile-time problem?

  1. A. File names cannot contain a dot.
  2. B. Scanner cannot be declared in a method.
  3. C. Constructing Scanner with a File can throw IOException, so the method must declare throws IOException (or handle it).
  4. D. close is missing, and that is a compile error.

Question 5 of 10

Pinewick Camp's pathname camp-log.txt is misspelled as camplog.txt and that file does not exist. loadNotes throws IOException and does not catch it. What happens?

  1. A. hasNext returns false and the method returns 0.
  2. B. The program terminates because of IOException.
  3. C. Java creates camplog.txt with zeros.
  4. D. next() returns the misspelled name as a String.

Question 6 of 10

Pinewick Camp reads two whole lines from note.txt using only nextLine on that Scanner. The table is the file. What text does this Java code display?

Line Text
1 harbor
2 closed
Scanner in = new Scanner(new File("note.txt"));
String headline = in.nextLine();
String body = in.nextLine();
in.close();
System.out.print(headline);
  1. A. closed
  2. B. harborclosed
  3. C. harbor
  4. D. note.txt

Question 7 of 10

Mirewick Locks reads only nextDouble tokens from rise.txt: 1.5, 2.5, 0.5. What text does this Java code display?

Scanner in = new Scanner(new File("rise.txt"));
double last = 0.0;
while (in.hasNext()) {
    last = in.nextDouble();
}
in.close();
System.out.print(last);
  1. A. 1.5
  2. B. 4.5
  3. C. 3
  4. D. 0.5

Question 8 of 10

Ashgrove Flags stores boolean tokens false, true, false. What text does this Java code display?

Scanner in = new Scanner(new File("flags.txt"));
boolean any = false;
while (in.hasNext()) {
    if (in.nextBoolean()) {
        any = true;
    }
}
in.close();
System.out.print(any);
  1. A. false
  2. B. 2
  3. C. true
  4. D. false true false

Question 9 of 10

Quayline Market stores each stall as a comma token with no spaces. tokens are 5,red and 12,blue. What text does this Java code display?

Scanner in = new Scanner(new File("stalls.txt"));
int max = 0;
while (in.hasNext()) {
    String row = in.next();
    String[] parts = row.split(",");
    int n = Integer.parseInt(parts[0]);
    if (n > max) {
        max = n;
    }
}
in.close();
System.out.print(max);
  1. A. 5
  2. B. 17
  3. C. red
  4. D. 12

Question 10 of 10

A clerk writes a method that reads every word from ledger.txt. Which version both loops with hasNext and closes the file Scanner?

Version Body after Scanner in = new Scanner(new File("ledger.txt"));
W while (in.hasNext()) { System.out.print(in.next()); }
X while (in.hasNext()) { System.out.print(in.next()); } in.close();
Y System.out.print(in.next()); in.close();
Z while (in.hasNext()) { System.out.print(in.next()); } in = null;
  1. A. Version W
  2. B. Version X
  3. C. Version Y
  4. D. Version Z