Java I/O (Files, Streams, Readers/Writers) Quiz

Java
0 Passed
0% acceptance

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.

35 Questions
~70 minutes
1

Question 1

What is the difference between streams and readers/writers in Java I/O?

A
Streams work with bytes, readers/writers work with characters and handle encoding
B
Streams are faster
C
Readers/writers are deprecated
D
They are identical
2

Question 2

What is the base class for all byte input streams?

A
InputStream
B
Reader
C
FileInputStream
D
ByteArrayInputStream
3

Question 3

What is the base class for all character input readers?

A
Reader
B
InputStream
C
FileReader
D
StringReader
4

Question 4

Why should you use buffered I/O?

A
To reduce the number of expensive I/O operations by reading/writing in chunks
B
To compress data
C
To encrypt data
D
To validate data
5

Question 5

What is the result of this basic file reading example?

java
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();
}
A
Reads and prints the file content byte by byte
B
Compilation error
C
Runtime exception
D
No output
6

Question 6

What is try-with-resources?

A
A syntax that automatically closes resources, added in Java 7
B
A way to try multiple operations
C
A replacement for finally blocks
D
A way to suppress exceptions
7

Question 7

What is the difference between FileReader and FileInputStream?

A
FileReader handles character encoding, FileInputStream works with raw bytes
B
FileReader is faster
C
FileInputStream is deprecated
D
They are identical
8

Question 8

What is the output of this buffered reading example?

java
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();
}
A
Reads and prints the file line by line
B
Compilation error
C
Runtime exception
D
No output
9

Question 9

Why should you close I/O streams?

A
To free system resources and prevent resource leaks
B
To save memory
C
To improve performance
D
To validate data
10

Question 10

What is the result of this file copying example?

java
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();
}
A
Copies the file using a 1KB buffer
B
Compilation error
C
Runtime exception
D
No output
11

Question 11

What is the difference between PrintWriter and FileWriter?

A
PrintWriter has convenience methods like println(), FileWriter is basic
B
PrintWriter is faster
C
FileWriter handles formatting
D
They are identical
12

Question 12

What is the purpose of the flush() method?

A
To force buffered data to be written immediately
B
To clear the buffer
C
To close the stream
D
To read data
13

Question 13

What is the output of this character encoding example?

java
try (FileOutputStream fos = new FileOutputStream("test.txt");
     OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8")) {
    
    osw.write("Hello, 世界");
} catch (IOException e) {
    e.printStackTrace();
}
A
Writes the string in UTF-8 encoding
B
Compilation error
C
Runtime exception
D
Writes in default encoding
14

Question 14

What is the difference between available() and read() methods?

A
available() estimates bytes available, read() actually reads data
B
available() is blocking, read() is not
C
read() is deprecated
D
They are identical
15

Question 15

What is the result of this try-with-resources example?

java
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());
}
A
Using resource Closed Caught: Error
B
Using resource Caught: Error Closed
C
Using resource Closed
D
Compilation error
16

Question 16

Why should you use BufferedInputStream with FileInputStream?

A
To reduce expensive disk I/O operations by reading larger chunks
B
To compress data
C
To handle character encoding
D
To validate data
17

Question 17

What is the difference between mark() and reset() methods?

A
mark() sets a position, reset() returns to that position
B
mark() closes the stream, reset() opens it
C
reset() is deprecated
D
They work on different stream types
18

Question 18

What is the output of this DataInputStream example?

java
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);
}
A
42 Hello
B
Compilation error
C
Runtime exception
D
No output
19

Question 19

Why should you handle IOException?

A
I/O operations can fail due to file permissions, disk space, network issues, etc.
B
To improve performance
C
To validate data
D
To compress data
20

Question 20

What is the difference between text files and binary files?

A
Text files contain human-readable characters, binary files contain any byte values
B
Text files are smaller
C
Binary files are platform-independent
D
They are identical
21

Question 21

What is the result of this file existence check?

java
File file = new File("test.txt");
if (file.exists()) {
    System.out.println("File exists");
} else {
    System.out.println("File does not exist");
}
A
Checks if the file exists and prints appropriate message
B
Compilation error
C
Runtime exception
D
Always prints 'File exists'
22

Question 22

What is the purpose of the skip() method?

A
To skip a specified number of bytes/characters in the stream
B
To close the stream
C
To flush the buffer
D
To mark a position
23

Question 23

What is the output of this Scanner example?

java
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();
}
A
Reads integers from file and prints their sum
B
Compilation error
C
Runtime exception
D
No output
24

Question 24

Why should you specify character encoding explicitly?

A
To ensure consistent behavior across different platforms and avoid encoding bugs
B
To improve performance
C
To compress data
D
To validate data
25

Question 25

What is the difference between RandomAccessFile and regular streams?

A
RandomAccessFile allows seeking to any position, regular streams are sequential
B
RandomAccessFile is faster
C
Regular streams are deprecated
D
They are identical
26

Question 26

What is the result of this resource leak example?

java
FileInputStream fis = null;
try {
    fis = new FileInputStream("test.txt");
    // do something
} catch (IOException e) {
    e.printStackTrace();
}
// fis is not closed if exception occurs!
A
Resource leak - stream not closed if exception occurs
B
No problem, garbage collector handles it
C
Compilation error
D
Runtime exception
27

Question 27

What is the purpose of ByteArrayInputStream?

A
To read from a byte array as if it were a stream
B
To write to files
C
To compress data
D
To handle character encoding
28

Question 28

What is the output of this PrintWriter example?

java
try (PrintWriter pw = new PrintWriter("output.txt")) {
    pw.println("Line 1");
    pw.print("Line 2");
    pw.println(" continues");
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
A
Line 1 Line 2 continues
B
Line 1Line 2 continues
C
Compilation error
D
Runtime exception
29

Question 29

Why should you use buffered streams for network I/O?

A
Network latency makes buffering even more important than disk I/O
B
To compress network traffic
C
To encrypt network traffic
D
To validate network data
30

Question 30

What is the result of this file copying with NIO?

java
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();
}
A
Copies the file using NIO.2 API
B
Compilation error
C
Runtime exception
D
No output
31

Question 31

What is the difference between checked and unchecked I/O exceptions?

A
IOException is checked, others like RuntimeException are unchecked
B
Checked exceptions are more serious
C
Unchecked exceptions are deprecated
D
They are identical
32

Question 32

What is the output of this StringReader example?

java
try (StringReader sr = new StringReader("Hello\nWorld")) {
    int ch;
    while ((ch = sr.read()) != -1) {
        System.out.print((char) ch);
    }
} catch (IOException e) {
    e.printStackTrace();
}
A
Hello World
B
HelloWorld
C
Compilation error
D
Runtime exception
33

Question 33

Why should you avoid reading large files entirely into memory?

A
Can cause OutOfMemoryError for large files
B
Makes code slower
C
Prevents character encoding
D
Makes files corrupted
34

Question 34

What is the result of this file walking example?

java
Path start = Paths.get(".");
try (Stream<Path> paths = Files.walk(start)) {
    paths.filter(Files::isRegularFile)
         .forEach(System.out::println);
} catch (IOException e) {
    e.printStackTrace();
}
A
Prints all regular files in the directory tree
B
Compilation error
C
Runtime exception
D
No output
35

Question 35

Mastering Java I/O means you now write code that:

A
Handles files, streams, and data reliably with proper resource management, encoding awareness, and performance optimization through buffering
B
Never uses I/O operations
C
Ignores exception handling
D
Always reads entire files into memory

QUIZZES IN Java