Completed
Class 12 CS – CSV File Handling Challenge
Questions From This Challenge
Q1mcq1 mark
What will be the output? import csv f = open("student.csv", "w") w = csv.writer(f) w.writerow(["Amit", 85, "A"]) w.writerow(["Riya", 92, "A+"]) f.close() f = open("student.csv", "r") r = csv.reader(f) for row in r: print(row) f.close()
['Amit', '85', 'A'] followed by ['Riya', '92', 'A+'] ✓
['Amit', 85, 'A'] followed by ['Riya', 92, 'A+']
Amit,85,A followed by Riya,92,A+
['Amit', '85', 'A+'] followed by ['Riya', '92', 'A']
Q2mcq1 mark
What will be written to the CSV file? import csv f = open("data.csv", "w") w = csv.writer(f) w.writerow(["A", "B"]) w.writerow(["C", "D"]) f.close()
A,B,C,D in one row
A,B in one row and C,D in the next row ✓
A B in one row and C D in the next row
Four separate rows containing A, B, C and D
Q3output1 mark
What will be the output?
import csv
f = open("data.csv", "w")
w = csv.writer(f)
w.writerow(["A", 10])
w.writerow(["B", 20])
f.close()
f = open("data.csv", "r")
r = csv.reader(f)
total = 0
for row in r:
total = total + int(row[1])
print(total)
f.close()
Correct output:
30Q4mcq1 mark
Which statement correctly writes all the following rows into a CSV file? rows = [["A", 10], ["B", 20], ["C", 30]]
w.writerow(rows)
w.writerows(rows) ✓
csv.writerow(rows)
csv.writerows(rows)
Q5output1 mark
What will be the output?
import csv
f = open("data.csv", "w")
w = csv.writer(f)
w.writerow(["Name", "Marks"])
w.writerow(["A", 80])
w.writerow(["B", 90])
f.close()
f = open("data.csv", "r")
r = csv.reader(f)
for row in r:
print(row[0])
f.close()
Correct output:
Name
A
BQ6mcq1 mark
A CSV file contains: Amit,85 Riya,92 Karan,78 Which code correctly calculates the total of the marks?
for row in r: total = total + row[1]
for row in r: total = total + int(row[1]) ✓
for row in r: total = total + float(row[0])
for row in r: total = total + row[0]
Q7mcq1 mark
What will happen to an existing CSV file when it is opened using mode "w"?
New data is added after existing data
Existing contents are preserved and read-only
Existing contents are removed before new data is written ✓
Only the last row is removed
Q8mcq1 mark
What will be the output? import csv f = open("data.csv", "w") w = csv.writer(f) w.writerows([["A", 10], ["B", 20], ["C", 30]]) f.close() f = open("data.csv", "r") r = csv.reader(f) count = 0 for row in r: if int(row[1]) > 15: count = count + 1 print(count) f.close()
1
2 ✓
3
0
Q9mcq1 mark
What will happen when the following code is executed? import csv f = open("data.csv", "a") w = csv.writer(f) w.writerow(["C", 30]) f.close()
Existing contents are deleted
The new row is added at the beginning
The new row is added at the end ✓
The file cannot be opened
Q10output1 mark
What will be the output?
import csv
f = open("data.csv", "w")
w = csv.writer(f)
w.writerow(["A", 10])
w.writerow(["B", 20])
f.close()
f = open("data.csv", "r")
r = csv.reader(f)
for row in r:
print(row[0], int(row[1]) + 5)
f.close()
Correct output:
A 15
B 25Q11mcq1 mark
What is the main difference between writerow() and writerows()?
writerow() writes one row, while writerows() writes multiple rows ✓
writerow() reads one row, while writerows() reads multiple rows
writerow() writes columns, while writerows() reads columns
There is no difference
Q12output1 mark
What will be the output?
import csv
f = open("student.csv", "w")
w = csv.writer(f)
w.writerow(["Name", "Marks"])
w.writerow(["A", 75])
w.writerow(["B", 85])
w.writerow(["C", 65])
f.close()
f = open("student.csv", "r")
r = csv.reader(f)
next(r)
for row in r:
if int(row[1]) >= 75:
print(row[0])
f.close()
Correct output:
A
BQ13output1 mark
What will be the output?
import csv
f = open("data.csv", "w")
w = csv.writer(f)
w.writerows([["A", 10], ["B", 20]])
f.close()
f = open("data.csv", "a")
w = csv.writer(f)
w.writerow(["C", 30])
f.close()
f = open("data.csv", "r")
r = csv.reader(f)
for row in r:
print(row)
f.close()
Correct output:
['A', '10']
['B', '20']
['C', '30']Q14mcq1 mark
What will be the output? import csv f = open("data.csv", "w") w = csv.writer(f) w.writerows([["A", 5], ["B", 10], ["C", 15]]) f.close() f = open("data.csv", "r") r = csv.reader(f) total = 0 for row in r: if int(row[1]) % 5 == 0: total = total + int(row[1]) print(total) f.close()
15
20
30 ✓
25
Q15output1 mark
What will be the output?
import csv
f = open("data.csv", "w")
w = csv.writer(f)
w.writerow(["Name", "Marks"])
w.writerows([["A", 70], ["B", 80]])
f.close()
f = open("data.csv", "r")
r = csv.reader(f)
count = 0
for row in r:
if row[0] != "Name" and int(row[1]) > 70:
count = count + 1
print(count)
f.close()
Correct output:
1