Bird
Raised Fist0

What will be the output of this code snippet?

medium📝 Predict Output Q4 of Q15
Python - Structured Data Files
What will be the output of this code snippet?
import csv
with open('test.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['Name', 'Age'])
    writer.writerow(['Alice', 30])

with open('test.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)
A['Name', 'Age']\n['Alice', 30]
B['Name', 'Age']\n['Alice', '30']
CName,Age\nAlice,30
DName Age\nAlice 30
Step-by-Step Solution
Solution:
  1. Step 1: Understand csv.writer.writerow output

    Each call writes a list as a CSV row; all values are serialized to strings.
  2. Step 2: Reading with csv.reader returns lists of strings

    Printing each row shows: ['Name', 'Age']\n['Alice', '30'].
  3. Final Answer:

    ['Name', 'Age']\n['Alice', '30'] -> Option B
  4. Quick Check:

    csv.reader returns lists of strings as printed [OK]
Quick Trick: csv.reader returns each row as a list of strings [OK]
Common Mistakes:
MISTAKES
  • Thinking numbers remain integers after reading
  • Expecting CSV format string output from print
  • Confusing print output with file content

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes