Using Text Files MCQ Practice

Question 1 of 10

Marshwick Keep ran a program yesterday that created the file fog-log.txt. A new program starts today and does not create that file. Which statement is true?

  1. A. The file is gone because the first program ended.
  2. B. The file remains on disk after the first program ended.
  3. C. The file exists only while a Scanner is open.
  4. D. The file is stored only in RAM for one run.

Question 2 of 10

Puddlewick Station connects a Scanner to rain totals stored in a file. Which constructor pair matches Topic 4.6?

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
  1. A. new Scanner(System.in) then new File("rain.txt")
  2. B. new File() with no pathname, then new Scanner(that File)
  3. C. new Scanner(new File("rain.txt"))
  4. D. new File("rain.txt") without a Scanner, then nextInt on the File

Question 3 of 10

Cinderwick Kiln stores three integer firings in kiln.txt. The table is the file. What text does this Java code display?

File line Contents
1 12
2 8
3 15
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class KilnSum {
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(new File("kiln.txt"));
        int sum = 0;
        while (in.hasNext()) {
            sum += in.nextInt();
        }
        in.close();
        System.out.print(sum);
    }
}
  1. A. 3
  2. B. 12
  3. C. 35
  4. D. 1215

Question 4 of 10

Bramblewick Ranger tries to open trail-notes.txt. The file name is wrong, so the File cannot be found. The method header is public static void readNotes() throws IOException. What happens when new Scanner(new File("trail-notes.txt")) runs?

  1. A. The Scanner stores null and later nextInt returns 0.
  2. B. Java skips the missing file and the method returns normally.
  3. C. An IOException occurs and, because it is not caught, the program terminates.
  4. D. The program creates an empty trail-notes.txt and continues.

Question 5 of 10

Nettlewick Lab compiles a class that constructs File and Scanner for hive-weights.txt. Which import set is required for File and IOException?

  1. A. import java.io.File; and import java.io.IOException;
  2. B. import java.util.File; and import java.util.IOException;
  3. C. No import is needed if Scanner is imported.
  4. D. import java.lang.File; only

Question 6 of 10

Driftwick Buoy reads only nextDouble values from swell.txt. What text does this Java code display?

Scanner in = new Scanner(new File("swell.txt"));
double sum = 0.0;
while (in.hasNext()) {
    sum += in.nextDouble();
}
in.close();
System.out.print(sum);

swell.txt contains the tokens 3.5 then 2.0 then 4.5.

  1. A. 3.5
  2. B. 10.0
  3. C. 9
  4. D. 2.0

Question 7 of 10

Willowwick Dock reads boolean flags from open.txt with nextBoolean. The file tokens are true, false, true. What text does this Java code display?

Scanner in = new Scanner(new File("open.txt"));
int yes = 0;
while (in.hasNext()) {
    if (in.nextBoolean()) {
        yes++;
    }
}
in.close();
System.out.print(yes);
  1. A. 3
  2. B. 1
  3. C. truefalsetrue
  4. D. 2

Question 8 of 10

Copperwick Fares stores each tram row as one token with commas and no spaces, such as 12,3. What text does this Java code display?

Scanner in = new Scanner(new File("fares.txt"));
int total = 0;
while (in.hasNext()) {
    String row = in.next();
    String[] parts = row.split(",");
    total += Integer.parseInt(parts[0]);
}
in.close();
System.out.print(total);

fares.txt contains the tokens 12,3 and 8,1.

  1. A. 15
  2. B. 20
  3. C. 4
  4. D. 12,38,1

Question 9 of 10

Mosswick Spore log uses a space delimiter inside one String already stored in memory. What text does this Java code display?

String row = "9 4 6";
String[] parts = row.split(" ");
int sum = 0;
for (int i = 0; i < parts.length; i++) {
    sum += Integer.parseInt(parts[i]);
}
System.out.print(sum);
System.out.print(":");
System.out.print(parts.length);
  1. A. 946:1
  2. B. 19:3
  3. C. 18:3
  4. D. 9:3

Question 10 of 10

Amberwick Records reads one full line from resin.txt using only nextLine, then finishes. Which statement about close is correct?

Scanner in = new Scanner(new File("resin.txt"));
String label = in.nextLine();
in.close();
System.out.print(label);
  1. A. close is optional because the file is deleted when main ends.
  2. B. close must be called when the program is done using the Scanner connected to the file.
  3. C. close writes new numbers into resin.txt.
  4. D. close is only used with System.in.