MCQs on File Handling and Serialization | Ruby

Dive into Ruby’s file handling, serialization using JSON and YAML, and working with CSV files. These questions will test your understanding and help you work effectively with files in Ruby.


Topic 1: Reading and Writing Files in Depth

  1. What method is used to open a file for reading in Ruby?
    a) File.read()
    b) File.open()
    c) File.write()
    d) File.open('r')
  2. How do you open a file for writing in Ruby?
    a) File.open('file.txt', 'w')
    b) File.write('file.txt')
    c) File.open('file.txt', 'r')
    d) File.write('file.txt', 'w')
  3. Which Ruby method is used to read the entire content of a file into a string?
    a) File.read()
    b) File.gets()
    c) File.open()
    d) File.readlines()
  4. Which method is used to read the content of a file line by line in Ruby?
    a) File.open('file.txt')
    b) File.read()
    c) File.each_line()
    d) File.gets()
  5. What is the default mode when opening a file in Ruby using File.open()?
    a) r (read-only)
    b) w (write)
    c) a (append)
    d) rb (read-binary)
  6. How do you append content to a file in Ruby?
    a) File.open('file.txt', 'a')
    b) File.open('file.txt', 'w')
    c) File.write('file.txt', 'a')
    d) File.append('file.txt')
  7. Which method is used to write data to a file in Ruby?
    a) File.append()
    b) File.write()
    c) File.open()
    d) File.write_data()
  8. How can you safely close a file after opening it in Ruby?
    a) File.close()
    b) close()
    c) File.open().close()
    d) end
  9. How do you handle exceptions when working with files in Ruby?
    a) begin and rescue
    b) try and catch
    c) if and else
    d) error and exception
  10. What is the result of reading a file in binary mode (rb) in Ruby?
    a) It reads the file as a binary stream
    b) It reads the file line by line
    c) It converts the file content to a string
    d) It only reads the file’s metadata

Topic 2: Serialization with JSON and YAML

  1. Which gem is required to use JSON for serialization in Ruby?
    a) json
    b) yaml
    c) json_pure
    d) json_serializer
  2. Which Ruby method is used to convert an object into a JSON string?
    a) JSON.to_json()
    b) JSON.dump()
    c) JSON.parse()
    d) JSON.generate()
  3. How do you parse a JSON string back into a Ruby object?
    a) JSON.parse()
    b) JSON.to_ruby()
    c) JSON.read()
    d) JSON.load()
  4. What method is used to write Ruby objects into a JSON file?
    a) File.write()
    b) JSON.generate()
    c) JSON.dump()
    d) JSON.create()
  5. Which Ruby gem is used to handle YAML serialization?
    a) yaml
    b) json
    c) yaml_parser
    d) serialize
  6. Which method is used to convert a Ruby object into a YAML string?
    a) YAML.load()
    b) YAML.dump()
    c) YAML.parse()
    d) YAML.stringify()
  7. How do you parse a YAML string into a Ruby object?
    a) YAML.parse()
    b) YAML.load()
    c) YAML.to_ruby()
    d) YAML.import()
  8. Which of the following can be serialized using JSON and YAML in Ruby?
    a) Arrays and Hashes
    b) Procs and Lambdas
    c) Methods and Blocks
    d) Only Hashes
  9. What happens if you try to serialize an unsupported object with JSON or YAML in Ruby?
    a) It raises an error
    b) It returns nil
    c) It converts the object to an empty string
    d) It skips the object
  10. How can you read and write JSON data to a file in Ruby?
    a) Using File.read() and File.write()
    b) Using JSON.read() and JSON.write()
    c) Using File.json_read() and File.json_write()
    d) Using JSON.load() and JSON.dump()

Topic 3: Using CSV Files

  1. Which Ruby gem is required to handle CSV files?
    a) csv
    b) json
    c) yaml
    d) csv_reader
  2. What method is used to read CSV data from a file in Ruby?
    a) CSV.read()
    b) CSV.load()
    c) CSV.parse()
    d) CSV.open()
  3. Which of the following methods is used to write CSV data to a file in Ruby?
    a) CSV.write()
    b) CSV.dump()
    c) CSV.generate()
    d) CSV.open()
  4. How do you define the delimiter when reading a CSV file in Ruby?
    a) CSV.open(file, { col_sep: ";" })
    b) CSV.read(file, { delimiter: "," })
    c) CSV.parse(file, { sep: ";" })
    d) CSV.write(file, { delimiter: "|" })
  5. What method can be used to read CSV data line by line in Ruby?
    a) CSV.each()
    b) CSV.gets()
    c) CSV.line()
    d) CSV.each_line()
  6. How do you write data to a CSV file using an array of arrays in Ruby?
    a) CSV.write(file, data)
    b) CSV.open(file, data)
    c) CSV.generate(file, data)
    d) CSV.dump(file, data)
  7. What does the CSV.foreach() method do in Ruby?
    a) It reads the file line by line
    b) It parses the entire file
    c) It writes data to the file
    d) It converts CSV data to JSON
  8. How do you specify the encoding when working with CSV files in Ruby?
    a) CSV.open('file.csv', encoding: 'utf-8')
    b) CSV.read('file.csv', encoding: 'utf-8')
    c) CSV.load('file.csv', encoding: 'utf-8')
    d) CSV.write('file.csv', encoding: 'utf-8')
  9. What will happen if you try to read a CSV file without specifying the correct delimiter in Ruby?
    a) It will raise an error
    b) It will treat all data as a single column
    c) It will skip the first row
    d) It will insert a default delimiter
  10. Which method is used to convert CSV data into a hash in Ruby?
    a) CSV.to_hash()
    b) CSV.parse_hash()
    c) CSV.read_hash()
    d) CSV.each_with_index()

Answers Table

QnoAnswer (Option with the text)
1b) File.open()
2a) File.open('file.txt', 'w')
3a) File.read()
4c) File.each_line()
5a) r (read-only)
6a) File.open('file.txt', 'a')
7b) File.write()
8a) File.close()
9a) begin and rescue
10a) It reads the file as a binary stream
11a) json
12d) JSON.generate()
13a) JSON.parse()
14c) JSON.dump()
15a) yaml
16b) YAML.dump()
17b) YAML.load()
18a) Arrays and Hashes
19a) It raises an error
20a) Using File.read() and File.write()
21a) csv
22a) CSV.read()
23d) CSV.open()
24b) CSV.read(file, { delimiter: "," })
25a) CSV.each()
26a) CSV.write(file, data)
27a) It reads the file line by line
28a) CSV.open('file.csv', encoding: 'utf-8')
29b) It will treat all data as a single column
30a) CSV.to_hash()

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