Java Topic Practice
Text Files and Scanner
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
For practice use only.
Text Files and Scanner MCQ Practice
Text Files and Scanner Java practice with 15 questions, code tracing, and a worked explanation for every answer.
Answered 0 of 15
Choose one answer.
What is printed by the following code segment?
Scanner s = new Scanner("12 7 3");
int t = 0;
while (s.hasNext())
{
t += s.nextInt();
}
System.out.print(t);All 15 text files and scanner questions
Work through the interactive quiz above first. This is the full text files and scanner question set with worked solutions, so students can review any question after attempting it.
1.What is printed by the following code segment?
Scanner s = new Scanner("12 7 3"); int t = 0; while (s.hasNext()) { t += s.nextInt(); } System.out.print(t);- 22
- 12
- 0
- 123
Show worked solution
Correct answer: 22
hasNext reports whether another token remains, and nextInt reads it as a number. Testing before every read is what stops the loop cleanly at the end of the input.
2.What is printed by the following code segment?
Scanner s = new Scanner("one two three"); int n = 0; while (s.hasNext()) { s.next(); n++; } System.out.print(n);- 1
- 3
- 0
- 11
Show worked solution
Correct answer: 3
Tokens are separated by whitespace, and next consumes exactly one per pass.
3.What is printed by the following code segment?
Scanner s = new Scanner(" spaced out "); int n = 0; while (s.hasNext()) { s.next(); n++; } System.out.print(n);- 4
- 3
- 2
- 6
Show worked solution
Correct answer: 2
Runs of whitespace collapse into a single separator, and leading or trailing spaces never produce an empty token.
4.What is printed by the following code segment?
Scanner s = new Scanner("Ada Lovelace\nGrace Hopper"); String a = s.nextLine(); String b = s.nextLine(); System.out.print(a.length() + " " + b.length());- 13 13
- 3 5
- 12 13
- 12 12
Show worked solution
Correct answer: 12 12
Each nextLine returns one whole line including its internal space, and the line terminator itself is consumed but not returned.
5.What is printed by the following code segment?
Scanner s = new Scanner("3\n10 20 30"); int n = s.nextInt(); int t = 0; for (int i = 0; i < n; i++) { t += s.nextInt(); } System.out.print(n + " " + t);- 3 60
- 3 10
- 60 60
- 3 0
Show worked solution
Correct answer: 3 60
A leading count tells the loop how many values follow. Whitespace and line breaks separate tokens identically, so the three values on one line read the same as three on separate lines.
6.What is printed by the following code segment?
String line = "a,b,c"; String[] parts = line.split(","); String out = ""; for (String x : parts) { out += x + "-"; } System.out.print(out);- a,b,c-
- a-b-c-
- a-b-c
- abc-
Show worked solution
Correct answer: a-b-c-
split is the Quick Reference method for breaking a line on a separator, and it returns an array of the pieces without the separator.
7.Which statement about the code below is true?
Scanner in = new Scanner(new File("data.txt")); while (in.hasNext()) { System.out.println(in.nextLine()); }- The code compiles as written because Scanner handles a missing file itself.
- The loop skips the last line of the file.
- The method containing this code must indicate what happens if the file cannot be opened, for example with throws IOException.
- A File object cannot be passed to a Scanner.
Show worked solution
Correct answer: The method containing this code must indicate what happens if the file cannot be opened, for example with throws IOException.
Using a File requires the enclosing method to say what happens when the file cannot be opened. Adding throws IOException to the header is the form the course framework uses, and File and IOException both come from java.io.
8.What is printed by the following code segment?
Scanner s = new Scanner("7.5 8"); double a = s.nextDouble(); double b = s.nextInt(); System.out.print(a + b);- 15
- 7.58
- 15.0
- 15.5
Show worked solution
Correct answer: 15.5
Reading an int and assigning it to a double widens the value, and the sum prints in double form.
9.What is printed by the following code segment?
Scanner s = new Scanner("one two three"); int words = 0; int chars = 0; while (s.hasNext()) { String w = s.next(); words++; chars += w.length(); } System.out.print(words + " " + chars);- 3 11
- 3 13
- 11 3
- 1 13
Show worked solution
Correct answer: 3 11
The separators are consumed but never returned, so only the letters are counted.
10.What is printed by the following code segment?
Scanner s = new Scanner("4 8 15 16"); int max = Integer.MIN_VALUE; int c = 0; while (s.hasNext()) { int v = s.nextInt(); c++; if (v > max) { max = v; } } System.out.print(c + " " + max);- 4 4
- 4 16
- 16 4
- 3 16
Show worked solution
Correct answer: 4 16
Seeding with the smallest possible int guarantees the first value replaces it.
11.A file holds one integer per line and may be empty. Which loop is safe?
- for (int i = 0; i < 10; i++) { total += in.nextInt(); }
- while (true) { total += in.nextInt(); }
- while (in.hasNext()) { total += in.nextInt(); }
- total += in.nextInt();
Show worked solution
Correct answer: while (in.hasNext()) { total += in.nextInt(); }
Testing before every read is the only form that handles both an empty file and a file whose length is unknown.
12.What is printed by the following code segment?
Scanner s = new Scanner("Bob 88 Amy 93"); String best = ""; int bestScore = -1; while (s.hasNext()) { String name = s.next(); int score = s.nextInt(); if (score > bestScore) { bestScore = score; best = name; } } System.out.print(best + " " + bestScore);- Bob 88
- Amy 88
- Bob 93
- Amy 93
Show worked solution
Correct answer: Amy 93
Alternating a token read with a number read walks one record at a time.
13.What is printed by the following code segment?
Scanner s = new Scanner("10 5 0 7"); int t = 0; boolean stop = false; while (s.hasNext() && !stop) { int v = s.nextInt(); if (v == 0) { stop = true; } else { t += v; } } System.out.print(t + " " + s.hasNext());- 15 true
- 22 false
- 15 false
- 22 true
Show worked solution
Correct answer: 15 true
A flag in the loop condition ends the scan at the sentinel, and the value after it is still waiting unread.
14.What is printed by the following code segment?
Scanner s = new Scanner("dog cat"); String a = s.next(); String b = s.next(); System.out.print(a.compareTo(b) > 0);- false
- true
- dog
- cat
Show worked solution
Correct answer: true
compareTo orders strings alphabetically and returns a positive value when the calling string comes later, and d comes after c.
15.What is printed by the following code segment?
Scanner s = new Scanner("5 3"); int a = s.nextInt(); int b = s.nextInt(); System.out.print(a / b + " " + a % b);- 1.67 2
- 2 1
- 1 2
- 1 1
Show worked solution
Correct answer: 1 2
Both tokens are read as ints, so the division truncates and the remainder is what is left over.
