Python CSV Files MCQs | 25 Practice Questions with Answers | CBSE Class 12 Computer Science
Class 12 · Computer Science
Python CSV Files MCQs (10 Questions)
Practice these multiple-choice questions to strengthen your understanding of Python CSV Files. Click Show Answer to reveal the correct answer and explanation.
Q1. What does CSV stand for?
A. Common Separated Values
B. Comma Separated Values
C. Computer Separated Values
D. Character Separated Values
Show Answer
Answer: B. Comma Separated Values
Explanation: CSV stands for Comma Separated Values, a popular file format used to store tabular data.
Q2. Which module is used to work with CSV files in Python?
A. pickle
B. csv
C. random
D. os
Show Answer
Answer: B. csv
Explanation: The csv module provides functions to read and write CSV files.
Q3. Which statement is used to import the CSV module?
A. include csv
B. using csv
C. import csv
D. open csv
Show Answer
Answer: C. import csv
Explanation: The import csv statement imports the CSV module.
Q4. Which function is used to open a CSV file?
A. csv.open()
B. file.open()
C. open()
D. csv.file()
Show Answer
Answer: C. open()
Explanation: CSV files are opened using the built-in open() function.
Q5. Which method is used to close a CSV file?
A. end()
B. stop()
C. close()
D. exit()
Show Answer
Answer: C. close()
Explanation: The close() method closes the CSV file and releases system resources.
Q6. Which function creates a CSV writer object?
A. csv.reader()
B. csv.writer()
C. csv.writerow()
D. csv.write()
Show Answer
Answer: B. csv.writer()
Explanation: The csv.writer() function creates a writer object used to write data into a CSV file.
Q7. Which method writes a single row into a CSV file?
A. writerows()
B. writerow()
C. write()
D. read()
Show Answer
Answer: B. writerow()
Explanation: The writerow() method writes one record (one row) into the CSV file.
Q8. Which method writes multiple rows into a CSV file?
A. writerows()
B. writerow()
C. write()
D. writelines()
Show Answer
Answer: A. writerows()
Explanation: The writerows() method writes multiple rows from a list of records.
Q9. Which function is used to create a CSV reader object?
A. csv.writer()
B. csv.reader()
C. csv.read()
D. csv.load()
Show Answer
Answer: B. csv.reader()
Explanation: The csv.reader() function creates a reader object for reading CSV files.
Q10. Which statement about CSV files is correct?
A. CSV files store tabular data.
B. Each row represents one record.
C. Values are generally separated by commas.
D. All of these.
Show Answer
Answer: D. All of these.
Explanation: CSV files store tabular data where each row is a record and values are generally separated by commas.
Q11. Which method writes one record to a CSV file?
A. writerows()
B. writerow()
C. write()
D. append()
Show Answer
Answer: B. writerow()
Explanation: The writerow() method writes a single row (record) to a CSV file.
Q12. Which method writes multiple records to a CSV file?
A. writerow()
B. writerows()
C. write()
D. writelines()
Show Answer
Answer: B. writerows()
Explanation: The writerows() method writes multiple rows from a list of records.
Q13. Which function is used to read records from a CSV file?
A. csv.writer()
B. csv.writerow()
C. csv.reader()
D. csv.read()
Show Answer
Answer: C. csv.reader()
Explanation: The csv.reader() function creates a reader object for reading CSV records.
Q14. What will be the output of the following code?
import csv
f=open("student.csv","w",newline="")
w=csv.writer(f)
w.writerow(["101","Aman",95])
f.close()
print("Done")
A. Error
B. Done
C. 101 Aman 95
D. Nothing
Show Answer
Answer: B. Done
Explanation: The record is successfully written to the CSV file, and the program prints Done.
Q15. Which argument is commonly used with open() while writing CSV files to avoid blank lines on Windows?
A. end=""
B. sep=","
C. newline=""
D. encoding="utf-8"
Show Answer
Answer: C. newline=""
Explanation: The newline="" argument prevents extra blank lines while writing CSV files, especially on Windows systems.
Q16. What will be the output of the following code?
import csv
f=open("student.csv","r")
r=csv.reader(f)
for row in r:
print(row)
f.close()
A. Each record is displayed as a list.
B. Error
C. Only the first record is displayed.
D. Nothing is displayed.
Show Answer
Answer: A.
Explanation: The csv.reader() object returns each record as a list of values.
Q17. Which statement is true about csv.reader()?
A. It writes records to a CSV file.
B. It returns one row at a time during iteration.
C. It creates a binary file.
D. It deletes all records from the file.
Show Answer
Answer: B.
Explanation: The csv.reader() object is iterable and returns one row at a time.
Q18. Which statement is true about writerow()?
A. It writes one record at a time.
B. It writes multiple records at a time.
C. It reads one record at a time.
D. It closes the CSV file.
Show Answer
Answer: A.
Explanation: The writerow() method writes exactly one row into the CSV file.
Q19. Which statement is true about writerows()?
A. It writes only one record.
B. It writes multiple records from a sequence.
C. It reads all records.
D. It opens the CSV file.
Show Answer
Answer: B.
Explanation: The writerows() method writes multiple records stored in a list or other iterable.
Q20. Which of the following is the correct sequence for writing data into a CSV file?
A. Open file → Create writer object → Write rows → Close file
B. Create writer object → Open file → Close file
C. Open file → Read file → Close file
D. Import pickle → dump() → Close file
Show Answer
Answer: A.
Explanation: First open the file, create a writer object, write records using writerow() or writerows(), and finally close the file.
Q21. What will be the output of the following code?
import csv
f=open("student.csv","w",newline="")
w=csv.writer(f)
w.writerows([
["101","Aman",95],
["102","Riya",92]
])
f.close()
print("Records Written")
A. Error
B. Records Written
C. Two records are displayed on the screen.
D. Nothing
Show Answer
Answer: B. Records Written
Explanation: The writerows() method writes both records to the CSV file, and the program prints Records Written.
Q22. Which object is created by the csv.writer() function?
A. Reader object
B. File object
C. Writer object
D. Binary object
Show Answer
Answer: C. Writer object
Explanation: The csv.writer() function creates a writer object used to write records into a CSV file.
Q23. Which object is created by the csv.reader() function?
A. File object
B. Reader object
C. Writer object
D. Pickle object
Show Answer
Answer: B. Reader object
Explanation: The csv.reader() function creates a reader object used to read records from a CSV file.
Q24. Which mode is generally used while reading a CSV file?
A. w
B. a
C. r
D. wb
Show Answer
Answer: C. r
Explanation: CSV files are generally opened in r mode for reading records.
Q25. Which statement about Python CSV file handling is correct?
A. The csv module is required to work with CSV files.
B. writerow() writes a single record.
C. writerows() writes multiple records.
D. All of these.
Show Answer
Answer: D. All of these.
Explanation: Python uses the csv module to work with CSV files. The writerow() method writes one record, while writerows() writes multiple records.
Answer Key
| Q.No. | Answer | Q.No. | Answer | Q.No. | Answer |
|---|---|---|---|---|---|
| 1 | B | 10 | D | 19 | B |
| 2 | B | 11 | B | 20 | A |
| 3 | C | 12 | B | 21 | B |
| 4 | C | 13 | C | 22 | C |
| 5 | C | 14 | B | 23 | B |
| 6 | B | 15 | C | 24 | C |
| 7 | B | 16 | A | 25 | D |
| 8 | A | 17 | B | ||
| 9 | B | 18 | A |
Practice Tips
- Always import the
csvmodule before working with CSV files. - Remember to use
newline=""while opening a CSV file for writing to avoid blank lines, especially on Windows. - Revise the difference between:
csv.writer()– Creates a writer object.writerow()– Writes a single record.writerows()– Writes multiple records.csv.reader()– Creates a reader object.
- Remember that
csv.reader()returns each row as a list of values. - Practice both reading and writing operations using CSV files, as they are frequently tested in practical examinations.
- Always close the CSV file after completing operations or use the
withstatement for automatic file closure. - Practice output-based questions involving
writerow(),writerows(), andreader(), as they are common in CBSE board examinations.