🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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?
Answer:
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?
Answer:
Explanation:
The "r" mode is used for reading from a file.
3. How do you open a file for writing in Python?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The close() method of a file object is used to close the file.
7. What does the 'b' in 'rb' or 'wb' signify?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The readline() method reads a single line from the file.
10. What does file.readlines() do?
Answer:
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?
Answer:
Explanation:
Both write() and writelines() methods can be used to write to a file.
12. How can you delete a file in Python?
Answer:
Explanation:
The os.remove() function from the os module is used to delete a file.
13. What is the use of the flush() method?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The tell() method returns the current position of the file cursor.
18. Which function is used to rename a file in Python?
Answer:
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?
Answer:
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?
Answer:
Explanation:
Opening a non-existent file in read mode ('r') will raise an IOError or FileNotFoundError.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment