Quizzes

Python File Handling Quiz


Welcome to the Python File Handling Quiz!

File handling is an essential concept in Python programming that allows reading, writing, and manipulating files on a computer. This quiz will test your understanding of file operations, modes, and best practices.

Try to answer the following 20 questions to evaluate your knowledge!

Table of Contents:


File Handling Quiz


Quiz Questions

1. Which function is used to open a file in Python?
A. open()
B. read()
C. file()
D. write()

2. What mode is used to open a file for reading only?
A. “r”
B. “w”
C. “a”
D. “x”

3. What happens if you open a file in “w” mode and the file already exists?
A. It appends to the file
B. It creates a new file
C. It deletes the file content and writes new content
D. It throws an error

4. Which mode is used to append data to an existing file?
A. “a”
B. “w”
C. “r”
D. “x”

5. How do you properly close a file after opening it?
A. close()
B. end()
C. stop()
D. exit()

6. Which statement is used to automatically close a file after working with it?
A. with open()
B. try-except
C. auto_close()
D. shut()

7. Which function is used to read the entire content of a file?
A. read()
B. readline()
C. readlines()
D. fetch()

8. What does readline() do?

A. Reads a single line from the file
B. Reads the entire file
C. Writes to the file
D. Deletes the file

9. How do you write a list of strings to a file?
A. write()
B. writelines()
C. write_all()
D. append()

10. What does “x” mode do when opening a file?
A. Opens a file for exclusive creation
B. Opens a file for reading
C. Opens a file for appending
D. Opens a file and deletes its content

11. What function is used to check if a file exists before opening it?
A. exists()
B. isfile()
C. os.path.exists()
D. file_check()

12. What will happen if you try to open a non-existent file in “r” mode?
A. It creates the file
B. It throws an error
C. It writes an empty file
D. It appends to a new file

13. Which module is used to work with file paths?
A. sys
B. os
C. path
D. file

14. How do you delete a file in Python?

A. delete()
B. remove()
C. erase()
D. del()

15. What is the default mode when opening a file using open()?
A. “w”
B. “r”
C. “a”
D. “x”

16. What does “rb” mode mean?
A. Read binary
B. Read buffer
C. Read bytes
D. Read binary and text

17. What does seek() do in file handling?
A. Moves the file pointer to a specific position
B. Closes the file
C. Deletes the file
D. Opens a new file

18. Which function is used to rename a file?
A. rename()
B. change_name()
C. move()
D. rename_file()

19. What is the use of tell() function?
A. Returns the current position of the file pointer
B. Reads a line from the file
C. Writes data to the file
D. Deletes file content

20. How do you ensure that file changes are saved?
A. By closing the file
B. By using save()
C. By using apply()
D. By using commit()


How did you do? 🎯

  • 18-20 correct → 🏆 Excellent! You’re a Python functions pro!
  • 14-17 correct → 👍 Great job! Keep practicing.
  • 10-13 correct → 🙂 Good, but there’s room for improvement.
  • Below 10 → 🤔 No worries! Review the concepts and try again.

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.