Welcome to the Python File I/O MCQ Quiz! File enter/output (I/O) is a vital side of programming, permitting information to be learn from and written to information on disk. Python supplies highly effective built-in features 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 appropriate reply. Take your time to fastidiously learn every query and select the best choice. Let’s dive into the world of Python file I/O collectively!
30+ MCQs on Python File I/O
Q1. What operate is used to open a file in Python for studying?
a) open_file()
b) read_file()
c) open()
d) learn()
Reply: c
Rationalization: The open() operate 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
Rationalization: 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 operate is used to learn your entire contents of a file as a string in Python?
a) read_file()
b) read_string()
c) readlines()
d) learn()
Reply: d
Rationalization: The learn() operate is used to learn your entire contents of a file as a string in Python.
This fall. In Python, what does the readline() operate do?
a) Reads your entire file as a string
b) Reads a selected line from the file
c) Reads all of the strains from the file
d) Reads the primary line from the file
Reply: d
Rationalization: The readline() operate 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
Rationalization: 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
Rationalization: The shut() methodology is used to shut a file object in Python.
Q7. Which operate is used to jot down information to a file in Python?
a) write()
b) append()
c) add()
d) insert()
Reply: a
Rationalization: The write() operate is used to jot down information 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 robotically closes the file when achieved
c) It writes information to a file
d) It appends information to a file
Reply: b
Rationalization: The with assertion ensures correct dealing with of sources and robotically closes the file when achieved, stopping useful resource leaks.
Q9. Which methodology is used to examine if a file exists in Python?
a) exists()
b) check_file()
c) isfile()
d) file_exists()
Reply: c
Rationalization: The isfile() methodology is used to examine 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 is no such thing as 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
Rationalization: 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
Rationalization: The ‘r+’ mode is used to open a file for studying and writing in Python.
Q12. What operate is used to maneuver the file cursor to a selected place in a file in Python?
a) search()
b) move_cursor()
c) set_position()
d) place()
Reply: a
Rationalization: The search() operate is used to maneuver the file cursor to a selected 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 measurement
Reply: b
Rationalization: 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 entire file directly
c) The readlines() methodology reads one character at a time
d) The learn() methodology reads your entire file directly
Reply: d
Rationalization: The learn() methodology reads your entire file directly in Python.
Q15. What’s the output of the next code?
file = open("information.txt", "w")
file.write("Howdy, World!")
file.shut()
a) It writes “Howdy, World!” to the file information.txt
b) It reads “Howdy, World!” from the file information.txt
c) It appends “Howdy, World!” to the file information.txt
d) It does nothing
Reply: a
Rationalization: The code opens the file information.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
Rationalization: To open a file in binary mode in Python, use the ‘rb’ mode.
Q17. What’s the objective of the os.path.isfile() operate in Python?
a) To create a brand new file
b) To examine if a file exists
c) To learn the contents of a file
d) To put in writing information to a file
Reply: b
Rationalization: The os.path.isfile() operate is used to examine 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
Rationalization: The writelines() methodology is used to jot down a number of strains to a file in Python.
Q19. What occurs when 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 information to the present file
d) It raises a FileNotFoundError
Reply: a
Rationalization: Should you open a file in Python utilizing the ‘x’ mode and the file already exists, it raises a FileExistsError.
Q20. How are you going to learn a JSON file in Python?
a) Utilizing the load_json() operate
b) Utilizing the read_json() operate
c) Utilizing the json.load() operate
d) Utilizing the json.learn() operate
Reply: c
Rationalization: You’ll be able to learn a JSON file in Python utilizing the json.load() operate.
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
Rationalization: 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
Rationalization: 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
Rationalization: The csv module is used for studying and writing CSV information in Python.
Q24. What’s the output of the next code?
with open('information.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 information.txt
Reply: c
Rationalization: This code will increase a FileNotFoundError as a result of it tries to learn from a file that doesn’t exist.
Q25. How are you going to write binary information to a file in Python?
a) Utilizing the write_binary() operate
b) Utilizing the binary.write() operate
c) Utilizing the write() operate with bytes as enter
d) Utilizing the binary_write() operate
Reply: c
Rationalization: You’ll be able to write binary information to a file in Python utilizing the write() operate 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
Rationalization: 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
Rationalization: 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
Rationalization: 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
Rationalization: You’ll be able to 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 information to a file
Reply: c
Rationalization: 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 information")
a) It appends “New information” to the file existing_data.txt
b) It reads “New information” from the file existing_data.txt
c) It writes “New information” to the file existing_data.txt
d) It overwrites the file existing_data.txt with “New information”
Reply: a
Rationalization: The code appends “New information” 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
Rationalization: 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 presents strong options to deal with them successfully. By mastering file I/O in Python, you acquire the flexibility to control information saved in information, work with completely different file codecs, and construct highly effective functions that learn from and write to exterior information. Maintain practising and exploring Python’s file I/O functionalities to develop into proficient in dealing with information inside your applications. You probably have any questions or need to delve deeper into any matter, don’t hesitate to proceed your studying journey. Glad coding!
It’s also possible to enroll in out free Python Course At the moment!
Learn our extra articles associated to MCQs in Python: