Ruby File Handling MCQ
This section focuses on "File Handling" MCQs in Ruby. These Multiple Choice Questions (mcq) should be practiced to improve the Ruby skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive examinations.
1. Which of the following is not correct method class IO provides?
A. readline
B. gets
C. puts
D. Writeline
View Answer
Ans : D
Explanation: Writeline is not correct method class IO provides.The class IO provides all the basic methods, such as read, write, gets, puts, readline, getc, and printf.
2. What will be output for the following code?
val1 = ""This is variable one""
puts ""val1""
A. This is variable one
B. VAL1
C. This is variable one val1
D. val1
View Answer
Ans : D
Explanation: val1 is the correct output for the following code.
3. Which statement can be used to take any input from the user from standard screen?
A. gets
B. puts
C. read
D. write
View Answer
Ans : A
Explanation: The gets statement can be used to take any input from the user from standard screen called STDIN.
4. What will be output for the following code?
str = ""Hello Ruby!""
putc str
A. Hello Ruby!
B. H
C. R
D. Hello
View Answer
Ans : B
Explanation: The output of the following code is just the character H.The putc statement can be used to output one character at a time.
5. What is the syntax to close file in ruby?
A. File.close()
B. close.File
C. File.close
D. close.File()
View Answer
Ans : C
Explanation: File.close is the syntax to close file in ruby.
6. What is the syntax to open file in ruby?
A. File.open("filename", "mode")
B. File.open("filename")
C. File.open()
D. All of the above
View Answer
Ans : A
Explanation: File.open("filename", "mode") is the syntax to open file in ruby.
7. Which method is used to write content in the file ?
A. systemwrite
B. sysputs
C. syswrite
D. systemputs
View Answer
Ans : C
Explanation: You can use the method syswrite to write the contents into a file. You need to open the file in write mode when using the method syswrite.
8. Which of the following is correct difference between foreach and readlines?
A. The method readline does not return an array.
B. The method foreach does not return an array.
C. readline is associated with a block
D. Both A and C
View Answer
Ans : B
Explanation: The difference between the method foreach and the method readlines is that the method foreach is associated with a block. However, unlike the method readlines, the method foreach does not return an array.
9. Which of the method is used to returns the contents of the file line by line?
A. IO.read
B. IO.foreach
C. IO.syswrite
D. IO.readlines
View Answer
Ans : D
Explanation: One of the IO class methods is IO.readlines. This method returns the contents of the file line by line.
10. Which method is used to change the mode or permissions/access list of a file?
A. mode
B. chmod
C. access
D. permissions
View Answer
Ans : B
Explanation: Use the chmod method with a mask to change the mode or permissions/access list of a file.
Discussion