Python File handling Quiz - MCQ Questions and Answers

Welcome to our "Python File Handling Quiz - MCQ Questions and Answers" blog post! If you're looking to test your knowledge and sharpen your skills in Python's file-handling capabilities, you've come to the right place. This quiz offers a selection of 20 multiple-choice questions, ranging from basic to more advanced topics, designed to challenge both beginners and seasoned Python developers. 

Whether you're preparing for an interview, an exam, or just looking to refresh your understanding of Python's file operations, these questions will provide a comprehensive review. Get ready to navigate through file modes, methods, and best practices in Python file handling. Let the quiz begin!

1. What is the correct function to open a file in Python?

a) file.open()
b) open.file()
c) open()
d) File()

Answer:

c) open()

Explanation:

In Python, the open() function is used to open a file in either text or binary mode.

2. Which mode do you use to read a file in Python?

a) "r"
b) "w"
c) "a"
d) "rb"

Answer:

a) "r"

Explanation:

The "r" mode is used for reading from a file.

3. How do you open a file for writing in Python?

a) open("file.txt", "w")
b) open("file.txt", "r")
c) open("file.txt", "a")
d) open("file.txt", "rb")

Answer:

a) open("file.txt", "w")

Explanation:

The "w" mode opens a file for writing. It creates a new file if it does not exist or truncates the file if it exists.

4. What does the 'a' mode do in file handling?

a) Appends to the end of the file
b) Reads from the file
c) Writes to the file
d) None of the above

Answer:

a) Appends to the end of the file

Explanation:

The 'a' mode opens the file for appending. Any data written to the file is automatically added to the end.

5. What does the 'x' mode do in file handling?

a) Deletes the file
b) Reads from the file
c) Creates a new file
d) Appends to the file

Answer:

c) Creates a new file

Explanation:

The 'x' mode creates a new file and opens it for writing. If the file already exists, the operation fails.

6. How do you close a file in Python?

a) file.close()
b) close.file()
c) close()
d) File.close()

Answer:

a) file.close()

Explanation:

The close() method of a file object is used to close the file.

7. What does the 'b' in 'rb' or 'wb' signify?

a) Binary mode
b) Byte mode
c) Both a and b
d) None of the above

Answer:

a) Binary mode

Explanation:

The 'b' in 'rb' or 'wb' signifies that the file is opened in binary mode.

8. What is the purpose of the seek() method?

a) Deletes the specified number of characters from the file
b) Closes the file
c) Changes the file's current position
d) Reads a specific line

Answer:

c) Changes the file's current position

Explanation:

The seek() method sets the file's current position at the offset.

9. How do you read a single line from a file in Python?

a) file.readline()
b) file.readlines()
c) read.fileline()
d) file.read()

Answer:

a) file.readline()

Explanation:

The readline() method reads a single line from the file.

10. What does file.readlines() do?

a) Reads the entire file
b) Reads the first line of the file
c) Reads all the lines into a list
d) None of the above

Answer:

c) Reads all the lines into a list

Explanation:

The readlines() method reads all the lines of a file into a list.

11. What is the correct way to write to a file in Python?

a) file.write()
b) file.writelines()
c) Both a and b
d) None of the above

Answer:

c) Both a and b

Explanation:

Both write() and writelines() methods can be used to write to a file.

12. How can you delete a file in Python?

a) Use the delete() method
b) Use the os.remove() function
c) Use the file.delete() method
d) None of the above

Answer:

b) Use the os.remove() function

Explanation:

The os.remove() function from the os module is used to delete a file.

13. What is the use of the flush() method?

a) Flushes the internal buffer
b) Closes the file
c) Deletes the file content
d) Reads the file content

Answer:

a) Flushes the internal buffer

Explanation:

The flush() method is used to flush the internal buffer, like writing the data to the file.

14. What is the difference between 'w+' and 'r+' modes?

a) 'w+' can read and write, 'r+' can only read
b) 'w+' truncates the file, 'r+' does not
c) 'r+' can read and write, 'w+' can only write
d) Both are same

Answer:

b) 'w+' truncates the file, 'r+' does not

Explanation:

The 'w+' mode opens the file for reading and writing, truncating the file first. 'r+' opens the file for reading and writing without truncating.

15. How do you check if a file is closed in Python?

a) file.is_closed
b) file.closed
c) is_closed(file)
d) file.isclosed()

Answer:

b) file.closed

Explanation:

The closed attribute of a file object is used to check if a file is closed.

16. What is the purpose of the 'with' statement in file handling in Python?

a) Improves readability
b) Automatically closes the file
c) Handles exceptions
d) Both b and c

Answer:

d) Both b and c

Explanation:

The 'with' statement simplifies exception handling by encapsulating common preparation and cleanup tasks in file handling. It also automatically closes the file.

17. What does the tell() method do?

a) Tells the name of the file
b) Returns the current position of the file cursor
c) Tells the size of the file
d) None of the above

Answer:

b) Returns the current position of the file cursor

Explanation:

The tell() method returns the current position of the file cursor.

18. Which function is used to rename a file in Python?

a) os.rename()
b) os.renameto()
c) file.rename()
d) rename.file()

Answer:

a) os.rename()

Explanation:

The os.rename() function is used to rename a file.

19. How do you open a file for both reading and writing without truncating it?

a) open("file.txt", "r+")
b) open("file.txt", "w+")
c) open("file.txt", "a+")
d) open("file.txt", "rw")

Answer:

a) open("file.txt", "r+")

Explanation:

The "r+" mode opens the file for both reading and writing without truncating it.

20. What will happen if you try to open a file that doesn't exist in read mode?

a) A new file is created
b) The file is opened in write mode instead
c) An error is raised
d) Nothing happens

Answer:

c) An error is raised

Explanation:

Opening a non-existent file in read mode ('r') will raise an IOError or FileNotFoundError.

Comments