AMP

Chapter-2 File Handling in Python — Online MCQ Test

COMPUTER SCIENCE · Grade 12 · CBSE(NCERT)
Practice Chapter-2 File Handling in Python with a free chapter-wise online MCQ test. This chapter covers: text files (.txt) - binary files (.dat) - pickle module - CSV files (.csv) - file reading - file writing. AI-generated questions from basic to board-exam level, with instant results and explanations.

Start Exam on Full Site →

Chapter-2 File Handling in Python — Important Questions & Answers

Which of the following is the correct extension for a text file in Python?
  • A. .txt
  • B. .dat
  • C. .csv
  • D. .py
Answer: A. .txt
Text files in Python use the .txt extension to store plain text data.
What does the 'r' mode do when opening a file in Python?
  • A. Write mode
  • B. Read mode
  • C. Append mode
  • D. Binary mode
Answer: B. Read mode
The 'r' mode opens a file for reading existing content without modifying it.
When you open a file in 'w' mode, what happens if the file already exists?
  • A. It appends to the existing file
  • B. It overwrites the existing file
  • C. It creates a new version of the file
  • D. It raises an error
Answer: B. It overwrites the existing file
Write mode ('w') truncates and overwrites any existing file with the same name.
Analyze this code: What will be the output?\nwith open('test.txt', 'w') as f:\n f.write('Hello')\nwith open('test.txt', 'r') as f:\n print(f.read())
  • A. Hello
  • B. test.txt
  • C. None
  • D. An error occurs
Answer: A. Hello
The code writes 'Hello' to the file, then reads and prints it. The with statement ensures proper file handling.
TRICKY: What will happen if you try to pickle an open file object?
  • A. It will successfully pickle the file path
  • B. It will raise a TypeError because file objects cannot be pickled
  • C. It will pickle the file contents
  • D. It will create a copy of the file
Answer: B. It will raise a TypeError because file objects cannot be pickled
File objects, along with lambda functions and some built-ins, cannot be pickled as they depend on runtime state.