CSV File MCQ Question Bank For Class 12

 

1. To open a file c:\scores.csv for reading, we use _______ command.

a) infile = open(“c:\scores.csv”, “r”)

b) infile = open(“c:\\scores.csv”, “r”)

c) infile = open(file = “c:\scores.csv”, “r”)

d) infile = open(file = “c:\\scores.csv”, “r”)

 

b) infile = open(“c:\\scores.csv”, “r”)

 

 

2. State True/False :

(i) The csv files are Binary Files:

a) True

b) False

b) False

 

 

3. Which of the following statement(s) are true for csv files?

a) When you open a file for reading, if the file does not exist, an error occurs

b) When you open a file for writing, if the file does not exist, a new file is created

c) When you open a file for writing, if the file exists, the existing file is overwritten with the new file

d) All the above

d) All the above

 

 

4. To read the entire content of the CSV file as a nested list from a file object infile, we use __________ command.

(a) infile.read()

(b) infile.reader()

(c) csv.reader(infile)

(d) infile.readlines()

(c) csv.reader(infile)

 

 

5. State True/False :

The separator character of csv files is called delimiter.

a) True

b) False

a) True

 

 

6. The full form of CSV is

a) Comma Separated Values

b) Comma Separated Value

c) Comma Separated Variables

d) Comma Separate Values

a) Comma Separated Values

 

 

7. State True/False:

The CSV files only take comma as delimiter.

a) True

b) False

b) False

 

 

8. EOL character used in windows operating system in CSV file is

a) \r

b) \n

c) \r\n

d) \0

c) \r\n

 

 

9. The CSV files are popular because they are

a) capable of storing large amount of data

b) easier to create

c) preferred export and import format for databases and spread sheets

d) All the above

d) All the above

 

 

10. The default delimiter character of CSV file is____.

a) : (colon)

b)\t (tab)

c) , (comma)

d) ; (semi-colon)

c) , (comma)

 

 

11. State True/False:

A CSV file is open in the same way as text file.

a) True

b) False

a) True

 

 

12. Which of the following is not a valid mode to open CSV file?

a) a

b) w

c) ab

d) r

c) ab

 

 

13. The file mode to open a CSV file for appending as well as reading is _____.

a) a+

b) w+

c) r+

d) All the above.

a) a+

 

 

14. The file mode to open a CSV file for reading as well as writing is _____.

a) a+

b) w+

c) r+

d) All the above.

c) r+

 

 

15. The CSV files are ____ files.

a) Text

b) Binary

c) Data

d) Python

a) Text

 

 

CSV file: import csv module, open / close csv file, write into a csv file using csv.writerow() and read from a csv file using csv.reader( )

16. The character that separates values in csv files is called the ………

a) delimit

b) delimiter

c) delimited

d) delimits

b) delimiter

 

 

17. The default delimiter of csv file is ……………

a) comma

b) colon

c) semicolon

d) hyphen

a) comma

 

 

18. The file mode to open a csv file for reading as well writing is ………

a) r

b) rw

c) r+

d) rb

c) r+

 

 

19. The file mode to open a csv file for appending as well reading is ……..

a) w

b) w+

c) a

d) a+

d) a+

 

 

20. To specify a different delimiter while writing into csv file, ……. argument is used with csv.writer().

a) delimit

b) delimiter

c) delimited

d) delimits

b) delimiter

 

 

21. To cancel the EOL translation in csv file while writing the data ………… argument is used with open().

a) newline

b) next

c) open

d) EOL

a) newline

 

 

22. To add data to an existing csv file, the mode of the file should be ……..

a) w

b) w+

c) a

d) a+

c) a

 

 

23. CSV stands for …….

a) Cursor Separated Variables

b) Comma Separated Values

c) Cursor Separated Values

d) Cursor Separated Version

b) Comma Separated Values

 

 

24. Which module is used for working with CSV files in Python?

a) random

b) statistics

c) csv

d) math

c) csv

 

 

25. Every record in a CSV file is stored in reader object in the form of a list using which method?

a) writer()

b) append()

c) reader()

d) list()

c) reader()

 

 

CASE STUDY QUESTIONS (R)

