1
What does File.open() return in Ruby when the file is successfully opened?
A)
Integer representing file descriptor
B)
Array of file contents
C)
Boolean value
D)
File object
Answer & Solution
Answer: Option
D
2
How do you open a file in Ruby in write mode, creating it if it does not exist?
A)
File.create("filename.txt", "w")
B)
File.write("filename.txt", "w")
C)
File.open("filename.txt", "w")
D)
File.new("filename.txt", "w")
Answer & Solution
Answer: Option
C
3
Which method in Ruby is used to read a line from a file?
A)
read
B)
readline
C)
getline
D)
None of the above
Answer & Solution
Answer: Option
B
4
What is the purpose of the File.readlines() method in Ruby?
A)
To delete all lines from a file
B)
To write all lines of a file from an array
C)
To read all lines of a file into an array
D)
To read a specific line from a file
Answer & Solution
Answer: Option
C
5
How do you close a file in Ruby after performing file operations?
A)
close(file)
B)
File.close(file)
C)
Close.file()
D)
file.close()
Answer & Solution
Answer: Option
D
6
Which method in Ruby is used to write data to a file?
A)
write()
B)
puts()
C)
print()
D)
<<()
Answer & Solution
Answer: Option
A
7
What does the 'a+' file mode in Ruby signify?
A)
Opens a file for appending, creating it if it does not exist
B)
Opens a file for writing, creating it if it does not exist
C)
Opens a file for reading and writing, creating it if it does not exist
D)
Opens a file for reading and appending, creating it if it does not exist
Answer & Solution
Answer: Option
D
8
Which method in Ruby is used to check if the end of a file has been reached?
A)
end_of_file()
B)
at_end()
C)
eof()
D)
file_end()
Answer & Solution
Answer: Option
C
9
What does the File.exist? method in Ruby return if the specified file exists?
A)
1
B)
TRUE
C)
0
D)
Error
Answer & Solution
Answer: Option
B
10
What does the 'r+' file mode in Ruby signify?
A)
Opens a file for appending, creating it if it does not exist
B)
Opens a file for writing, creating it if it does not exist
C)
Opens a file for reading and writing, creating it if it does not exist
D)
Opens a file for reading and appending, creating it if it does not exist
Answer & Solution
Answer: Option
C