AlgoMaster Logo

File I/O - Quiz

Last Updated: December 6, 2025

File I/O Exercises

30 quizzes

1
Code Completion

Create a File object that represents the path stored in the variable path

java
1
File file = new (path);

Click an option to fill the blank:

2
Code Completion

Open a byte stream to read from an existing file represented by the File object file

java
1
FileInputStream in = new FileInputStream();

Click an option to fill the blank:

3
Code Completion

Create a Path instance from a relative string path stored in fileName using the NIO API

java
1
Path p = Paths.(fileName);

Click an option to fill the blank:

4
Multiple Choice

When using java.io.File, which method checks if the File instance refers to a directory?

5
Multiple Choice

Which stream pair is most appropriate for copying a binary image file?

6
Multiple Choice

What is a key advantage of wrapping a FileReader in a BufferedReader?

7
Multiple Choice

Which PrintWriter method allows you to produce formatted output similar to printf in C?

8
Multiple Choice

To read integers from a text file using Scanner, which constructor is most appropriate?

9
Multiple Choice

Which interface must a class implement to allow its instances to be serialized using ObjectOutputStream?

10
Multiple Choice

Which NIO type represents a location of a file or directory in a file system?

11
Multiple Choice

Which Files method checks if a file exists at a given Path without throwing an exception?

12
Multiple Choice

Which FileChannel feature allows you to map a region of a file directly into memory for fast access?

13
Multiple Choice

Which statement about ObjectInputStream deserialization is correct?

14
Sequencing

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.

15
Sequencing

Order the steps to serialize an object to a file using ObjectOutputStream

Drag and drop to reorder, or use the arrows.

16
Output Prediction

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}
17
Output Prediction

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}
18
Output Prediction

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}
19
Output Prediction

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}
20
Output Prediction

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}
21
Bug Spotting

Find the bug related to closing resources in this file copy code

Click on the line(s) that contain the bug.

java
1
import java.io.*;
2
 
3
public class Copier {
4
    public void copy(String src, String dest) throws IOException {
5
        FileInputStream in = new FileInputStream(src);
6
        FileOutputStream out = new FileOutputStream(dest);
7
        byte[] buffer = new byte[1024];
8
        int bytesRead;
9
        while ((bytesRead = in.read(buffer)) != -1) {
10
            out.write(buffer, 0, bytesRead);
11
        }
12
    }
13
}
22
Bug Spotting

Find the bug in this deserialization code that reads a Person object

Click on the line(s) that contain the bug.

java
1
import java.io.*;
2
 
3
public class Loader {
4
    public Person load(String fileName) throws IOException, ClassNotFoundException {
5
        FileInputStream fis = new FileInputStream(fileName);
6
        ObjectInputStream ois = new ObjectInputStream(fis);
7
        Object obj = ois.readObject();
8
        ois.close();
9
        fis.close();
10
        return (Person) fis; // cast and return
11
    }
12
}
23
Matching

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.

24
Matching

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.

25
Fill in the Blanks

Complete the code to write text to a file using PrintWriter and then read it line by line with BufferedReader

java
1
import java.io.*;
2
3
public class Demo {
4
public static void main(String[] args) throws Exception {
5
try (PrintWriter pw = new PrintWriter(new FileWriter("log.txt"))) {
6
pw.println("Hello");
7
}
8
try ( br = new (new FileReader("log.txt"))) {
9
String line = br.();
10
System.out.println(line);
11
}
12
}
13
}

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the code to safely serialize an object to a file using ObjectOutputStream

java
1
import java.io.*;
2
3
public class SaveUtil {
4
public static void save(Person p) throws IOException {
5
try (FileOutputStream fos = new FileOutputStream("person.ser");
6
oos = new (fos)) {
7
oos.(p);
8
}
9
}
10
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to create a Path and check if it exists using the NIO Files API

java
1
import java.nio.file.*;
2
3
public class CheckPath {
4
public static void main(String[] args) throws Exception {
5
Path path = Paths.("data.txt");
6
boolean exists = Files.(path);
7
System.out.println();
8
}
9
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to read an int from the console using Scanner and print it

java
1
import java.util.*;
2
3
public class ReadNumber {
4
public static void main(String[] args) {
5
Scanner sc = new Scanner(System.in);
6
int value = sc.();
7
System.out.println();
8
sc.close();
9
}
10
}

Click an option to fill blank 1:

29
Hotspot Selection

Click the line that may cause an IOException when working with a FileInputStream

Click on the line to select.

java
1
import java.io.*;
2
 
3
public class ReaderDemo {
4
    public static void main(String[] args) throws Exception {
5
        FileInputStream fis = new FileInputStream("missing.txt");
6
        int data = fis.read();
7
        System.out.println(data);
8
        fis.close();
9
    }
10
}
30
Hotspot Selection

Click the line where a NullPointerException would occur when using a File object

Click on the line to select.

java
1
import java.io.*;
2
 
3
public class FileNullDemo {
4
    public static void main(String[] args) {
5
        File f = null;
6
        System.out.println(f.getAbsolutePath());
7
    }
8
}

Premium Content

Subscribe to unlock full access to this content and more premium articles.