Python Programming •
MCQ • Files
Files
Q1. A file is used to:
A) Store data permanently
B) Run loops
C) Create functions
D) Perform calculations only
Answer: A) Store data permanently
Explanation:
Files are used to store data permanently so it can be used later.
Q2. Which function is used to open a file in Python?
A) file()
B) open()
C) read()
D) create()
Answer: B) open()
Explanation:
open() is used to open or create a file.
Example: f = open("data.txt", "r")
Q3. Which mode is used to read a file?
A) w
B) a
C) r
D) x
Answer: C) r
Explanation:
r stands for read mode.
Q4. Which mode is used to write a file?
A) r
B) w
C) a
D) rb
Answer: B) w
Explanation:
w stands for write mode and overwrites old content.
Q5. Which mode is used to append data?
A) r
B) w
C) a
D) x
Answer: C) a
Explanation:
a adds new data at the end of the file.
Q6. Which mode is used for binary file reading?
A) rb
B) wb
C) ab
D) br
Answer: A) rb
Explanation:
rb means read binary mode.
Q7. Which method reads the entire file?
A) read()
B) readline()
C) readlines()
D) fetch()
Answer: A) read()
Explanation:
read() reads all content from the file.
Q8. Which method reads one line at a time?
A) read()
B) readline()
C) readlines()
D) get()
Answer: B) readline()
Explanation:
readline() reads a single line from the file.
Q9. Which method reads all lines as a list?
A) read()
B) readline()
C) readlines()
D) lines()
Answer: C) readlines()
Explanation:
readlines() returns all lines as list items.
Q10. Which method writes data into a file?
A) write()
B) print()
C) append()
D) insert()
Answer: A) write()
Explanation:
write() is used to store text inside a file.
Q11. Which method closes a file?
A) stop()
B) close()
C) end()
D) finish()
Answer: B) close()
Explanation:
close() releases system resources after file operations.
Q12. Which statement is best practice for file handling?
A) Never close file
B) Always close file
C) Use only append
D) Use only binary
Answer: B) Always close file
Explanation:
Closing files prevents data loss and resource leakage.
Q13. What happens if file is opened in w mode?
A) Old content is deleted
B) File becomes read-only
C) Error occurs
D) File closes automatically
Answer: A) Old content is deleted
Explanation:
Write mode overwrites existing file content.
Q14. Which module is used for binary object storage?
A) csv
B) pickle
C) math
D) os
Answer: B) pickle
Explanation:
pickle is used to store Python objects in binary form.
Q15. Which function is used in pickle for writing?
A) dump()
B) load()
C) save()
D) write()
Answer: A) dump()
Explanation:
pickle.dump() writes objects into binary files.
Q16. Which function is used in pickle for reading?
A) dump()
B) load()
C) fetch()
D) read()
Answer: B) load()
Explanation:
pickle.load() reads objects from binary files.
Q17. CSV stands for:
A) Common Separated Value
B) Comma Separated Value
C) Character Stored Value
D) Column Saved Value
Answer: B) Comma Separated Value
Explanation:
CSV files store data separated by commas.
Q18. Which module is used for CSV files?
A) os
B) csv
C) math
D) pickle
Answer: B) csv
Explanation:
The csv module helps read and write CSV files.
Q19. Which module is used for operating system tasks?
A) os
B) csv
C) random
D) math
Answer: A) os
Explanation:
os module provides system-related operations.
Q20. Which module is used for path handling?
A) math
B) os.path
C) csv
D) pickle
Answer: B) os.path
Explanation:
os.path is used for checking paths, files, and directories.
Q21. Which function checks if a file exists?
A) os.path.exists()
B) os.path.check()
C) os.file()
D) file.exists()
Answer: A) os.path.exists()
Explanation:
This function returns True if file exists.
Q22. Which function creates a new directory?
A) os.mkdir()
B) os.create()
C) os.folder()
D) os.make()
Answer: A) os.mkdir()
Explanation:
os.mkdir() creates a new folder.
Q23. Which function removes a file?
A) os.delete()
B) os.remove()
C) os.erase()
D) os.clear()
Answer: B) os.remove()
Explanation:
os.remove() deletes a file.
Q24. Binary files store:
A) Only text
B) Data in binary form
C) Only CSV
D) Only numbers
Answer: B) Data in binary form
Explanation:
Binary files store data in machine-readable binary format.
Q25. Text files store:
A) Human-readable characters
B) Only images
C) Only objects
D) Only videos
Answer: A) Human-readable characters
Explanation:
Text files store readable text like .txt
Q26. Which statement is correct?
A) r mode writes file
B) w mode reads file
C) a mode appends data
D) rb is text mode
Answer: C) a mode appends data
Explanation:
Append mode adds data without deleting old content.
Q27. What does with open() help with?
A) Automatic file closing
B) Loop creation
C) Function definition
D) String formatting
Answer: A) Automatic file closing
Explanation:
with open() automatically closes the file after use.
Q28. Which statement is true for pickle?
A) Used for text formatting
B) Used for object serialization
C) Used for loops
D) Used for conditions
Answer: B) Used for object serialization
Explanation:
Pickle converts Python objects into binary stream.
Q29. Which file type is best for table-like data?
A) CSV
B) TXT
C) Binary
D) EXE
Answer: A) CSV
Explanation:
CSV is commonly used for rows and columns of data.
Q30. Which statement is correct?
A) Files help permanent storage
B) Files are temporary only
C) Python has no file handling
D) CSV is not supported in Python
Answer: A) Files help permanent storage
Explanation:
Files are mainly used for permanent data storage and retrieval.
Google AdSense Ad Placement Here 📢