MCQs on Input and Output | Ruby

Master the key concepts of input and output operations in Ruby, covering reading and writing to the console, and working with files. These 30 multiple-choice questions will help you understand these essential Ruby features.


Reading from and Writing to the Console

  1. Which method in Ruby is used to print a message to the console?
    • A) puts
    • B) print
    • C) get
    • D) output
  2. How can you read user input from the console in Ruby?
    • A) gets
    • B) input
    • C) read
    • D) input_data
  3. Which of the following methods prints data without adding a newline at the end?
    • A) puts
    • B) print
    • C) write
    • D) echo
  4. What will the following Ruby code snippet output? name = gets.chomp puts "Hello, #{name}"
    • A) It will print the value of ‘name’
    • B) It will print “Hello, name”
    • C) It will print “Hello,” and wait for input
    • D) It will print “Hello, ” followed by the user’s input
  5. Which method is used to remove the trailing newline character from user input in Ruby?
    • A) strip
    • B) chomp
    • C) trim
    • D) clean
  6. How do you read a single character of input in Ruby?
    • A) gets_char
    • B) gets.chomp
    • C) getc
    • D) input_char
  7. What is the difference between the puts and print methods in Ruby?
    • A) puts adds a newline, print does not
    • B) puts prints without formatting, print formats output
    • C) puts prints on the same line, print moves to a new line
    • D) There is no difference
  8. How do you print a formatted string with interpolation in Ruby?
    • A) printf “Hello, #{name}”
    • B) puts “Hello, name”
    • C) print “Hello, #name”
    • D) puts “Hello, #{name}”
  9. What will the following code snippet print? age = 30 puts "You are #{age} years old"
    • A) You are 30 years old
    • B) You are age years old
    • C) You are #{age} years old
    • D) It will throw an error
  10. Which method is used to clear the console output in Ruby?
    • A) clear
    • B) cls
    • C) system(‘clear’)
    • D) reset

Working with Files

  1. How do you open a file for reading in Ruby?
    • A) File.read(“file.txt”)
    • B) File.open(“file.txt”, “r”)
    • C) File.read(“file”)
    • D) File.open(“file”, “r”)
  2. What method do you use to write content to a file in Ruby?
    • A) File.write
    • B) File.print
    • C) File.puts
    • D) File.open
  3. Which Ruby method is used to close an open file?
    • A) close_file
    • B) close
    • C) file.close
    • D) end
  4. What will happen if you try to open a file for writing with File.open("file.txt", "w") when the file does not exist?
    • A) Ruby will raise an error
    • B) The file will be created and opened for writing
    • C) The file will be created but opened for reading
    • D) The file will not be created and nothing will happen
  5. How do you append data to a file in Ruby?
    • A) File.open(“file.txt”, “w”)
    • B) File.open(“file.txt”, “a”)
    • C) File.append(“file.txt”)
    • D) File.write(“file.txt”)
  6. What will the following code do? File.open("example.txt", "r") do |file| file.each_line { |line| puts line } end
    • A) Read and print each line of the file
    • B) Open the file for writing
    • C) Write each line to a new file
    • D) Read the first line of the file
  7. How can you read the entire content of a file as a string in Ruby?
    • A) File.read(“file.txt”)
    • B) File.open(“file.txt”)
    • C) File.read_all(“file.txt”)
    • D) File.readlines(“file.txt”)
  8. What is the purpose of the w mode in the File.open method?
    • A) It opens the file for writing, creating a new file if it doesn’t exist
    • B) It opens the file for reading and writing
    • C) It opens the file in binary mode
    • D) It opens the file for reading only
  9. Which method allows you to read a file line by line in Ruby?
    • A) File.each_line
    • B) File.read
    • C) File.readlines
    • D) File.lines
  10. What will the following code output? File.open("file.txt", "w") do |file| file.puts "Hello, World!" end
    • A) It will print “Hello, World!” to the console
    • B) It will create a file called “file.txt” and write “Hello, World!” to it
    • C) It will read the contents of “file.txt”
    • D) It will throw an error
  11. Which Ruby method is used to check if a file exists?
    • A) File.exist?
    • B) File.exists?
    • C) File.check
    • D) File.open?
  12. What does the r mode do when opening a file in Ruby?
    • A) Opens the file for reading only
    • B) Opens the file for reading and writing
    • C) Opens the file for writing
    • D) Opens the file for appending
  13. Which Ruby class is used to handle file paths?
    • A) File
    • B) Path
    • C) Pathname
    • D) Directory
  14. How do you check if a file is empty in Ruby?
    • A) File.empty?
    • B) File.size == 0
    • C) File.read.empty?
    • D) File.exists?
  15. Which method would you use to delete a file in Ruby?
    • A) File.delete
    • B) File.remove
    • C) File.delete_file
    • D) File.erase
  16. What is the default mode when opening a file in Ruby using File.open("file.txt")?
    • A) “r”
    • B) “w”
    • C) “a”
    • D) “rw”
  17. What is the result of the following Ruby code? File.open("file.txt", "r") { |file| puts file.read(5) }
    • A) It prints the first 5 bytes of the file
    • B) It reads the entire file
    • C) It prints the entire content of the file
    • D) It throws an error
  18. How do you handle errors when working with files in Ruby?
    • A) Using rescue
    • B) Using try
    • C) Using handle
    • D) Using catch
  19. Which method would you use to read a file line by line in Ruby, while also returning an array of lines?
    • A) File.readlines
    • B) File.open
    • C) File.each_line
    • D) File.readlines
  20. What will happen if you attempt to open a file in Ruby using an invalid path?
    • A) Ruby will raise an error
    • B) The program will continue without opening the file
    • C) It will return nil
    • D) It will create the file at the invalid path

Answer Key

QnoAnswer (Option with the text)
1A) puts
2A) gets
3B) print
4D) It will print “Hello, ” followed by the user’s input
5B) chomp
6C) getc
7A) puts adds a newline, print does not
8D) puts “Hello, #{name}”
9A) You are 30 years old
10C) system(‘clear’)
11B) File.open(“file.txt”, “r”)
12A) File.write
13B) close
14B) The file will be created and opened for writing
15B) File.open(“file.txt”, “a”)
16A) Read and print each line of the file
17A) File.read(“file.txt”)
18A) It opens the file for writing, creating a new file if it doesn’t exist
19A) File.each_line
20B) It will create a file called “file.txt” and write “Hello, World!” to it
21A) File.exist?
22A) Opens the file for reading only
23C) Pathname
24B) File.size == 0
25A) File.delete
26A) “r”
27A) It prints the first 5 bytes of the file
28A) Using rescue
29A) File.readlines
30A) Ruby will raise an error

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