Rust File and I/O Operations Quiz

Rust
0 Passed
0% acceptance

Master Rust's file system operations and I/O handling for robust data persistence and streaming

40 Questions
~80 minutes
1

Question 1

What is the std::fs module used for?

A
File system operations like reading, writing, and manipulating files
B
Network I/O operations
C
Memory management
D
std::fs doesn't exist
2

Question 2

What does this file reading example do?

rust
use std::fs;
let content = fs::read_to_string("hello.txt")?;
println!("{}", content);
A
Reads entire file into String, handling errors with ?
B
Compilation error
C
Reads file as bytes
D
Runtime error
3

Question 3

What does fs::read() return?

A
Result<Vec<u8>, std::io::Error> - file content as bytes
B
Result<String, std::io::Error>
C
String
D
fs::read() doesn't exist
4

Question 4

What does this file writing example do?

rust
use std::fs;
fs::write("output.txt", "Hello, World!")?;
println!("File written");
A
Creates/overwrites file with content, handling errors
B
Compilation error
C
Appends to file
D
Runtime error
5

Question 5

What is the difference between fs::write() and File operations?

A
fs::write() is simple one-shot, File allows streaming and control
B
fs::write() is for binary, File for text
C
They are identical
D
File doesn't exist
6

Question 6

What does this File opening example do?

rust
use std::fs::File;
let file = File::open("data.txt")?;
// Use file for reading
A
Opens file for reading, returns Result<File, io::Error>
B
Compilation error
C
Opens file for writing
D
Runtime error
7

Question 7

What does File::create() do?

A
Creates new file or truncates existing file for writing
B
Opens file for reading
C
Appends to file
D
File::create() doesn't exist
8

Question 8

What is the Read trait?

A
Trait for types that can be read from, like files and network streams
B
Trait for writing data
C
Trait for file metadata
D
Read trait doesn't exist
9

Question 9

What does this Read implementation example do?

rust
use std::io::Read;
let mut file = File::open("data.txt")?;
let mut buffer = [0; 10];
file.read(&mut buffer)?;
A
Reads up to 10 bytes into buffer, returns bytes read
B
Compilation error
C
Reads entire file
D
Runtime error
10

Question 10

What is the Write trait?

A
Trait for types that can be written to, providing write() and flush()
B
Trait for reading data
C
Trait for file creation
D
Write trait doesn't exist
11

Question 11

What does this Write implementation example do?

rust
use std::io::Write;
let mut file = File::create("output.txt")?;
file.write_all(b"Hello, World!")?;
file.flush()?;
A
Writes bytes to file and ensures data is flushed to disk
B
Compilation error
C
Reads from file
D
Runtime error
12

Question 12

What is BufReader used for?

A
Buffering reads for better performance with small, frequent reads
B
Buffering writes
C
Reading entire files
D
BufReader doesn't exist
13

Question 13

What does this BufReader example do?

rust
use std::io::{BufReader, BufRead};
let file = File::open("data.txt")?;
let reader = BufReader::new(file);
for line in reader.lines() {
    println!("{}", line?);
}
A
Reads file line by line efficiently using buffered reading
B
Compilation error
C
Reads entire file at once
D
Runtime error
14

Question 14

What is BufWriter used for?

A
Buffering writes to reduce system calls and improve performance
B
Buffering reads
C
Reading files
D
BufWriter doesn't exist
15

Question 15

What does this BufWriter example do?

rust
use std::io::BufWriter;
let file = File::create("output.txt")?;
let mut writer = BufWriter::new(file);
writeln!(writer, "Hello")?;
writeln!(writer, "World")?;
writer.flush()?;
A
Buffers writes and flushes all data to file at once
B
Compilation error
C
Writes immediately
D
Runtime error
16

Question 16

What is std::io::Error?

A
Standard error type for I/O operations with error kind information
B
File reading error only
C
Network error only
D
std::io::Error doesn't exist
17

Question 17

What does this error handling example show?

rust
let result = fs::read_to_string("missing.txt");
match result {
    Ok(content) => println!("{}", content),
    Err(error) => match error.kind() {
        std::io::ErrorKind::NotFound => println!("File not found"),
        _ => println!("Other error: {}", error),
    }
}
A
Pattern matching on error kinds for specific handling
B
Compilation error
C
Ignores errors
D
Runtime error
18

Question 18

What does fs::metadata() return?

A
Result<Metadata, io::Error> with file size, permissions, timestamps
B
File content
C
File path
D
fs::metadata() doesn't exist
19

Question 19

What does this metadata example show?

rust
let metadata = fs::metadata("file.txt")?;
if metadata.is_file() {
    println!("Size: {}", metadata.len());
}
A
Checks if path is file and gets its size
B
Compilation error
C
Reads file content
D
Runtime error
20