26. Deepesh works as a programmer with Delta Technologies. He has been assigned the job of generating the salary of all employees using the file “employee.csv”. He has written a program to read the CSV file “employee.csv” which will contain details of all the employees. He has written the following code. As a programmer, help him to successfully execute the given task.

import______ # Line 1

def readCsvEmp( ): # to read data from the CSV file

with ______(’employees.csv’, newline=”) as f: # Line 2

reader = csv.______ (f) # Line 3

data_list = ______(reader) # Line 4

______ (data_list) # Line 5

 

(i) Name the module he should import in Line 1.

a) import csv

b) csv import

c) import

d) export csv

a) import csv

 

 

(ii) Write the method that he should use to open the file to read data from it.

a) read

b) open

c) close

d) append

b) open

 

 

(iii) Fill in the blank in Line 3 to read the data from a csv file.

a) read

b) readline

c) reader

d) writer

c) reader

 

 

(iv) Fill in the blank in Line 4 with the method to convert the data read from the file into list.

a) list

b) sets

c) dictionary

d) tuple

a) list

 

 

27. Observe the following code and fill the blank in statement1

import csv

with _________ as f: #statement1

r = csv.______(f) #statement2

for row in ______: #statement3

print(_____) #statement4

a) open(“data.csv”)

b) f=open(“data.csv”)

c) Both A & B are Correct

d) Both A & B are incorrect

a) open(“data.csv”)

 

 

28. Observe the following code and fill the blank in statement2

import csv

with _________ as f: #statement1

r = csv.______(f) #statement2

for row in ______: #statement3

print(_____) #statement4

 

a) load

b) read()

c) reader()

d) readlines()

c) reader()

 

 

29. Observe the following code and fill the blank in statement3

import csv

with _________ as f: #statement1

r = csv.______(f) #statement2

for row in ______: #statement3

print(_____) #statement4

a) F

b) r

c) r,f

d) None of the above

b) r

 

 

30. Observe the following code and fill the blank in statement4

import csv

with _________ as f: #statement1

r = csv.______(f) #statement2

for row in ______: #statement3

print(_____) #statement4

a) r

b) row

c) f

d) csv

b) row

 

 

31. Legend sports wanted to store the number of prizes for each sport as a SPORTS.CSV file. As a programmer help them to complete the task successfully.

import _______________ #Line 1

fh=___________________ # Line 2

swriter = ______________(fh) #Line 3

ans=‟y‟

i=1

while ans==‟y‟:

print(“Record”,i)

sport=input(“Sport name”)

prizes=int(input(“Enter prizes won”))

__________ # Line 4

i=i+1

ans=input(“Want to enter records”)

fh._________________#Line 5

 

(i) Name the module to be imported in Line 1.

a) .tsv

b) .csv

c) .py

d) .bin

b) .csv

 

 

(ii) Fill in line 2 to open the CSV file.

a) fh = open(“sports.csv”,”w”)

b) fh=read(“sports.csv”,”w”)

c) fh = file(“sports.csv”,”w”)

d) fh = append(“sports.csv”,”w”)

a) fh = open(“sports.csv”,”w”)

 

 

(iii) Write the correct statement to write the data into file in line 3.

a) writerows( )

b) writerow( )

c) writer( )

d) swriter = csv.csvwriter(fh)

d) swriter = csv.csvwriter(fh)

 

 

(iv) Write the statement to write the records given as input from user in line 4.

a) swriter([sport,prizes])

b) swriter.writrrow([sport,prizes])

c) swriter_writrrow([sport,prizes])

d) swriterwritrrow([sport,prizes])

b) swriter.writrrow([sport,prizes])

 

 

32. Krishna of class 12 is writing a program to read the details of Sports performance and store in the csv file “Sports.csv” delimited with a tab character. As a programmer, help him to achieve the task.

import ___________ # Line 1

f = open(“Sports.csv”,”a”)

wobj = csv.______________ (f, delimiter = „\t‟) # Line 2

wobj.writerow( [„Sport‟, „Competitions‟, „Prizes Won‟] )

ans = „y‟

i = 1

while ans == „y‟:

print(“Record :”, i)

sport = input(“Sport Name :”)

comp = int(input(“No. of competitions participated :”))

prize = int(input(“Prizes won:”))

record = ____________________ # Line 3

wobj.______________ (rec) # Line 4

