Python csv: 40 MCQs
All 40 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 21 questions
Q1. Which of the following is the correct way to open a file for writing with the csv module?
BeginnerAnswers and explanations are in the Veda app.
Q2. What does `csv.reader` return when you iterate over it?
BeginnerAnswers and explanations are in the Veda app.
Q3. Which of the following is a benefit of using `csv.DictReader` over `csv.reader`?
BeginnerAnswers and explanations are in the Veda app.
Q4. What happens if you try to parse a CSV file with simple string splitting on commas?
BeginnerAnswers and explanations are in the Veda app.
Q5. How do you read a CSV file that uses semicolons as delimiters?
BeginnerAnswers and explanations are in the Veda app.
Q6. What is the purpose of `io.StringIO` when working with the csv module?
BeginnerAnswers and explanations are in the Veda app.
Q7. When using `csv.DictWriter`, what must you provide to define the column order?
BeginnerAnswers and explanations are in the Veda app.
Q8. What is the default quoting style of the csv module?
BeginnerAnswers and explanations are in the Veda app.
Q9. Which of the following is a common mistake when writing CSV files?
BeginnerAnswers and explanations are in the Veda app.
Q10. What does the csv module do when a field contains a double quote character?
BeginnerAnswers and explanations are in the Veda app.
Q11. How can you define a reusable custom CSV format?
BeginnerAnswers and explanations are in the Veda app.
Q12. What is the output of the following code? ```python import csv from io import StringIO data = "a,b,c\n1,2,3\n" reader = csv.reader(StringIO(data)) for row in reader: print(row) ```
BeginnerAnswers and explanations are in the Veda app.
Q13. Which of the following is true about the data type of fields read by the csv module?
BeginnerAnswers and explanations are in the Veda app.
Q14. What is the primary advantage of using the csv module over manual string parsing?
BeginnerAnswers and explanations are in the Veda app.
Q15. The csv module is part of Python's standard library and requires no installation.
BeginnerAnswers and explanations are in the Veda app.
Q16. When writing a CSV file, you should open the file in binary mode to avoid blank lines.
BeginnerAnswers and explanations are in the Veda app.
Q17. What is the purpose of the `newline=''` parameter when opening a file for CSV writing?
BeginnerAnswers and explanations are in the Veda app.
Q18. How do you read a CSV file and print each row as a list of strings?
BeginnerAnswers and explanations are in the Veda app.
Q19. What does the `delimiter` parameter do in `csv.reader`?
BeginnerAnswers and explanations are in the Veda app.
Q20. Why is it important to convert numeric strings to numbers when processing CSV data?
BeginnerAnswers and explanations are in the Veda app.
Q21. What happens if you forget to use `newline=''` when writing a CSV file?
BeginnerAnswers and explanations are in the Veda app.
Building up · 13 questions
Q22. What is the difference between `csv.reader` and `csv.DictReader`?
IntermediateAnswers and explanations are in the Veda app.
Q23. Why should you avoid using simple string splitting to parse CSV files?
IntermediateAnswers and explanations are in the Veda app.
Q24. How can you parse CSV data that is stored in a string variable?
IntermediateAnswers and explanations are in the Veda app.
Q25. How do you write a header row and data rows using `csv.writer`?
IntermediateAnswers and explanations are in the Veda app.
Q26. What is a dialect in the csv module?
IntermediateAnswers and explanations are in the Veda app.
Q27. What is the default quoting style of the csv module and when does it quote fields?
IntermediateAnswers and explanations are in the Veda app.
Q28. What is the purpose of `csv.DictWriter` and what parameter is required?
IntermediateAnswers and explanations are in the Veda app.
Q29. How can you read a TSV (tab-separated values) file using the csv module?
IntermediateAnswers and explanations are in the Veda app.
Q30. Consider the following code snippet that reads a CSV file and prints each row: ```python import csv with open('data.csv', 'r') as f: reader = csv.reader(f) for row in reader: print(row) ``` What is the data type of each `row` variable?
IntermediateAnswers and explanations are in the Veda app.
Q31. You are writing a CSV file using `csv.writer`. Which of the following file-opening modes is correct to avoid blank lines in the output?
IntermediateAnswers and explanations are in the Veda app.
Q32. Given the following CSV data stored in a string: ```python csv_data = "name,age\nAlice,30\nBob,25" ``` Which code correctly parses this data into rows using the csv module?
IntermediateAnswers and explanations are in the Veda app.
Q33. You have a CSV file that uses semicolons (`;`) as delimiters and also contains fields with embedded semicolons. Which approach correctly reads this file?
IntermediateAnswers and explanations are in the Veda app.
Q34. When using `csv.DictWriter`, what happens if you try to write a row that contains a key not specified in the `fieldnames` parameter?
IntermediateAnswers and explanations are in the Veda app.
Topper level · 6 questions
Q35. How does the csv module handle fields that contain double quotes?
AdvancedAnswers and explanations are in the Veda app.
Q36. How do you create a reusable custom dialect for a legacy system that uses semicolons and a specific quote character?
AdvancedAnswers and explanations are in the Veda app.
Q37. You are writing a function that reads a CSV file with a header row and returns a dictionary mapping each column name to a list of values, converting numeric-looking values to floats. Which implementation correctly handles quoted fields and type conversion?
AdvancedAnswers and explanations are in the Veda app.
Q38. You have a CSV file where some fields contain newlines and quotes. You need to read it and count the number of records where the 'status' column equals 'active'. Which code correctly handles this?
AdvancedAnswers and explanations are in the Veda app.
Q39. You are given a CSV string with a custom dialect: fields are separated by `|`, quoted with `'`, and lines end with `\r\n`. You need to parse it and write a new CSV with standard comma delimiters and double quotes. Which code correctly handles this transformation?
AdvancedAnswers and explanations are in the Veda app.
Q40. You are writing a script that reads a CSV file with a header row and writes a new CSV file with the same data but with all string values stripped of leading/trailing whitespace. Which code correctly handles this, including fields that contain commas and quotes?
AdvancedAnswers and explanations are in the Veda app.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free to start, in more than 40 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Paper Solver
Get Veda free See what students get
Free to start · Android app and web · More than 40 languages