1.9 C
New York
Tuesday, February 20, 2024

30+ MCQs on Python File I/O


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 offers 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 fastidiously learn every query and select the most suitable choice. Let’s dive into the world of Python file I/O collectively!

Python File I/O

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 entire 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 entire contents of a file as a string in Python.

This fall. In Python, what does the readline() perform do?

a) Reads your entire 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 technique is used to shut a file object in Python?

a) shut()

b) shutdown()

c) finish()

d) terminate()

Reply: a

Clarification: The shut() technique is used to shut a file object in Python.

Q7. Which perform is used to put in writing information to a file in Python?

a) write()

b) append()

c) add()

d) insert()

Reply: a

Clarification: The write() perform is used to put in writing information to a file in Python.

Q8. What’s the function 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 assets and routinely closes the file when carried out

c) It writes information to a file

d) It appends information to a file

Reply: b

Clarification: The with assertion ensures correct dealing with of assets and routinely closes the file when carried out, stopping useful resource leaks.

Q9. Which technique is used to verify if a file exists in Python?

a) exists()

b) check_file()

c) isfile()

d) file_exists()

Reply: c

Clarification: The isfile() technique is used to verify 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

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() technique 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

Clarification: The inform() technique 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() technique reads one line at a time

b) The readline() technique reads your entire file directly

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

d) The learn() technique reads your entire file directly

Reply: d

Clarification: The learn() technique reads your entire file directly in Python.

Q15. What’s the output of the next code?

file = open("information.txt", "w")
file.write("Hiya, World!")
file.shut()

a) It writes “Hiya, World!” to the file information.txt

b) It reads “Hiya, World!” from the file information.txt

c) It appends “Hiya, World!” to the file information.txt

d) It does nothing

Reply: a

Clarification: The code opens the file information.txt in write mode, writes “Hiya, 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 function of the os.path.isfile() perform in Python?

a) To create a brand new file

b) To verify if a file exists

c) To learn the contents of a file

d) To write down information to a file

Reply: b

Clarification: The os.path.isfile() perform is used to verify if a file exists in Python.

Q18. Which technique is used to put in writing 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() technique is used to put in writing a number of strains to a file in Python.

Q19. What occurs for those who 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

Clarification: When 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() 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 technique is used to put in writing 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() technique is used to put in writing a dictionary to a JSON file in Python.

Q22. What’s the function 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('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

Clarification: 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() 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 information 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 technique 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() technique 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) technique

b) Utilizing the readlines(n) technique

c) Utilizing the readline(n) technique

d) Utilizing the read_first(n) technique

Reply: a

Clarification: You may learn solely the primary n characters from a file in Python utilizing the learn(n) technique.

Q30. What’s the function 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 write down information to a file

Reply: c

Clarification: The os module in Python is used to handle information and directories.

Q31. What’s 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

Clarification: The code appends “New information” to the file existing_data.txt.

Q32. What’s going to the next Python code do?

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

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

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

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

d) It does nothing

Reply: c

Clarification: The code writes “Hiya, 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 gives sturdy options to deal with them successfully. By mastering file I/O in Python, you acquire the power to govern information saved in information, work with completely different file codecs, and construct highly effective functions that learn from and write to exterior information. Hold training and exploring Python’s file I/O functionalities to grow to be proficient in dealing with information inside your packages. In case you have any questions or wish to delve deeper into any matter, don’t hesitate to proceed your studying journey. Completely satisfied coding!

It’s also possible to enroll in out free Python Course Immediately!

Learn our extra articles associated to MCQs in Python:



Supply hyperlink

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles