Java I/O (Files, Streams, Readers/Writers) Quiz
35 in-depth questions (18 with code) covering Input/Output streams, Readers vs Writers, File operations, Buffered I/O, and handling I/O exceptions in Java.
Question 1
What is the difference between streams and readers/writers in Java I/O?
Question 2
What is the base class for all byte input streams?
Question 3
What is the base class for all character input readers?
Question 4
Why should you use buffered I/O?
Question 5
What is the result of this basic file reading example?
try (FileInputStream fis = new FileInputStream("test.txt")) {
int data = fis.read();
while (data != -1) {
System.out.print((char) data);
data = fis.read();
}
} catch (IOException e) {
e.printStackTrace();
}Question 6
What is try-with-resources?
Question 7
What is the difference between FileReader and FileInputStream?
Question 8
What is the output of this buffered reading example?
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}Question 9
Why should you close I/O streams?
Question 10
What is the result of this file copying example?
try (FileInputStream in = new FileInputStream("source.txt");
FileOutputStream out = new FileOutputStream("dest.txt")) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}Question 11
What is the difference between PrintWriter and FileWriter?
Question 12
What is the purpose of the flush() method?
Question 13
What is the output of this character encoding example?
try (FileOutputStream fos = new FileOutputStream("test.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8")) {
osw.write("Hello, 世界");
} catch (IOException e) {
e.printStackTrace();
}Question 14
What is the difference between available() and read() methods?
Question 15
What is the result of this try-with-resources example?
class CustomResource implements AutoCloseable {
public void close() { System.out.println("Closed"); }
}
try (CustomResource r = new CustomResource()) {
System.out.println("Using resource");
throw new RuntimeException("Error");
} catch (Exception e) {
System.out.println("Caught: " + e.getMessage());
}Question 16
Why should you use BufferedInputStream with FileInputStream?
Question 17
What is the difference between mark() and reset() methods?
Question 18
What is the output of this DataInputStream example?
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.bin"))) {
dos.writeInt(42);
dos.writeUTF("Hello");
}
try (DataInputStream dis = new DataInputStream(new FileInputStream("data.bin"))) {
int num = dis.readInt();
String str = dis.readUTF();
System.out.println(num + " " + str);
}Question 19
Why should you handle IOException?
Question 20
What is the difference between text files and binary files?
Question 21
What is the result of this file existence check?
File file = new File("test.txt");
if (file.exists()) {
System.out.println("File exists");
} else {
System.out.println("File does not exist");
}Question 22
What is the purpose of the skip() method?
Question 23
What is the output of this Scanner example?
try (Scanner scanner = new Scanner(new File("numbers.txt"))) {
int sum = 0;
while (scanner.hasNextInt()) {
sum += scanner.nextInt();
}
System.out.println("Sum: " + sum);
} catch (FileNotFoundException e) {
e.printStackTrace();
}Question 24
Why should you specify character encoding explicitly?
Question 25
What is the difference between RandomAccessFile and regular streams?
Question 26
What is the result of this resource leak example?
FileInputStream fis = null;
try {
fis = new FileInputStream("test.txt");
// do something
} catch (IOException e) {
e.printStackTrace();
}
// fis is not closed if exception occurs!Question 27
What is the purpose of ByteArrayInputStream?
Question 28
What is the output of this PrintWriter example?
try (PrintWriter pw = new PrintWriter("output.txt")) {
pw.println("Line 1");
pw.print("Line 2");
pw.println(" continues");
} catch (FileNotFoundException e) {
e.printStackTrace();
}Question 29
Why should you use buffered streams for network I/O?
Question 30
What is the result of this file copying with NIO?
Path source = Paths.get("source.txt");
Path dest = Paths.get("dest.txt");
try {
Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}Question 31
What is the difference between checked and unchecked I/O exceptions?
Question 32
What is the output of this StringReader example?
try (StringReader sr = new StringReader("Hello\nWorld")) {
int ch;
while ((ch = sr.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}Question 33
Why should you avoid reading large files entirely into memory?
Question 34
What is the result of this file walking example?
Path start = Paths.get(".");
try (Stream<Path> paths = Files.walk(start)) {
paths.filter(Files::isRegularFile)
.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}Question 35
Mastering Java I/O means you now write code that:
