SaveMyCert
Log in
5 of 5 free questions left today·for 30 a day
1Z0-831 · Domain 9

Performing Input and Output Operations Using the Java I/O API practice questions

Performing Input and Output Operations Using the Java I/O API is worth 11% of the 1Z0-831 exam — the 2nd-heaviest of the 10 domains. Console and file I/O streams, serialization, and Path objects with the java.nio.file API. Oracle publishes no domain weights; weight approximates the objective share, with a floor for every section. 6 fully worked examples are further down this page, answers included.

Exam weight
11%
the 2nd-heaviest of the 10 domains
Questions
105
across 3 topics
Free, no account
5/day
a free account allows 30 a day
Explanations
Every option
right and wrong

Build a practice session

5 free questions left today.

Domains

How many?

Mode

Ready when you are

10 fresh questions drawn across 1 of 10 domains, in Learn mode.

Focused review

Every question you answer incorrectly, and every question you flag while practising, is saved here automatically. Finish a session and you can come back to re-drill just those.

6 sample Performing Input and Output Operations Using the Java I/O API questions, fully explained

Questions from the 1Z0-831 bank mapped to domain 9, with the answer key and the reasoning behind every option. None of them repeat the examples on the main 1Z0-831 practice page.

Question 1Performing Input and Output Operations Using the Java I/O API

Given:import java.io.*; public class Main { public static void main(String[] args) throws IOException { byte[] data = { 65, -1, 66 }; InputStream in = new ByteArrayInputStream(data); int b; while ((b = in.read()) != -1) { System.out.print(b + " "); } } }What is the result?

Choose one.

  • a
    65

    This assumes the stored byte -1 is returned as -1 and ends the loop. read() reserves -1 for end of stream and returns the byte as 255.

  • b
    65 -1 66

    read() never returns a negative value for a data byte; the int it returns is in the range 0 to 255, so -1 is not printed.

  • c
    65 255 66 -1

    The loop test checks the value before printing it, so the -1 that signals end of stream ends the loop and is never printed.

  • d
    65 255 66 Correct

    read() returns each byte as an unsigned int from 0 to 255. The byte -1 (all bits set) comes back as 255, so the loop does not stop there and also prints 66.

The concept

InputStream.read() returns the next byte as an int in the range 0 to 255, or -1 at end of stream.

Why that’s the answer

The single-byte read() widens each byte to an unsigned value. That is exactly why it returns int and not byte: the extra range leaves -1 free to mean end of stream. The stored byte -1 (0xFF) is returned as 255, which is not equal to -1, so the loop continues and reads 66 as well. Only after the third byte does read() return -1. A candidate who thinks read() returns the signed byte value stops after 65 or prints -1.

How to reason it out
  1. read() returns int, not byte.
  2. Data bytes are returned as 0 to 255; -1 means end of stream only.
  3. The byte -1 is therefore returned as 255 and the loop goes on.
  4. Each value is printed as a number followed by a space.

Exam tip: read() returns 0-255 for data and -1 only at end of stream; never store its result in a byte before comparing with -1.

Java I/O Streams: Console and File Readers, Writers and Streams (1Z0-831) — the lesson that teaches this.

Question 2Performing Input and Output Operations Using the Java I/O API

Given:import java.io.*; public class Main { public static void main(String[] args) throws IOException { InputStream in = new ByteArrayInputStream("abcdefg".getBytes()); byte[] buf = new byte[3]; StringBuilder sb = new StringBuilder(); while (in.read(buf) != -1) { sb.append(new String(buf)); } System.out.println(sb); } }What is the result?

Choose one.

  • a
    abcdefg

    This would need new String(buf, 0, n) with the count returned by read. The code ignores the count and converts the whole array each time.

  • b
    abcdefgef Correct

    The last read(buf) fills only buf[0] with g and returns 1. buf[1] and buf[2] still hold e and f from the previous read, and new String(buf) uses all three bytes.

  • c
    abcdef

    The third call still reads one byte, g, and returns 1, not -1. Only the fourth call returns -1.

  • d
    abcdefgfg

    read(buf) writes the new byte at index 0, so the stale bytes left over are e and f, not f and g.

The concept

read(byte[]) returns how many bytes it stored; the rest of the array keeps its old contents.

Why that’s the answer

read(byte[] b) reads up to b.length bytes into the start of the array and returns the number read, or -1 at end of stream. The first two calls fill the array with abc and def. The third finds one byte left: it writes g into buf[0] and returns 1, leaving e and f in buf[1] and buf[2]. Because the loop ignores the count and builds new String(buf), it appends gef. The fourth call returns -1. The correct pattern is new String(buf, 0, n).

