wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

file handling in python

Total questions: 25

Worksheet time: 13mins

Name
Class
Date
1.

What will be written inside the file test.csv using the following program


import csv

D = [['Exam'],['Quarterly'],['Halfyearly']]

csv.register_dialect('M',lineterminator = '\n')

with open('c:\pyprg\ch13\line2.csv', 'w') as f:

wr = csv.writer(f,dialect='M')

wr.writerows(D)

f.close()

a)

Exam, Quarterly, Halfyearly

b)

Exam Quarterly Halfyearly

c)

E

Q

H

d)

Exam

Quarterly

Halfyearly

2.

Making some changes in the data of the existing file or adding more data is called

a)

Editing

b)

Appending

c)

Modification

d)

Alteration

3.

What is the output of the following program? import csv d=csv.reader(open ("city.csv'))

next(d)

for row in d:

print(row)

if the file called “city.csv” contain the following details

chennai,mylapore

mumbai,andheri

a)

chennai,mylapore

b)

mumbai,andheri

c)

chennai

mumbai

d)

chennai,mylapore

mumbai,andheri

4.

The command used to skip a row in a CSV file is

a)

next()

b)

skip()

c)

omit()

d)

bounce()

5.

The contents of a CSV file "sample.csv" and the code to read and display the data in this file are given


What will be the values of sno, name and age (respectively) of the first record:

a)

0,12, None

b)

0,12, ' '

c)

1, None, 12

d)

1, ' ' , 12

6.

The contents of a CSV file "sample.csv" and the code to read and display the data in this file are given


What will be the keys of rec:

a)

0,1,2

b)

'0', '1', '2'

c)

'son', 'name', 'age'

d)

'sno_0', 'name_1', 'age_2'

7.

The contents of a CSV file "sample.csv" and the code to read and display the data in this file are given


What will be the data type of variable rec used in the code:

a)

list

b)

tuple

c)

str

d)

dict

8.

The contents of a CSV file "sample.csv" and the code to read and display the data in this file are given


How many lines of output will this code display

a)

2

b)

3

c)

4

d)

5

9.

The contents of the CSV file 'members.csv' are given also the code to display all the records from this file and calculate the average age of all those members whose name does not start with 'A'.


import csv

with open ("member.csv",'r','newline='') as f:

r = csv.___________________ #1

___________________ #2

s=c=0 # sum and counter

for rec in r:

if rec[1][ _ ] != 'A' : #3

s += ______________ #4

c+=1

if _______________: #5

print("Average age = ", s/c)

else: print("All names stars with 'A')

a)

c > 0

b)

c == 0

c)

c >= 0

d)

None of these

10.

The contents of the CSV file 'members.csv' are given also the code to display all the records from this file and calculate the average age of all those members whose name does not start with 'A'.


import csv

with open ("member.csv",'r','newline='') as f:

r = csv.___________________ #1

___________________ #2

s=c=0 # sum and counter

for rec in r:

if rec[1][ _ ] != 'A' : #3

s += ______________ #4

c+=1

if _______________: #5

print("Average age = ", s/c)

else: print("All names stars with 'A')

a)

rec[2]

b)

int (rec[2])

c)

rec

d)

rec[0]

11.

The contents of the CSV file 'members.csv' are given also the code to display all the records from this file and calculate the average age of all those members whose name does not start with 'A'.


import csv

with open ("member.csv",'r','newline='') as f:

r = csv.___________________ #1

___________________ #2

s=c=0 # sum and counter

for rec in r:

if rec[1][ _ ] != 'A' : #3

s += ______________ #4

c+=1

if _______________: #5

print("Average age = ", s/c)

else: print("All names stars with 'A')

a)

0

b)

1

c)

'A'

d)

' -1 '

12.

The contents of the CSV file 'members.csv' are given also the code to display all the records from this file and calculate the average age of all those members whose name does not start with 'A'.


import csv

with open ("member.csv",'r','newline='') as f:

r = csv.___________________ #1

___________________ #2

s=c=0 # sum and counter

for rec in r:

if rec[1][ _ ] != 'A' : #3

s += ______________ #4

c+=1

if _______________: #5

print("Average age = ", s/c)

else: print("All names stars with 'A')

a)

rec[1]

b)

w [1]

c)

first(r)

d)

next(r)

13.

The contents of the CSV file 'members.csv' are given also the code to display all the records from this file and calculate the average age of all those members whose name does not start with 'A'.


import csv

with open ("member.csv",'r','newline='') as f:

r = csv.___________________ #1

___________________ #2

s=c=0 # sum and counter

for rec in r:

if rec[1][ _ ] != 'A' : #3

s += ______________ #4

c+=1

if _______________: #5

print("Average age = ", s/c)

else: print("All names stars with 'A')

a)

reader()

b)

reader_f()

c)

reader(f)

d)

f.reader

14.

The given code is leaving an unwanted blank line after every row in the file sample.csv


The correct

a)

close the file at the end

b)

use keyword argument newline='' with open function

c)

make data a tuple instead of a list

d)

use '\n' as the last element of each record

15.

The given code is raising an exception:

To correct the code, we have to:

a)

close the file at the end

b)

use writerow() instead of writerows()

c)

make row a tuple instead of a list

d)

import pickle

16.

The contents of a text file"sample.txt", and a script to read the file contents are shown


Pick the correct statement w.r.t the given contents and code:

a)

rec will be list in 1 element

b)

rec will be list in 3 element

c)

rec will be a nested list of 1 element

d)

rec will be a nested list of 3 elements

17.

The contents of a csv file "sample.csv" are shown below:

sno, name, age

1, Amit, 12

2, Harsh, 13

3, Vivek, 21

a)

It can be treated as a text file

b)

It cannot be treated as a text file as it is a CSV file

c)

It cannot be opened in a text editor a sit is a CSV file

d)

An error will be raised in Python

18.

The content of a file "sample.txt" are shown below:


1,2,3,4

'a', 'b', 'c'

5, 23, 'me', 'peace'

'to', 'be', 'or' 'not'

a)

It can be treated as a csv file

b)

It can not be treated as CSV file as it is a text file

c)

It can not be treated as CSV file as it contains mixed data

d)

It can not be treated as CSV file as it contains different number of elements in different lines.

19.

CSV module allows writing multiple records in a CSV file using

a)

writerows()

b)

writerow()

c)

writerec()

d)

writelines

20.

Which module in Python is used to process csv file

a)

pickle

b)

random

c)

CSV

d)

csv.reader()

21.

File opening modes for CSV files are:

a)

Same as those for text files

b)

same as those for binary files

c)

different from text files and binary files

d)

mix of text files and binary files

22.

We can manage (create/read/update)CSV files using

a)

A text editor

b)

A Python program

c)

A Spreadsheet application

d)

All of the above

23.

Which character can work as delimiter for CSV files:

a)

Comma only

b)

Tab only

c)

Space only

d)

Any character

24.

Delimeter for CSV file is

a)

Comma

b)

Tab

c)

Space

d)

Semi-colon

25.

CSV stands for

a)

Comma Separated Values

b)

Comma Spaced Values

c)

Completely Separated VAlues

d)

Comma Segregated Values