i += 1

ans = input(“Do u want to continue ? (y/n) :”)

f.___________ # Line 5

 

(i) Name the module he should import in Line 1

a) .tcs

b) .tmp

c) .bin

d) .csv

d) .csv

 

 

(ii) To create an object to enable to write in the csv file in Line 2

a) open

b) writer

c) file

d) read

b) writer

 

 

(iii) To create a sequence of user data in Line 3

a) [prize,comp,sport]

b) [comp,prize,sport]

c) [sport, comp, prize]

d) none of the above

c) [sport, comp, prize]

 

 

(iv) To write a record onto the writer object in Line 4

a) write

b) writerow

c) writeline

d) writelines

b) writerow

 

 

33. Kumar is writing a program to create a CSV file “student.csv” which will contain rollno, name and age of some students. He has written the following code. As a programmer, help him to successfully execute the given task

import ________________ # Line 1

f=open(‘student.csv’,’w’,newline=””)

p=csv._________(f) # Line 2

ch=’y’

while ch==’y’:

l=[]

rollno=int(input(‘enter rollno’))

name=input(‘enter name’)

age=int(input(‘enter age’))

l.append(rollno)

l.append(name)

l.append(age)

p.___________(l) # Line 3

ch = input (‘want to continue y/n?’)

if ch==’y’:

continue

else:

break

f.__________()

f=open(‘student.csv’,’r+’)

c=list(csv.reader(f))

for i in c:

k=i[2]

if int(k)>15:

print(i)

f.close()

 

(i) Name the module he should import in Line 1

a) import csv

b) csv import

c) import

d) export csv

a) import csv

 

 

(ii) which function is used in Line 2 to create a writer object

a) p=tsv.writer(f)

b) p=psv.writer(f)

c) p=csv.writer(f)

d) p=dsv.writer(f)

c) p=csv.writer(f)

 

 

(iii) The method which is to be used in line 3 to writes a row of data into the specified file

a) p.writerow(l)

b) p.writerows(l)

c) p.writer()

d) p.writerow(l)

d) p.writerow(l)

 

 

(iv) Fill in the blank in Line 4 to close the file.

a) f.close()

b) f.open()

c) close.f()

d) f.read()

a) f.close()

 

 

34. Puneeta is storing the data of her gift store in a csv file named gift.csv which will store Gift_code and Gift_name of items of her gift store. She has written the following code .As a programmer help her to successfully execute her program in python:

import ___________ # Line 1

def writeFile(Gift_code,Gift_name):

F=open(“gift.csv”,‟___‟) #Line 2

FW=csv.________(F) #Line 3

FW.writerow([Gift_code,Gift_name)

F.close()

#CSV file for reading

def readFile():

with ________(„gift.csv‟,‟r‟) as newF #Line 4

FR=csv.reader(newF)

for row in FR:

if row[0]==101:

print(row)

newF.close()

writeFile(101,”Photoframe”)

writeFile(102,”Soft Toys”)

writeFile(103,”Flower Pot”)

readFile() #Line 5

 

(i) Name the module she should import in line 1.

a) .tmp

b) .bin

c) .tsc

d) .csv

d) .csv

 

 

(ii) In which mode Puneeta should open the file to add data in it?

a) “a”

b) “ab”

c) “r”

d) “w”

a) “a”

 

 

(iii) Fill in the blanks in Line 3 to write data to csv file gift.csv

a) close()

b) open()

c) writer()

d) append()

c) writer()

 

 

(iv) Fill in the blank in Line 4 to open the csv file from the disk

a) close

b) open

c) write

d) read

b) open

 

 

35. What is the output of the following program?

import csv

d=csv.reader(open(‘c:\PYPRG\ch13\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,mumba

d) chennai,mylapore mumbai,andheri

b) mumbai,andheri

 

 

 

#TRENDING

Text File MCQs for Class 12 Term 1

Binary File MCQs for Class 12 Term 1

 

CSV Case Study Based Questions

36. Sonal, a student of class 12th, is learning CSV File Module in Python. During examination, she has been assigned an incomplete python code (shown below) to create a CSV file ‘Customer.csv’ (content shown below). Help her in completing the code which creates the desired CSV file.

Cus_No Name Address Ph_No
11 Rohit Mumbai 8567843243
12 Sonal Delhi 9645342345

   

Incomplete Code

______ csv #Statement 1

def Create_CSV():

    fw=open("Customer.csv","w")

    _______=csv.writer(fw) #Statement 2

    Cuswriter.writerow(["Cus_No","Name","Address","Ph_No"])

    n=int(input("Enter total number of Customer"))

    for i in range(n):

        Cusno=int(input("Enter Customer no."))

        Name=input("Enter Name")

        Add=input("Enter Address")

        Ph_No=int(input("Enter Phone No."))

        Rec=[Cusno,Name,Add,Ph_No]

        Cuswriter.writerow(____) #Statement 3

    fw.close()

def Display_CSV():

    fr=open("__________","r") #Statement 4

    Cusreader=csv.reader(fr)

    i=0

    for ____ in Cusreader: #Statement 5

        if i%2==0:

            print(rec[0],'\t',rec[1],'\t',rec[2],'\t',rec[3])

        else:

            pass

        i+=1

    fr.close()

Create_CSV()
Display_CSV()

 

(i) Identify suitable code for the blank space in line marked as Statement-1.

a) include

b) add

c) Import

d) import

 

d) import

 

 

(ii) Identify the missing code for the blank space in line marked as Statement-2.

a) Customer

b) reader

c) Cuswriter

d) writer

 

c) Cuswriter

 

(iii) Identify the argument name for the blank space in line marked as Statement-3?

a) Row

b) Rec

c) row

d) rec

 

b) Rec

 

 

(iv) Identify the missing file name for the blank space in line marked as Statement-4?

a) Customer

b) Customer.csv

c) Customer.txt

d) Customer.dat

 

b) Customer.csv

 

 

(v) Identify the object name for the blank space in line marked as Statement-5?

a) i

b) Rec

c) row

d) rec

 

d) rec

 

 

37. Mohan has written following program to create a CSV file “File_extent.csv” which will contain file types and file extensions for some records. As a programmer, help him to successfully execute the given task:

import ______ # Statement 1

def adddata(filetype,extension): # To write /add data into the file

    f=open(______,_____,newline='') # Statement 2

    newFileWriter = csv.writer(f)

    newFileWriter.writerow([filetype,extension])

    f.close()

#Csv file reading code

def readdata(filename): # To read data

    with open(filename,'r') as f:

        filereader = csv.______(f) # Statement 3

        for row in filereader:

            print (row[0],row[1])

        f._______ # Statement 4

adddata("Notepad","txt")

adddata("Word","docx")

adddata("Excel","xlsx")

adddata("PowerPoint","pptx")

readdata("________") #Statement 5

 

(i) Identify the module he should import for Statement 1.

a) math

b) csv

c) pickle

d) random

 

b) csv

 

 

(ii) Choose the correct option for Statement 2 to open file name and mode. (Suresh should open the file to add data into the file)

a) “File_extent.csv”,”r”

b) “File_extent.csv”,”w”

c) “File_extent.csv”,”a”

d) “File_extent.csv”,”w+”

 

c) “File_extent.csv”,”a”

 

 

(iii) Choose the correct option for Statement 3 to read the data from a csv file.

a) read()

b) readline()

c) load()

d) reader

 

d) reader

 

 

(iv) Choose the correct option for Statement 4 to close the file.

a) end()

b) close()

c) close

d) from

 

b) close()

 

(v) Choose the correct option for Statement 5 to send the file name as parameter.

a) “File_extent.csv”

b) File_extent.csv

c) File_extent

d) .csv

 

b) File_extent.csv

 

 

38. Ashok Kumar of class 12 is writing a program to create a CSV file “empdata.csv” with empid, name & mobile number. Also to search a particular empid and display its record details. He has written the following code. As a programmer, help him to successfully execute the given task.

 

import ______ #Line1

fields=['empid','name','mobile_no']

rows=[['101','Rohit','8982345659'],['102','Shaurya','8974564589'],['103','Deep','8753695421'],['104','Prerna','9889984567'],['105','Lakshya','7698459876']]

filename="empdata.csv"

f=open(filename,'w',newline='')

csv_w=csv.writer(f)

csv_w._________ #Line2

for row in rows:

    csv_w.______ #Line3

f.close()

f=open(filename,'r')

csv_r=________ #Line4

ans='y'

while ans=='y':

    found=False

    emplid=input("Enter employee id to search=")

    for row in csv_r:

        if len(row)!=0:

            if _______==emplid: #Line5

                print("Name : ",row[1])

                print("Mobile No : ",row[2])

                found=True

                break

if not found:

    print("Employee id not found")

ans=input("Do you want to search more? (y)")

 

(i) Choose the module he should import in Line 1.

a) math

b) pickle

c) csv

d) random

 

c) csv

 

 

(ii) Choose a code to write the fields (column heading) from fields list in Line2.

a) writerows(fields)

b) writerow(field)

c) writerow(fields)

d) writerows(fields)

c) writerow(fields)

 

 

(iii) Choose a code to write the row from rows list in Line3.

a) writerows(row)

b) writerow(row)

c) writerow(rows)

d) write_row(row)

 

b) writerow(row)

 

 

(iv) Choose a code for line 4 to read the data from a csv file.

a) csv.reader(f)

b) csv.read(f)

d) pickle.load(f)

e) f.read()

a) csv.reader(f)

 

 

(v) Choose the correct variable (list value) to check “emplid” in Line5.

a) Row[0]

b) Rec[0]

c) row[0]

d) rec[0]

 

c) row[0]

 

 

39. Sumit is making a software on “Countries and their Capitals” in which various records are to be stored/retrieved in “CAPITAL.CSV” data file. It consists of few records of Countries and their Capitals. He has written the following code in python. As a programmer, you have to help him to successfully execute the program.

 

import csv

# Fn. to add a new record in CSV file

def _________(Country,Capital): # Statement-1

    f=open("CAPITAL.CSV","__") # Statement-2

    fwriter=csv.writer(f)

    fwriter.writerow([_____]) # Statement-3

    f.close()

def ShowRec(): # Fn. to display all records from CSV file

    with open("CAPITAL.CSV","r") as NF:

        NewReader=csv._____ (NF) # Statement-4

        for rec in NewReader:

            if len(rec)!=0:

                print(rec[0],rec[1])

AddNewRec("INDIA","NEW DELHI")

AddNewRec("CHINA","BEIJING")

ShowRec() # Statement-5

 

 

(i) Choose the Name of the function in Statement-1.

a) AddNewRec

b) Addnew

c) Addrec

d) AddNewRec()

 

a) AddNewRec

 

 

(ii) Choose the file mode to be passed to add new records in Statement-2.

a) w

b) r

c) w+

d) a

d) a

 

 

(iii) Identify the correct variables in Statement-3 to store data to the file.

a) country,capital

b) Country,Capital

c) Coun,Cap

d) [Country,Capital]

b) Country,Capital

 

 

(iv) Choose the correct option for Statement-4 to read the data from a csv file.

a) Reader()

b) reader()

c) read

d) reader

d) reader

 

 

(v) Choose the output which will come after executing Statement-5.

a) ‘INDIA NEW DELHI’

‘CHINA BEIJING’

b) ‘INDIA’ ‘NEW DELHI’

‘CHINA’ ‘BEIJING’

c) INDIA NEW DELHI

CHINA BEIJING

d) All the above

c) INDIA NEW DELHI

CHINA BEIJING

 

 

40. Choose the possible output from the following code:

 

import csv

def addFile(Name,Phno):

    f=open("Diary.csv","a",newline='')

    newFileWriter = csv.writer(f)

    newFileWriter.writerow([Name,Phno])

    f.close()

def readFile():

    with open("Diary.csv",'r') as f:

        filereader = csv.reader(f)

        for row in filereader:

            print (row[0],row[1])
    f.close()

addFile("SOMU","9867564534")

addFile("KRISH","9678564534")

readFile()

 

a) KRISH 9678564534

SOMU 9867564534

b) SOMU 9867564534

KRISH 9678564534

c) somu 9867564534

krish 9678564534

d) ‘SOMU’ 9867564534

‘KRISH’ 9678564534

 

b) SOMU 9867564534

KRISH 9678564534

 

 

 

#TRENDING

Text File MCQs for Class 12 Term 1

Binary File MCQs for Class 12 Term 1

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved