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
Which method in Ruby is used to print a message to the console?
A) puts
B) print
C) get
D) output
How can you read user input from the console in Ruby?
A) gets
B) input
C) read
D) input_data
Which of the following methods prints data without adding a newline at the end?
A) puts
B) print
C) write
D) echo
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
Which method is used to remove the trailing newline character from user input in Ruby?
A) strip
B) chomp
C) trim
D) clean
How do you read a single character of input in Ruby?
A) gets_char
B) gets.chomp
C) getc
D) input_char
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
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}”
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
Which method is used to clear the console output in Ruby?
A) clear
B) cls
C) system(‘clear’)
D) reset
Working with Files
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”)
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
Which Ruby method is used to close an open file?
A) close_file
B) close
C) file.close
D) end
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
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”)
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
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”)
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
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
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
Which Ruby method is used to check if a file exists?
A) File.exist?
B) File.exists?
C) File.check
D) File.open?
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
Which Ruby class is used to handle file paths?
A) File
B) Path
C) Pathname
D) Directory
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?
Which method would you use to delete a file in Ruby?
A) File.delete
B) File.remove
C) File.delete_file
D) File.erase
What is the default mode when opening a file in Ruby using File.open("file.txt")?
A) “r”
B) “w”
C) “a”
D) “rw”
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
How do you handle errors when working with files in Ruby?
A) Using rescue
B) Using try
C) Using handle
D) Using catch
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
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
Qno
Answer (Option with the text)
1
A) puts
2
A) gets
3
B) print
4
D) It will print “Hello, ” followed by the user’s input
5
B) chomp
6
C) getc
7
A) puts adds a newline, print does not
8
D) puts “Hello, #{name}”
9
A) You are 30 years old
10
C) system(‘clear’)
11
B) File.open(“file.txt”, “r”)
12
A) File.write
13
B) close
14
B) The file will be created and opened for writing
15
B) File.open(“file.txt”, “a”)
16
A) Read and print each line of the file
17
A) File.read(“file.txt”)
18
A) It opens the file for writing, creating a new file if it doesn’t exist
19
A) File.each_line
20
B) It will create a file called “file.txt” and write “Hello, World!” to it