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:
- Python quizzes for beginners series
File Handling Quiz
Quiz Questions
1. Which function is used to open a file in Python?
A. open()
B. read()
C. file()
D. write()
Answer
A. open()
2. What mode is used to open a file for reading only?
A. “r”
B. “w”
C. “a”
D. “x”
Answer
A. “r”
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
Answer
C. It deletes the file content and writes new content
4. Which mode is used to append data to an existing file?
A. “a”
B. “w”
C. “r”
D. “x”
Answer
A. “a”
5. How do you properly close a file after opening it?
A. close()
B. end()
C. stop()
D. exit()
Answer
A. close()
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()
Answer
A. with open()
7. Which function is used to read the entire content of a file?
A. read()
B. readline()
C. readlines()
D. fetch()
Answer
A. read()
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
Answer
A. Reads a single line from the file
9. How do you write a list of strings to a file?
A. write()
B. writelines()
C. write_all()
D. append()
Answer
B. writelines()
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
Answer
A. Opens a file for exclusive creation
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()
Answer
C. os.path.exists()
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
Answer
B. It throws an error
13. Which module is used to work with file paths?
A. sys
B. os
C. path
D. file
Answer
B. os
14. How do you delete a file in Python?
A. delete()
B. remove()
C. erase()
D. del()
Answer
B. remove()
15. What is the default mode when opening a file using open()?
A. “w”
B. “r”
C. “a”
D. “x”
Answer
B. “r”
16. What does “rb” mode mean?
A. Read binary
B. Read buffer
C. Read bytes
D. Read binary and text
Answer
A. Read binary
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
Answer
A. Moves the file pointer to a specific position
18. Which function is used to rename a file?
A. rename()
B. change_name()
C. move()
D. rename_file()
Answer
A. rename()
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
Answer
A. Returns the current position of the file pointer
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()
Answer
A. By closing the file
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.