Last Updated: December 6, 2025
30 quizzes
Create a File object that represents the path stored in the variable path
File file = new (path);Click an option to fill the blank:
Open a byte stream to read from an existing file represented by the File object file
FileInputStream in = new FileInputStream();Click an option to fill the blank:
Create a Path instance from a relative string path stored in fileName using the NIO API
Path p = Paths.(fileName);Click an option to fill the blank:
When using java.io.File, which method checks if the File instance refers to a directory?
Which stream pair is most appropriate for copying a binary image file?
What is a key advantage of wrapping a FileReader in a BufferedReader?
Which PrintWriter method allows you to produce formatted output similar to printf in C?
To read integers from a text file using Scanner, which constructor is most appropriate?
Which interface must a class implement to allow its instances to be serialized using ObjectOutputStream?
Which NIO type represents a location of a file or directory in a file system?
Which Files method checks if a file exists at a given Path without throwing an exception?
Which FileChannel feature allows you to map a region of a file directly into memory for fast access?
Which statement about ObjectInputStream deserialization is correct?
Order the steps to read all lines from a text file using BufferedReader and print them
Drag and drop to reorder, or use the arrows.
Order the steps to serialize an object to a file using ObjectOutputStream
Drag and drop to reorder, or use the arrows.
What is the output of this code related to the File class?
1import java.io.File;
2
3public class Test {
4 public static void main(String[] args) {
5 File f = new File("notes.txt");
6 System.out.println(f.getName());
7 }
8}What is the output when reading characters with FileReader?
1import java.io.*;
2
3public class Test {
4 public static void main(String[] args) throws Exception {
5 char[] data = {'J', 'a', 'v', 'a'};
6 System.out.println(data[1]);
7 }
8}What does this Scanner-based code print?
1import java.util.*;
2
3public class Test {
4 public static void main(String[] args) {
5 String text = "42 true";
6 Scanner sc = new Scanner(text);
7 int n = sc.nextInt();
8 boolean b = sc.nextBoolean();
9 System.out.println(n + "-" + b);
10 sc.close();
11 }
12}What is the output when using Paths and Files?
1import java.nio.file.*;
2import java.io.*;
3
4public class Test {
5 public static void main(String[] args) throws Exception {
6 Path p = Paths.get("example.txt");
7 System.out.println(p.getFileName());
8 }
9}What is the output when positioning a ByteBuffer?
1import java.nio.*;
2
3public class Test {
4 public static void main(String[] args) {
5 ByteBuffer buffer = ByteBuffer.allocate(4);
6 buffer.put((byte)10);
7 buffer.put((byte)20);
8 buffer.flip();
9 buffer.get();
10 System.out.println(buffer.get());
11 }
12}Find the bug related to closing resources in this file copy code
Click on the line(s) that contain the bug.
import java.io.*; public class Copier { public void copy(String src, String dest) throws IOException { FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } }}Find the bug in this deserialization code that reads a Person object
Click on the line(s) that contain the bug.
import java.io.*; public class Loader { public Person load(String fileName) throws IOException, ClassNotFoundException { FileInputStream fis = new FileInputStream(fileName); ObjectInputStream ois = new ObjectInputStream(fis); Object obj = ois.readObject(); ois.close(); fis.close(); return (Person) fis; // cast and return }}Match each I/O class with what it is best suited for
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match NIO concepts with their primary purpose
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the code to write text to a file using PrintWriter and then read it line by line with BufferedReader
import java.io.*;public class Demo { public static void main(String[] args) throws Exception { try (PrintWriter pw = new PrintWriter(new FileWriter("log.txt"))) { pw.println("Hello"); } try ( br = new (new FileReader("log.txt"))) { String line = br.(); System.out.println(line); } }}Click an option to fill blank 1:
Complete the code to safely serialize an object to a file using ObjectOutputStream
import java.io.*;public class SaveUtil { public static void save(Person p) throws IOException { try (FileOutputStream fos = new FileOutputStream("person.ser"); oos = new (fos)) { oos.(p); } }}Click an option to fill blank 1:
Complete the code to create a Path and check if it exists using the NIO Files API
import java.nio.file.*;public class CheckPath { public static void main(String[] args) throws Exception { Path path = Paths.("data.txt"); boolean exists = Files.(path); System.out.println(); }}Click an option to fill blank 1:
Complete the code to read an int from the console using Scanner and print it
import java.util.*;public class ReadNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int value = sc.(); System.out.println(); sc.close(); }}Click an option to fill blank 1:
Click the line that may cause an IOException when working with a FileInputStream
Click on the line to select.
import java.io.*; public class ReaderDemo { public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream("missing.txt"); int data = fis.read(); System.out.println(data); fis.close(); }}Click the line where a NullPointerException would occur when using a File object
Click on the line to select.
import java.io.*; public class FileNullDemo { public static void main(String[] args) { File f = null; System.out.println(f.getAbsolutePath()); }}