How to reason it out
  1. Track the buffer: abc, then def.
  2. The third read stores only g at index 0 and returns 1.
  3. Indexes 1 and 2 still hold e and f.
  4. new String(buf) uses all three bytes, so gef is appended.

Exam tip: Always use the count returned by read(buf): new String(buf, 0, n) or write(buf, 0, n).

Java I/O Streams: Console and File Readers, Writers and Streams (1Z0-831) — the lesson that teaches this.

Question 3Performing Input and Output Operations Using the Java I/O API

Given:import java.io.*; public class Main { public static void main(String[] args) throws IOException { String text = "one\ntwo\n\nthree\n"; int count = 0; try (var br = new BufferedReader(new StringReader(text))) { while (br.readLine() != null) { count++; } } System.out.println(count); } }What is the result?

Choose one.

  • a
    3

    The empty line between two and three is a line of its own; readLine() returns an empty String for it, which is not null.

  • b
    5

    This counts an extra empty line after the final \n. readLine() returns null once the stream ends right after a terminator.

  • c
    4 Correct

    readLine() returns one, two, an empty String and three. A final line terminator does not start a fifth line, so the next call returns null.

  • d
    2

    This stops at the empty line. readLine() returns "" for it, which is not null, so the loop keeps counting.

The concept

BufferedReader.readLine() returns a line without its terminator, an empty String for an empty line, and null at end of stream.

Why that’s the answer

readLine() reads up to a line terminator (\n, \r or \r\n) and returns the characters before it. Two terminators in a row produce an empty String, which still counts as a line because only null ends the loop. When the stream ends immediately after a terminator, the next call returns null instead of an empty line, so the trailing \n does not add one. The four lines are one, two, "" and three.

How to reason it out
  1. readLine() returns null only at end of stream.
  2. An empty line comes back as "", which is counted.
  3. A trailing terminator ends the last line; it does not create another.
  4. So the lines are one, two, "", three.

Exam tip: readLine(): text without the terminator, "" for a blank line, null at end of stream.

Java I/O Streams: Console and File Readers, Writers and Streams (1Z0-831) — the lesson that teaches this.

Question 4Performing Input and Output Operations Using the Java I/O API

Given:import java.io.*; public class Main { public static void main(String[] args) throws IOException { try (var w = new FileWriter("note.txt")) { w.write("A"); } try (var w = new FileWriter("note.txt")) { w.write("B"); } try (var r = new BufferedReader( new FileReader("note.txt"))) { System.out.println(r.readLine()); } } }What is the result?

Choose one.

  • a
    AB

    AB would need the second writer to append, for example new FileWriter("note.txt", true). The one-argument constructor replaces the file's contents.

  • b
    BA

    Writers add at the end of the file or replace it; nothing writes before existing content.

  • c
    A

    Opening the file again does not leave the old text in place: the second writer truncates it and writes B.

  • d
    B Correct

    new FileWriter("note.txt") opens the file for writing from the start and truncates it, so the second writer replaces A with B.

The concept

FileWriter and FileOutputStream overwrite an existing file unless they are created in append mode.

Why that’s the answer

Both FileWriter(String) and FileOutputStream(String) create the file if needed and truncate it if it exists. The first block writes A and closes the file. The second block opens it again with the same one-argument constructor, which empties it, and writes B. Reading the first line gives B. Passing true as the second argument (append) would have produced AB.

How to reason it out
  1. The first writer creates note.txt containing A.
  2. The second writer uses the constructor without an append flag.
  3. That constructor truncates the file before writing B.
  4. readLine() returns B.

Exam tip: new FileWriter(name) overwrites; new FileWriter(name, true) appends.

Java I/O Streams: Console and File Readers, Writers and Streams (1Z0-831) — the lesson that teaches this.

Question 5Performing Input and Output Operations Using the Java I/O API

Given:import java.io.*; public class Main { public static void main(String[] args) throws IOException { try (Writer w = new FileWriter("log.txt")) { w.write("1"); } try (Writer w = ________) { w.write("2"); } try (var r = new BufferedReader( new FileReader("log.txt"))) { System.out.println(r.readLine()); } } }Which two expressions, used in place of the blank, make the program print 12? (Select TWO.)

