Bird
0
0

What will be the output of this code?

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

with open('test.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)
AError due to missing encoding
B['Name', 'Age']\n['Alice', 30]
CName,Age\nAlice,30
D['Name', 'Age']\n['Alice', '30']
Step-by-Step Solution
Solution:
  1. Step 1: Writing rows with csv.writer

    The code writes two rows: header with strings and a data row with a string and an integer.
  2. Step 2: Reading rows with csv.reader

    csv.reader reads all fields as strings, so 30 becomes '30' when read.
  3. Final Answer:

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

    csv.reader reads all fields as strings [OK]
Quick Trick: csv.reader returns all fields as strings [OK]
Common Mistakes:
  • Expecting integers when reading CSV
  • Confusing printed list with CSV text
  • Assuming encoding error without cause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes