Welcome to the Python File I/O MCQ Quiz! File enter/output (I/O) is an important facet of programming, permitting knowledge to be learn from and written to information on disk. Python supplies highly effective built-in capabilities and strategies for dealing with file operations effectively. This quiz goals to check your understanding of assorted ideas associated to Python file I/O, together with opening, studying, writing, and shutting information, in addition to file modes and error dealing with. Every query is multiple-choice, with just one right reply. Take your time to rigorously learn every query and select the most suitable choice. Let’s dive into the world of Python file I/O collectively!
30+ MCQs on Python File I/O
Q1. What perform is used to open a file in Python for studying?
a) open_file()
b) read_file()
c) open()
d) learn()
Reply: c
Clarification: The open() perform is used to open a file in Python for studying.
Q2. Which mode is used to open a file for writing in Python?
a) r
b) w
c) a
d) x
Reply: b
Clarification: The ‘w’ mode is used to open a file for writing in Python. If the file doesn’t exist, it creates a brand new file. If the file exists, it truncates the file.
Q3. What perform is used to learn your complete contents of a file as a string in Python?
a) read_file()
b) read_string()
c) readlines()
d) learn()
Reply: d
Clarification: The learn() perform is used to learn your complete contents of a file as a string in Python.
This autumn. In Python, what does the readline() perform do?
a) Reads your complete file as a string
b) Reads a particular line from the file
c) Reads all of the strains from the file
d) Reads the primary line from the file
Reply: d
Clarification: The readline() perform reads the primary 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
Reply: c
Clarification: The ‘a’ mode is used to open a file for appending in Python. If the file doesn’t exist, it creates a brand new file.
Q6. What methodology is used to shut a file object in Python?
a) shut()
b) shutdown()
c) finish()
d) terminate()
Reply: a
Clarification: The shut() methodology is used to shut a file object in Python.
Q7. Which perform is used to jot down knowledge to a file in Python?
a) write()
b) append()
c) add()
d) insert()
Reply: a
Clarification: The write() perform is used to jot down knowledge to a file in Python.
Q8. What’s the objective of the with assertion when coping with file operations in Python?
a) It opens a file for studying
b) It ensures correct dealing with of sources and routinely closes the file when completed
c) It writes knowledge to a file
d) It appends knowledge to a file
Reply: b
Clarification: The with assertion ensures correct dealing with of sources and routinely closes the file when completed, stopping useful resource leaks.
Q9. Which methodology is used to test if a file exists in Python?
a) exists()
b) check_file()
c) isfile()
d) file_exists()
Reply: c
Clarification: The isfile() methodology is used to test if a file exists in Python.
Q10. What’s the distinction between the ‘r’ mode and the ‘rb’ mode when opening a file in Python?
a) There isn’t a distinction
b) The ‘r’ mode is for studying textual content information, whereas the ‘rb’ mode is for studying binary information
c) The ‘rb’ mode is for studying textual content information, whereas the ‘r’ mode is for studying binary information
d) The ‘r’ mode opens the file in read-write mode, whereas the ‘rb’ mode opens the file in read-only mode
Reply: b
Clarification: The ‘r’ mode is for studying textual content information, whereas the ‘rb’ mode is for studying binary information in Python.
Q11. Which mode is used to open a file for studying and writing in Python?
a) r
b) w
c) r+
d) w+
Reply: c
Clarification: The ‘r+’ mode is used to open a file for studying and writing in Python.
Q12. What perform is used to maneuver the file cursor to a particular place in a file in Python?
a) search()
b) move_cursor()
c) set_position()
d) place()
Reply: a
Clarification: The search() perform is used to maneuver the file cursor to a particular place in a file in Python.
Q13. What does the inform() methodology do in Python file dealing with?
a) Returns the present line quantity being learn
b) Returns the present place of the file cursor
c) Tells if the file exists or not
d) Tells the file dimension
Reply: b
Clarification: The inform() methodology returns the present place of the file cursor in Python file dealing with.
Q14. Which of the next statements is true about studying information in Python?
a) The learn() methodology reads one line at a time
b) The readline() methodology reads your complete file directly
c) The readlines() methodology reads one character at a time
d) The learn() methodology reads your complete file directly
Reply: d
Clarification: The learn() methodology reads your complete file directly in Python.
Q15. What’s the output of the next code?
file = open("knowledge.txt", "w")
file.write("Howdy, World!")
file.shut()
a) It writes “Howdy, World!” to the file knowledge.txt
b) It reads “Howdy, World!” from the file knowledge.txt
c) It appends “Howdy, World!” to the file knowledge.txt
d) It does nothing
Reply: a
Clarification: The code opens the file knowledge.txt in write mode, writes “Howdy, World!” to it, after which closes the file.
Q16. Which of the next 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’)
Reply: c
Clarification: To open a file in binary mode in Python, use the ‘rb’ mode.
Q17. What’s the objective of the os.path.isfile() perform in Python?
a) To create a brand new file
b) To test if a file exists
c) To learn the contents of a file
d) To put in writing knowledge to a file
Reply: b
Clarification: The os.path.isfile() perform is used to test if a file exists in Python.
Q18. Which methodology is used to jot down a number of strains to a file in Python?
a) writelines()
b) write_lines()
c) write_multiple_lines()
d) append_lines()
Reply: a
Clarification: The writelines() methodology is used to jot down a number of strains to a file in Python.
Q19. What occurs in case you open a file in Python utilizing the ‘x’ mode, and the file already exists?
a) It raises a FileExistsError
b) It overwrites the present file
c) It appends knowledge to the present file
d) It raises a FileNotFoundError
Reply: a
Clarification: When you open a file in Python utilizing the ‘x’ mode and the file already exists, it raises a FileExistsError.
Q20. How will you learn a JSON file in Python?
a) Utilizing the load_json() perform
b) Utilizing the read_json() perform
c) Utilizing the json.load() perform
d) Utilizing the json.learn() perform
Reply: c
Clarification: You may learn a JSON file in Python utilizing the json.load() perform.
Q21. Which methodology is used to jot down a dictionary to a JSON file in Python?
a) write_dict()
b) save_json()
c) json.dump()
d) write_json()
Reply: c
Clarification: The json.dump() methodology is used to jot down a dictionary to a JSON file in Python.
Q22. What’s the objective of the ‘a+’ mode when opening a file in Python?
a) It opens the file in append mode for studying and writing
b) It opens the file in append mode for writing solely
c) It opens the file in append mode for studying solely
d) It opens the file in append mode for studying, writing, and creating
Reply: a
Clarification: The ‘a+’ mode opens the file in append mode for studying and writing in Python.
Q23. Which module is used for studying and writing CSV information in Python?
a) os
b) csv
c) pandas
d) sys
Reply: b
Clarification: The csv module is used for studying and writing CSV information in Python.
Q24. What’s the output of the next code?
with open('knowledge.txt', 'r') as file:
print(file.learn())
a) Prints the contents of information.txt
b) Reads the contents of information.txt right into a variable
c) Raises a FileNotFoundError
d) Writes to knowledge.txt
Reply: c
Clarification: This code will increase a FileNotFoundError as a result of it tries to learn from a file that doesn’t exist.
Q25. How will you write binary knowledge to a file in Python?
a) Utilizing the write_binary() perform
b) Utilizing the binary.write() perform
c) Utilizing the write() perform with bytes as enter
d) Utilizing the binary_write() perform
Reply: c
Clarification: You may write binary knowledge to a file in Python utilizing the write() perform with bytes as enter.
Q26. What does the ‘rb+’ mode do when opening a file in Python?
a) Opens the file for studying and writing in binary mode
b) Opens the file for studying and writing, creating the file if it doesn’t exist
c) Opens the file for studying in binary mode
d) Opens the file for studying and writing, truncating the file to zero size
Reply: a
Clarification: The ‘rb+’ mode opens the file for studying and writing in binary mode in Python.
Q27. Which methodology is used to learn CSV information in Python?
a) read_csv()
b) learn()
c) csv_read()
d) csv.reader()
Reply: d
Clarification: The csv.reader() methodology is used to learn CSV information in Python.
Q28. What does the ‘wb’ mode do when opening a file in Python?
a) Opens the file for studying and writing in binary mode
b) Opens the file for writing in binary mode
c) Opens the file for studying in binary mode
d) Opens the file for studying and writing, truncating the file to zero size
Reply: b
Clarification: The ‘wb’ mode opens the file for writing in binary mode in Python.
Q29. How do you learn solely the primary n characters from a file in Python?
a) Utilizing the learn(n) methodology
b) Utilizing the readlines(n) methodology
c) Utilizing the readline(n) methodology
d) Utilizing the read_first(n) methodology
Reply: a
Clarification: You may learn solely the primary n characters from a file in Python utilizing the learn(n) methodology.
Q30. What’s the objective of the os module in Python file dealing with?
a) To create a brand new file
b) To learn the contents of a file
c) To handle information and directories
d) To put in writing knowledge to a file
Reply: c
Clarification: The os module in Python is used to handle information and directories.
Q31. What is going to the next Python code snippet do?
with open("existing_data.txt", "a") as file:
file.write("New knowledge")
a) It appends “New knowledge” to the file existing_data.txt
b) It reads “New knowledge” from the file existing_data.txt
c) It writes “New knowledge” to the file existing_data.txt
d) It overwrites the file existing_data.txt with “New knowledge”
Reply: a
Clarification: The code appends “New knowledge” to the file existing_data.txt.
Q32. What is going to the next Python code do?
with open("output.txt", "w") as file:
file.write("Howdy, World!")
a) It reads “Howdy, World!” from the file output.txt
b) It appends “Howdy, World!” to the file output.txt
c) It writes “Howdy, World!” to the file output.txt
d) It does nothing
Reply: c
Clarification: The code writes “Howdy, World!” to the file output.txt.
Congratulations on finishing the Python File I/O MCQ Quiz! File enter/output operations are basic to many programming duties, and Python provides strong options to deal with them successfully. By mastering file I/O in Python, you achieve the flexibility to control knowledge saved in information, work with totally different file codecs, and construct highly effective functions that learn from and write to exterior information. Preserve working towards and exploring Python’s file I/O functionalities to grow to be proficient in dealing with information inside your applications. When you have any questions or need to delve deeper into any subject, don’t hesitate to proceed your studying journey. Completely happy coding!
You can even enroll in out free Python Course Right now!
Learn our extra articles associated to MCQs in Python: