30+ MCQs on Python File I/O

Ayushi Trivedi Last Updated : 05 Mar, 2024
7 min read

Welcome to the Python File I/O MCQ Quiz! File input/output (I/O) is an essential aspect of programming, allowing data to be read from and written to files on disk. Python provides powerful built-in functions and methods for handling file operations efficiently. This quiz aims to test your understanding of various concepts related to Python file I/O, including opening, reading, writing, and closing files, as well as file modes and error handling. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Let’s dive into the world of Python file I/O Python Interview Questions.

Python File I/O

30+ Python Interview Questions on Python File I/O

Q1. What function is used to open a file in Python for reading?

a) open_file()

b) read_file()

c) open()

d) read()

Answer: c

Explanation: The open() function is used to open a file in Python for reading.

Q2. Which mode is used to open a file for writing in Python?

a) r

b) w

c) a

d) x

Answer: b

Explanation: The ‘w’ mode is used to open a file for writing in Python. If the file does not exist, it creates a new file. If the file exists, it truncates the file.

Q3. What function is used to read the entire contents of a file as a string in Python?

a) read_file()

b) read_string()

c) readlines()

d) read()

Answer: d

Explanation: The read() function is used to read the entire contents of a file as a string in Python.

Q4. In Python, what does the readline() function do?

a) Reads the entire file as a string

b) Reads a specific line from the file

c) Reads all the lines from the file

d) Reads the first line from the file

Answer: d

Explanation: The readline() function reads the first line from the file in Python.

Q5. Which mode is used to open a file for appending in Python?

a) r

b) w

c) a

d) x

Answer: c

Explanation: The ‘a’ mode is used to open a file for appending in Python. If the file does not exist, it creates a new file.

Q6. What method is used to close a file object in Python?

a) close()

b) shutdown()

c) end()

d) terminate()

Answer: a

Explanation: The close() method is used to close a file object in Python.

Q7. Which function is used to write data to a file in Python?

a) write()

b) append()

c) add()

d) insert()

Answer: a

Explanation: The write() function is used to write data to a file in Python.

Q8. What is the purpose of the with statement when dealing with file operations in Python?

a) It opens a file for reading

b) It ensures proper handling of resources and automatically closes the file when done

c) It writes data to a file

d) It appends data to a file

Answer: b

Explanation: The with statement ensures proper handling of resources and automatically closes the file when done, preventing resource leaks.

Q9. Which method is used to check if a file exists in Python?

a) exists()

b) check_file()

c) isfile()

d) file_exists()

Answer: c

Explanation: The isfile() method is used to check if a file exists in Python.

Q10. What is the difference between the ‘r’ mode and the ‘rb’ mode when opening a file in Python?

a) There is no difference

b) The ‘r’ mode is for reading text files, while the ‘rb’ mode is for reading binary files

c) The ‘rb’ mode is for reading text files, while the ‘r’ mode is for reading binary files

d) The ‘r’ mode opens the file in read-write mode, while the ‘rb’ mode opens the file in read-only mode

Answer: b

Explanation: The ‘r’ mode is for reading text files, while the ‘rb’ mode is for reading binary files in Python.

Q11. Which mode is used to open a file for reading and writing in Python?

a) r

b) w

c) r+

d) w+

Answer: c

Explanation: The ‘r+’ mode is used to open a file for reading and writing in Python.

Q12. What function is used to move the file cursor to a specific position in a file in Python?

a) seek()

b) move_cursor()

c) set_position()

d) position()

Answer: a

Explanation: The seek() function is used to move the file cursor to a specific position in a file in Python.

Q13. What does the tell() method do in Python file handling?

a) Returns the current line number being read

b) Returns the current position of the file cursor

c) Tells if the file exists or not

d) Tells the file size

Answer: b

Explanation: The tell() method returns the current position of the file cursor in Python file handling.

Q14. Which of the following statements is true about reading files in Python?

a) The read() method reads one line at a time

b) The readline() method reads the entire file at once

c) The readlines() method reads one character at a time

d) The read() method reads the entire file at once

Answer: d

Explanation: The read() method reads the entire file at once in Python.

Q15. What is the output of the following code?

file = open("data.txt", "w")
file.write("Hello, World!")
file.close()

a) It writes “Hello, World!” to the file data.txt

b) It reads “Hello, World!” from the file data.txt

c) It appends “Hello, World!” to the file data.txt

d) It does nothing

Answer: a

Explanation: The code opens the file data.txt in write mode, writes “Hello, World!” to it, and then closes the file.

Q16. Which of the following is used to open a file in Python in binary mode?

a) open(‘file.txt’, ‘b’)

b) open(‘file.txt’, ‘binary’)

c) open(‘file.txt’, ‘rb’)

d) open(‘file.txt’, ‘wb’)

Answer: c

Explanation: To open a file in binary mode in Python, use the ‘rb’ mode.

Q17. What is the purpose of the os.path.isfile() function in Python?

a) To create a new file

b) To check if a file exists

c) To read the contents of a file

d) To write data to a file

Answer: b

Explanation: The os.path.isfile() function is used to check if a file exists in Python.

Q18. Which method is used to write multiple lines to a file in Python?

a) writelines()

b) write_lines()

c) write_multiple_lines()

d) append_lines()

Answer: a

Explanation: The writelines() method is used to write multiple lines to a file in Python.

Q19. What happens if you open a file in Python using the ‘x’ mode, and the file already exists?

a) It raises a FileExistsError

b) It overwrites the existing file

c) It appends data to the existing file

d) It raises a FileNotFoundError

Answer: a

Explanation: If you open a file in Python using the ‘x’ mode and the file already exists, it raises a FileExistsError.

Q20. How can you read a JSON file in Python?

a) Using the load_json() function

b) Using the read_json() function

c) Using the json.load() function

d) Using the json.read() function

Answer: c

Explanation: You can read a JSON file in Python using the json.load() function.

Q21. Which method is used to write a dictionary to a JSON file in Python?

a) write_dict()

b) save_json()

c) json.dump()

d) write_json()

Answer: c

Explanation: The json.dump() method is used to write a dictionary to a JSON file in Python.

Q22. What is the purpose of the ‘a+’ mode when opening a file in Python?

a) It opens the file in append mode for reading and writing

b) It opens the file in append mode for writing only

c) It opens the file in append mode for reading only

d) It opens the file in append mode for reading, writing, and creating

Answer: a

Explanation: The ‘a+’ mode opens the file in append mode for reading and writing in Python.

Q23. Which module is used for reading and writing CSV files in Python?

a) os

b) csv

c) pandas

d) sys

Answer: b

Explanation: The csv module is used for reading and writing CSV files in Python.

Q24. What is the output of the following code?

with open('data.txt', 'r') as file:
    print(file.read())

a) Prints the contents of data.txt

b) Reads the contents of data.txt into a variable

c) Raises a FileNotFoundError

d) Writes to data.txt

Answer: c

Explanation: This code will raise a FileNotFoundError because it tries to read from a file that does not exist.

Q25. How can you write binary data to a file in Python?

a) Using the write_binary() function

b) Using the binary.write() function

c) Using the write() function with bytes as input

d) Using the binary_write() function

Answer: c

Explanation: You can write binary data to a file in Python using the write() function with bytes as input.

Q26. What does the ‘rb+’ mode do when opening a file in Python?

a) Opens the file for reading and writing in binary mode

b) Opens the file for reading and writing, creating the file if it does not exist

c) Opens the file for reading in binary mode

d) Opens the file for reading and writing, truncating the file to zero length

Answer: a

Explanation: The ‘rb+’ mode opens the file for reading and writing in binary mode in Python.

Q27. Which method is used to read CSV files in Python?

a) read_csv()

b) read()

c) csv_read()

d) csv.reader()

Answer: d

Explanation: The csv.reader() method is used to read CSV files in Python.

Q28. What does the ‘wb’ mode do when opening a file in Python?

a) Opens the file for reading and writing in binary mode

b) Opens the file for writing in binary mode

c) Opens the file for reading in binary mode

d) Opens the file for reading and writing, truncating the file to zero length

Answer: b

Explanation: The ‘wb’ mode opens the file for writing in binary mode in Python.

Q29. How do you read only the first n characters from a file in Python?

a) Using the read(n) method

b) Using the readlines(n) method

c) Using the readline(n) method

d) Using the read_first(n) method

Answer: a

Explanation: You can read only the first n characters from a file in Python using the read(n) method.

Q30. What is the purpose of the os module in Python file handling?

a) To create a new file

b) To read the contents of a file

c) To manage files and directories

d) To write data to a file

Answer: c

Explanation: The os module in Python is used to manage files and directories.

Q31. What will the following Python code snippet do?

with open("existing_data.txt", "a") as file:
    file.write("New data")

a) It appends “New data” to the file existing_data.txt

b) It reads “New data” from the file existing_data.txt

c) It writes “New data” to the file existing_data.txt

d) It overwrites the file existing_data.txt with “New data”

Answer: a

Explanation: The code appends “New data” to the file existing_data.txt.

Q32. What will the following Python code do?

with open("output.txt", "w") as file:
    file.write("Hello, World!")

a) It reads “Hello, World!” from the file output.txt

b) It appends “Hello, World!” to the file output.txt

c) It writes “Hello, World!” to the file output.txt

d) It does nothing

Answer: c

Explanation: The code writes “Hello, World!” to the file output.txt.

Congratulations on completing the Python File I/O MCQ Quiz! File input/output operations are fundamental to many programming tasks, and Python offers robust features to handle them effectively. By mastering file I/O in Python, you gain the ability to manipulate data stored in files, work with different file formats, and build powerful applications that read from and write to external files. Keep practicing and exploring Python’s file I/O functionalities to become proficient in handling files within your programs. If you have any questions or want to delve deeper into any topic, don’t hesitate to continue your learning journey. Happy coding!

You can also enroll in our free Python Course Today!

Read our more articles related to MCQs in Python:

My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details