Question 20

What does fs::create_dir() do?

A
Creates a directory, returns error if it already exists
B
Creates directory and all parent directories
C
Deletes directory
D
fs::create_dir() doesn't exist
21

Question 21

What does fs::create_dir_all() do?

A
Creates directory and all necessary parent directories
B
Creates single directory only
C
Deletes directories
D
fs::create_dir_all() doesn't exist
22

Question 22

What does fs::read_dir() return?

A
Result<ReadDir, io::Error> - iterator over directory entries
B
List of files
C
Directory metadata
D
fs::read_dir() doesn't exist
23

Question 23

What does this directory reading example do?

rust
for entry in fs::read_dir("src")? {
    let entry = entry?;
    let path = entry.path();
    if path.is_file() {
        println!("File: {}", path.display());
    }
}
A
Iterates over directory entries, printing file paths
B
Compilation error
C
Reads all files content
D
Runtime error
24

Question 24

What is std::path::Path?

A
Type for representing file system paths, cross-platform
B
File content type
C
Directory type
D
Path doesn't exist
25

Question 25

What is the difference between Path and PathBuf?

A
Path is borrowed string slice, PathBuf is owned string buffer
B
Path is owned, PathBuf is borrowed
C
They are identical
D
Neither exists
26

Question 26

What does this Path example show?

rust
use std::path::Path;
let path = Path::new("src/main.rs");
println!("Extension: {:?}", path.extension());
println!("Parent: {:?}", path.parent());
A
Path methods for getting file extension and parent directory
B
Compilation error
C
Creates file
D
Runtime error
27

Question 27

What does PathBuf do?

A
Owned path buffer that can be modified and extended
B
Borrows path data
C
Reads path from file
D
PathBuf doesn't exist
28

Question 28

What does this PathBuf example show?

rust
use std::path::PathBuf;
let mut path = PathBuf::from("src");
path.push("main.rs");
println!("{}", path.display());
A
Building path by pushing components: src/main.rs
B
Compilation error
C
Reading file
D
Runtime error
29

Question 29

What is the Seek trait?

A
Trait for seeking to positions in readable/writable streams
B
Trait for reading data
C
Trait for file metadata
D
Seek trait doesn't exist
30

Question 30

What does this Seek example do?

rust
use std::io::Seek;
let mut file = File::open("data.txt")?;
file.seek(std::io::SeekFrom::Start(100))?;
let mut buffer = [0; 10];
file.read(&mut buffer)?;
A
Seeks to position 100 and reads 10 bytes from there
B
Compilation error
C
Reads from beginning
D
Runtime error
31

Question 31

What is std::io::stdin()?

A
Standard input stream, implements Read trait
B
Standard output stream
C
File input
D
stdin() doesn't exist
32

Question 32

What does this stdin reading example do?

rust
use std::io::{self, BufRead};
let stdin = io::stdin();
for line in stdin.lines() {
    let line = line?;
    if line.trim().is_empty() { break; }
    println!("You entered: {}", line);
}
A
Reads lines from standard input until empty line
B
Compilation error
C
Reads single line
D
Runtime error
33

Question 33

What is std::io::stdout()?

A
Standard output stream, implements Write trait
B
Standard input stream
C
File output
D
stdout() doesn't exist
34

Question 34

What does fs::copy() do?

A
Copies file from source to destination path
B
Moves file
C
Deletes file
D
fs::copy() doesn't exist
35

Question 35

What does fs::rename() do?

A
Moves/renames file or directory
B
Copies file
C
Deletes file
D
fs::rename() doesn't exist
36

Question 36

What does fs::remove_file() do?

A
Deletes a file
B
Deletes directory
C
Moves file
D
fs::remove_file() doesn't exist
37

Question 37

What does fs::remove_dir() do?

A
Deletes empty directory
B
Deletes directory and contents
C
Deletes file
D
fs::remove_dir() doesn't exist
38

Question 38

What is the performance difference between buffered and unbuffered I/O?

A
Buffered I/O reduces system calls, significantly faster for small operations
B
Unbuffered I/O is always faster
C
No performance difference
D
Buffered I/O is slower
39

Question 39

What is the main advantage of using ? for error propagation in I/O?

A
Reduces boilerplate, makes error handling explicit and automatic
B
Makes code faster
C
Reduces memory usage
D
No advantages
40

Question 40

In a file processing application that needs to read large text files efficiently, handle various I/O errors gracefully, and write processed results, what combination of Rust I/O features would you use and why?

A
BufReader for input files, BufWriter for output, ? operator for error propagation, custom error types for different failure modes
B
Read entire files into memory, use unwrap() everywhere for simplicity
C
Use unbuffered I/O with manual error checking at every step
D
Avoid Rust's I/O and use external libraries

QUIZZES IN Rust