Choose TWO.

  • a
    new FileWriter("log.txt", true) Correct

    The boolean argument opens the file in append mode, so 2 is added after the existing 1.

  • b
    new PrintWriter("log.txt")

    PrintWriter(String fileName) has no append option: it truncates the file, so the program prints 2.

  • c
    new BufferedWriter(new FileWriter("log.txt"))

    Buffering does not change how the file is opened. The inner FileWriter truncates the file, so the program prints 2.

  • d
    new PrintWriter(new FileWriter("log.txt", true)) Correct

    The PrintWriter wraps a FileWriter opened in append mode. Closing the PrintWriter flushes 2 to the end of the file.

  • e
    new PrintWriter(new FileWriter("log.txt"), true)

    The true here is the PrintWriter autoflush flag, not append. The inner one-argument FileWriter truncates the file, so the program prints 2.

The concept

Append versus truncate is decided by the stream that opens the file, not by the wrappers around it.

Why that’s the answer

Only the class that actually opens the file chooses the mode. FileWriter(String, boolean) and FileOutputStream(String, boolean) append when the flag is true. Wrappers such as PrintWriter and BufferedWriter inherit whatever the inner stream did. PrintWriter(String fileName) opens the file itself and always truncates it, and a BufferedWriter or PrintWriter over a one-argument FileWriter also truncates, even when the PrintWriter is given true, because that flag means autoflush. Running each option gives 12, 12, 2, 2 and 2.

How to reason it out
  1. Find the innermost stream in each option: it opens the file.
  2. Check whether that inner stream was given the append flag true (a PrintWriter true is autoflush).
  3. Wrappers keep the mode chosen by the inner stream.
  4. PrintWriter(String) opens the file itself, without appending.

Exam tip: Append mode comes from the constructor that opens the file: FileWriter(name, true) or FileOutputStream(name, true).

Java I/O Streams: Console and File Readers, Writers and Streams (1Z0-831) — the lesson that teaches this.

Question 6Performing Input and Output Operations Using the Java I/O API

Given:import java.io.*; public class Main { public static void main(String[] args) throws IOException { var w = new BufferedWriter(new FileWriter("out.txt")); w.write("data"); try (var r = new BufferedReader( new FileReader("out.txt"))) { System.out.println("[" + r.readLine() + "]"); } w.close(); } }What is the result?

Choose one.

  • a
    [null] Correct

    The file was created and truncated but nothing was flushed to it yet. Reading an empty file, readLine() returns null, and string concatenation prints null.

  • b
    [data]

    write("data") only puts the characters into the BufferedWriter's buffer. They reach the file on flush() or close(), which happen after the read.

  • c
    []

    An empty file has no lines at all, so readLine() returns null rather than an empty String.

  • d
    A FileNotFoundException is thrown

    Creating the FileWriter already created out.txt on disk, so the FileReader can open it.

The concept

Buffered writers hold data in memory until flush() or close().

Why that’s the answer

Constructing the FileWriter creates (or truncates) out.txt, so the file exists. BufferedWriter.write stores the four characters in its buffer and does not pass them on yet. The file is still empty when it is read, so readLine() returns null at once, and "[" + null + "]" prints [null]. Only the later w.close() flushes data to the file. Calling w.flush() before reading would have printed [data].

How to reason it out
  1. The FileWriter constructor creates an empty out.txt.
  2. write() fills the BufferedWriter's buffer, not the file.
  3. Reading the still-empty file: readLine() returns null.
  4. Concatenation turns null into the text null.

Exam tip: Written is not saved: call flush() or close the writer before anything else reads the file.

Java I/O Streams: Console and File Readers, Writers and Streams (1Z0-831) — the lesson that teaches this.

What 1Z0-831 domain 9 tests, topic by topic

The official exam guide breaks Performing Input and Output Operations Using the Java I/O API into 3 topics. The question bank follows the same split, so a weak topic shows up as a cluster of misses you can go back and read.

Published 1Z0-831 practice questions per topic in Performing Input and Output Operations Using the Java I/O API
TopicWhat it coversQuestions
Console and file I/O streamsOfficial 1Z0-831 objective 9.1: "Read and write console and file data using I/O streams."35
Serialization and deserializationOfficial 1Z0-831 objective 9.2: "Serialize and de-serialize Java objects."35
Path objects and the java.nio.file APIOfficial 1Z0-831 objective 9.3: "Construct, traverse, create, read, and write Path objects and their properties using the java.nio.file API."35
Total105

Revise Performing Input and Output Operations Using the Java I/O API before you drill it

Other 1Z0-831 domains

Performing Input and Output Operations Using the Java I/O API: your questions

Performing Input and Output Operations Using the Java I/O API is domain 9 of the 1Z0-831 exam guide and carries 11% of the scored content — the 2nd-heaviest of the 10 domains. On a 50-question paper that works out to roughly 6 questions, though Oracle does not publish an exact per-domain count and individual exam forms vary.

Source

The domain weight and topic list on this page come from the official 1Z0-831 exam guide.