MCQs on File I/O | Lua

1. Reading from and Writing to Files

  1. Which function is used to open a file for reading in Lua?
    • a) io.read()
    • b) io.open()
    • c) io.write()
    • d) file.open()
  2. How do you write data to a file in Lua?
    • a) io.write()
    • b) io.append()
    • c) io.input()
    • d) file.write()
  3. What does the io.read() function do in Lua?
    • a) It reads the entire content of a file
    • b) It reads a specified number of characters from the file
    • c) It opens a file for reading
    • d) It writes data to the file
  4. Which of the following is used to read an entire file in Lua?
    • a) io.read("*all")
    • b) io.read("*line")
    • c) io.read("*char")
    • d) io.read("*block")
  5. How can you close a file after reading or writing in Lua?
    • a) io.close()
    • b) file.close()
    • c) io.end()
    • d) io.finish()
  6. Which function is used to write a string to a file in Lua?
    • a) io.write()
    • b) io.print()
    • c) io.output()
    • d) file.write()
  7. When you open a file in write mode ("w"), what happens if the file already exists?
    • a) It overwrites the file
    • b) It appends data to the file
    • c) It raises an error
    • d) It creates a backup of the file
  8. How do you read a single line from a file in Lua?
    • a) io.read("*line")
    • b) io.read(1)
    • c) file.readline()
    • d) io.read("*char")
  9. Which Lua function is used to read the next number from a file?
    • a) io.read("*number")
    • b) io.read("*line")
    • c) io.read("*all")
    • d) file.read("*num")
  10. What happens if you attempt to write to a file that is not open for writing in Lua?
    • a) An error is raised
    • b) The data is written to the console
    • c) The data is discarded
    • d) The program crashes

2. Using io Library Functions

  1. Which function in the io library is used to open a file?
    • a) io.open()
    • b) io.file()
    • c) io.openfile()
    • d) file.open()
  2. Which of the following is a valid file mode for reading in Lua?
    • a) "r"
    • b) "w"
    • c) "a"
    • d) "x"
  3. What file mode is used to open a file for appending in Lua?
    • a) "a"
    • b) "r"
    • c) "w"
    • d) "rw"
  4. How do you open a file for both reading and writing in Lua?
    • a) "r+"
    • b) "rw"
    • c) "rw+"
    • d) "r"
  5. Which function is used to check if a file exists in Lua?
    • a) io.open()
    • b) io.check()
    • c) io.exists()
    • d) file.exists()
  6. What happens if a file is opened in "r" mode but does not exist?
    • a) It returns nil and an error message
    • b) It creates a new file
    • c) It raises a runtime error
    • d) It creates a temporary file
  7. How can you read multiple lines from a file in Lua?
    • a) Using io.read("*line") in a loop
    • b) Using io.readlines()
    • c) Using file.read("*lines")
    • d) Using io.read(5)
  8. What is the purpose of the io.flush() function in Lua?
    • a) It flushes the output buffer to the file
    • b) It clears the content of a file
    • c) It closes the file
    • d) It reads the remaining content of the file
  9. What happens if you attempt to write to a file that was opened in "r" mode?
    • a) An error is raised
    • b) Data is written to the console
    • c) The program ignores the write request
    • d) The file is overwritten
  10. How can you write multiple lines to a file in Lua?
    • a) Use io.write() multiple times
    • b) Use io.println()
    • c) Use file.writelines()
    • d) Use io.append()

3. Working with File Modes (Binary/Text)

  1. Which file mode in Lua is used for binary file reading?
    • a) "rb"
    • b) "b"
    • c) "rt"
    • d) "w"
  2. How do you open a file for both reading and writing in binary mode?
    • a) "rb+"
    • b) "rw+"
    • c) "br+"
    • d) "rw"
  3. What is the difference between the "r" and "rb" modes in Lua?
    • a) "r" opens a text file, "rb" opens a binary file
    • b) "r" is used for writing, "rb" for reading
    • c) "r" opens for reading and writing, "rb" for reading only
    • d) There is no difference
  4. What does the "w" mode do when opening a file in Lua?
    • a) It opens the file for writing, creating a new file or overwriting an existing one
    • b) It opens the file for writing, appending data
    • c) It opens the file in binary mode
    • d) It opens the file for reading
  5. What is the purpose of the "a" mode when opening a file in Lua?
    • a) To open a file for appending data at the end of the file
    • b) To open a file for reading and writing
    • c) To open a file in binary mode
    • d) To create a new file
  6. In Lua, which file mode is used for reading and writing with text encoding?
    • a) "r"
    • b) "rb"
    • c) "w"
    • d) "wt"
  7. Which mode should be used to open a file for updating in Lua?
    • a) "r+"
    • b) "w+"
    • c) "rw"
    • d) "b+"
  8. How can you open a file for writing, but only if the file does not already exist?
    • a) "x"
    • b) "r+"
    • c) "w"
    • d) "a"
  9. What does "rb" mode do when opening a file in Lua?
    • a) Opens the file for reading in binary mode
    • b) Opens the file for reading in text mode
    • c) Opens the file for writing in binary mode
    • d) Opens the file for appending
  10. Which of the following modes can be used to append text to an existing file in Lua?
    • a) "a"
    • b) "w"
    • c) "r"
    • d) "rb"

Answer Key

QnoAnswer (Option with the text)
1b) io.open()
2a) io.write()
3b) It reads a specified number of characters from the file
4a) io.read("*all")
5a) io.close()
6a) io.write()
7a) It overwrites the file
8a) io.read("*line")
9a) io.read("*number")
10a) An error is raised
11a) io.open()
12a) "r"
13a) "a"
14a) "r+"
15a) io.open()
16a) It returns nil and an error message
17a) Using io.read("*line") in a loop
18a) It flushes the output buffer to the file
19a) An error is raised
20a) Use io.write() multiple times
21a) "rb"
22a) "rb+"
23a) "r" opens a text file, "rb" opens a binary file
24a) It opens the file for writing, creating a new file or overwriting an existing one
25a) To open a file for appending data at the end of the file
26a) "r"
27a) "r+"
28a) "x"
29a) Opens the file for reading in binary mode
30a) "a"

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