MCQs on File I/O and Working with Files | Rust

In Rust, file handling involves reading from and writing to files, managing paths and directories, and efficiently handling errors. This section also covers serializing and deserializing data, such as JSON, using Rust’s robust libraries for file I/O and data manipulation.


MCQs on File I/O and Working with Files in Rust

Reading from and Writing to Files

  1. Which function is used to read a file in Rust?
    • a) File::read()
    • b) fs::read()
    • c) fs::read_to_string()
    • d) File::open()
  2. To write data to a file in Rust, which method would you use?
    • a) fs::write()
    • b) File::write()
    • c) File::open()
    • d) fs::open()
  3. Which function in Rust is used to create a new file or truncate an existing file?
    • a) File::create()
    • b) File::new()
    • c) fs::open()
    • d) fs::create()
  4. What is the correct syntax to read the entire content of a file into a string in Rust?
    • a) fs::read_to_string("file.txt")
    • b) File::read("file.txt")
    • c) File::open("file.txt").read()
    • d) fs::read("file.txt")
  5. Which of the following is used to write to a file in Rust and append data to it?
    • a) File::write_all()
    • b) fs::append()
    • c) File::create()
    • d) fs::write_all()

Error Handling with File Operations

  1. Which of the following is the correct way to handle file operation errors in Rust?
    • a) Using try-catch blocks
    • b) Using Result type and match statements
    • c) Ignoring errors
    • d) Using error! macros
  2. What type does the read_to_string() method return if an error occurs?
    • a) Option<String>
    • b) Result<(), std::io::Error>
    • c) Result<String, std::io::Error>
    • d) None
  3. What does the ? operator do when working with file operations in Rust?
    • a) It prints the error message
    • b) It unwraps the error and panics
    • c) It propagates the error to the caller
    • d) It ignores the error
  4. How do you handle errors in file reading when using File::open() in Rust?
    • a) By using unwrap()
    • b) By using expect()
    • c) By using Result and match
    • d) By ignoring the error
  5. What does the File::open() function return in case of an error?
  • a) None
  • b) Option
  • c) Result
  • d) Error

Working with Paths and Directories

  1. Which module in Rust is used for working with paths?
  • a) fs::path
  • b) std::path
  • c) std::fs::path
  • d) path::std
  1. How do you join two paths together in Rust?
  • a) path::combine()
  • b) path::join()
  • c) Path::append()
  • d) Path::join()
  1. Which of the following is used to get the file name from a path in Rust?
  • a) Path::filename()
  • b) Path::file_name()
  • c) fs::file_name()
  • d) Path::get_name()
  1. How can you check if a path exists in Rust?
  • a) fs::exists(path)
  • b) path::exists()
  • c) path.exists()
  • d) fs::metadata(path).is_ok()
  1. Which function is used to create a new directory in Rust?
  • a) fs::mkdir()
  • b) std::mkdir()
  • c) fs::create_dir()
  • d) Path::create_dir()

Serializing and Deserializing Data (e.g., JSON)

  1. Which Rust crate is commonly used for serializing and deserializing JSON data?
  • a) serde_json
  • b) json_io
  • c) serde
  • d) rust_json
  1. What trait is required for an object to be serializable in Rust?
  • a) Serialize
  • b) Deserializable
  • c) JsonSerialize
  • d) Encodable
  1. How do you serialize a struct into JSON format in Rust using serde_json?
  • a) serde_json::to_json()
  • b) serde_json::serialize()
  • c) serde_json::to_string()
  • d) serde_json::serialize_to_json()
  1. What function is used to deserialize JSON into a Rust object using serde_json?
  • a) serde_json::deserialize()
  • b) serde_json::to_object()
  • c) serde_json::from_str()
  • d) serde_json::to_obj()
  1. Which of the following data types can be serialized using serde in Rust?
  • a) Only primitive types
  • b) Only structs and enums
  • c) All Rust data types
  • d) Only arrays and vectors
  1. Which crate do you need to add to your Cargo.toml to use serde for JSON serialization in Rust?
  • a) serde
  • b) serde_json
  • c) serde_serialize
  • d) serde_json_serialize
  1. How do you enable serialization for a struct in Rust with serde?
  • a) #[derive(Serializable)]
  • b) #[derive(Serialize)]
  • c) #[json_serialize]
  • d) #[derive(Json)]
  1. What is the default format for serializing data using serde_json in Rust?
  • a) Binary
  • b) YAML
  • c) JSON
  • d) XML
  1. Which method is used to deserialize JSON into a custom struct in Rust?
  • a) deserialize_json()
  • b) from_json()
  • c) serde_json::from_str()
  • d) serde_json::parse()
  1. In Rust, what is the purpose of the #[serde(rename_all = "camelCase")] attribute?
  • a) It renames fields during deserialization
  • b) It renames fields during serialization
  • c) It ensures correct JSON format
  • d) It disables field renaming
  1. Which method in serde_json is used to pretty-print JSON data?
  • a) serde_json::pretty()
  • b) serde_json::to_string_pretty()
  • c) serde_json::print_pretty()
  • d) serde_json::pretty_print()
  1. What does the #[serde(default)] attribute do in Rust serialization?
  • a) It sets the default value for a field during serialization
  • b) It ensures that a field is always serialized
  • c) It provides a default value during deserialization when the field is missing
  • d) It prevents the field from being serialized
  1. How can you check if a file is a directory in Rust?
  • a) fs::is_dir()
  • b) path.is_directory()
  • c) metadata(path).is_dir()
  • d) fs::is_directory(path)
  1. Which of the following is used to get the extension of a file in Rust?
  • a) path::get_extension()
  • b) Path::extension()
  • c) path.extension()
  • d) Path::get_extension()
  1. How do you write a struct as JSON to a file in Rust?
  • a) fs::write_json()
  • b) serde_json::to_writer()
  • c) serde_json::write()
  • d) fs::write_to_json()

Answers

QnoAnswer (Option with Text)
1c) fs::read_to_string()
2a) fs::write()
3a) File::create()
4a) fs::read_to_string(“file.txt”)
5a) File::write_all()
6b) Using Result type and match statements
7c) Result<String, std::io::Error>
8c) It propagates the error to the caller
9c) By using Result and match
10c) Result
11b) std::path
12d) Path::join()
13b) Path::file_name()
14d) fs::metadata(path).is_ok()
15c) fs::create_dir()
16a) serde_json
17a) Serialize
18c) serde_json::to_string()
19c) serde_json::from_str()
20c) All Rust data types
21a) serde
22b) #[derive(Serialize)]
23c) JSON
24c) serde_json::from_str()
25b) It renames fields during serialization
26b) serde_json::to_string_pretty()
27c) It provides a default value during deserialization when the field is missing
28c) metadata(path).is_dir()
29b) Path::extension()
30b) serde_json::to_writer()

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top