Java I/O & NIO
Test your understanding of Java file I/O, streams, readers/writers, NIO.2 Path API, serialisation, and modern file operations.
Questions in this quiz
20 questions on Java I/O & NIO. Work through them here, then start the quiz to answer them and see your score.
1.What is the difference between byte streams and character streams in Java I/O?
- AByte streams handle raw binary data; character streams handle text with encoding conversion
- BCharacter streams work only with UTF-8; byte streams handle all encodings
- CByte streams are faster; character streams are more accurate
- DThey are identical — just different naming conventions
2.What class should you use to efficiently read a text file line by line in Java?
- ABufferedReader wrapping a FileReader
- BFileInputStream
- CDataInputStream
- DScanner only
3.What does this code do?
import java.io.*; public class Main { public static void main(String[] args) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } } }- AWrites lines to data.txt
- BThrows an exception because FileReader is deprecated
- CCounts the number of lines in data.txt
- DReads and prints every line of data.txt, closing the reader automatically
4.What is the NIO.2 Path API introduced in Java 7?
- AA new I/O library for network streams
- BA faster replacement for sockets
- CA binary serialisation format
- DA modern file system API in java.nio.file with Path, Paths, and Files replacing the legacy File class
5.What does this code do?
import java.nio.file.*; import java.io.IOException; import java.util.List; public class Main { public static void main(String[] args) throws IOException { Path path = Paths.get("notes.txt"); List<String> lines = Files.readAllLines(path); System.out.println(lines.size()); } }- ACounts bytes in notes.txt
- BReads all lines of notes.txt into a List and prints the line count
- CWrites an empty list to notes.txt
- DThrows IOException always
6.What is serialisation in Java?
- AConverting a Java object to a sorted sequence
- BConverting strings to integers
- CConverting an object's state to a byte stream for persistence or transmission, and deserialising it back
- DOrganising class fields in memory order
7.What is the purpose of the transient keyword?
- AMakes a field temporary and garbage-collected sooner
- BMarks a field as thread-safe
- CPrevents a field from being accessed by subclasses
- DExcludes a field from serialisation
8.What does this code print?
import java.nio.file.*; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { Path path = Paths.get("test.txt"); Files.writeString(path, "Hello NIO"); String content = Files.readString(path); System.out.println(content); Files.delete(path); } }- ACompilation error — writeString does not exist
- BHello NIO\n
- CHello NIO
- Dnull
9.What is the difference between Files.copy() and Files.move()?
- Amove() copies then deletes; copy() only copies
- Bcopy() is for text; move() is for binary
- CThey are identical except move() is faster
- Dcopy() duplicates the file leaving the source intact; move() relocates the file (or renames it) removing the source
10.How do you walk a directory tree in Java NIO.2?
- ADirectoryStream.walk(path)
- BPath.walkTree()
- Cnew File(dir).list() recursively
- DFiles.walk(path) returns a lazy Stream<Path> of all files and directories in the tree
11.What does this code print?
import java.nio.file.*; public class Main { public static void main(String[] args) { Path p = Paths.get("/home/user/docs/report.pdf"); System.out.println(p.getFileName()); System.out.println(p.getParent()); System.out.println(p.getNameCount()); } }- A/home/user/docs/report.pdf /home/user 4
- Breport.pdf /home/user/docs 5
- Creport /home/user/docs 5
- Dreport.pdf /home/user/docs 4
12.What is a ByteBuffer in Java NIO?
- AA resizable byte array
- BA fixed-capacity buffer for reading and writing bytes used with Channels for non-blocking I/O
- CA byte array serialiser
- DA wrapper around OutputStream for buffering
13.What is the difference between absolute and relative paths in Java?
- AJava only supports absolute paths
- BRelative paths are faster to access
- CAbsolute paths start from the root of the filesystem; relative paths are resolved from the current working directory
- DAbsolute paths are shorter; relative paths are longer
14.What does this code print?
import java.nio.file.*; public class Main { public static void main(String[] args) { Path base = Paths.get("/projects/app"); Path resolved = base.resolve("src/Main.java"); Path normalised = Paths.get("/projects/app/../app/src/Main.java").normalize(); System.out.println(resolved); System.out.println(normalised); } }- A/projects/app/src/Main.java /projects/app/src/Main.java
- BCompilation error
- Csrc/Main.java /projects/app/src/Main.java
- D/projects/app/src/Main.java /projects/app/../app/src/Main.java
15.When should you use PrintWriter over FileWriter?
- APrintWriter is for binary output; FileWriter is for text
- BFileWriter is deprecated in favour of PrintWriter
- CPrintWriter is always better
- DPrintWriter adds println(), printf(), and format() convenience methods and can auto-flush, making it better for formatted text output
16.What does Files.lines() return and what is its advantage over Files.readAllLines()?
- AFiles.lines() returns a lazy Stream<String> — lines are read on demand; Files.readAllLines() loads the entire file into memory
- BThey are identical
- CFiles.lines() returns a List; Files.readAllLines() returns a Stream
- DFiles.lines() reads bytes; Files.readAllLines() reads characters
17.What does this code print?
import java.nio.file.*; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { Path dir = Paths.get("mydir"); Files.createDirectories(dir.resolve("sub1/sub2")); System.out.println(Files.exists(dir)); System.out.println(Files.isDirectory(dir)); } }- Atrue true
- Bfalse false
- Ctrue false
- DCompilation error
18.What is the serialVersionUID field used for in serialisation?
- AA version identifier that ensures the serialised class matches the deserialised class — mismatches throw InvalidClassException
- BA unique identifier assigned by the JVM at runtime
- CA field that controls the order of serialised fields
- DA counter that increments each time the object is serialised
19.What is the best modern practice for reading a small text file's full content in Java?
- Anew FileInputStream(file) with a byte array
- BFiles.readString(path) (Java 11+) or Files.readAllLines(path)
- Cnew Scanner(new File(path))
- Dnew BufferedReader(new FileReader(file)) with a StringBuilder
20.What does this code print?
import java.nio.file.*; public class Main { public static void main(String[] args) { Path p1 = Paths.get("a/b/../c"); Path p2 = p1.normalize(); System.out.println(p1); System.out.println(p2); System.out.println(p1.equals(p2)); } }- ACompilation error
- Ba/b/../c a/c true
- Ca/b/../c a/c false
- Da/c a/c true