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

Option a.) infile = open("c:\scores.csv", "r")
Option b.) infile = open("c:\\scores.csv", "r")
Option c.) infile = open(file = "c:\scores.csv", "r")
Option d.) infile = open(file = "c:\\scores.csv", "r")

    Answer

#Answer   b) infile = open("c:\\scores.csv", "r")

    

    



Question 2. State True/False :
(i) The csv files are Binary Files:

Option a.) True
Option b.) False
    Answer

#Answer   b) False

    

    






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


Option a.) When you open a file for reading, if the file does not exist, an error occurs
Option b.) When you open a file for writing, if the file does not exist, a new file is created
Option c.) When you open a file for writing, if the file exists, the existing file is overwritten with the new file
Option d.) All the above

    Answer

#Answer  d) All the above

    

    



Question 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()

    Answer

#Answer (c) csv.reader(infile)

    

    



Question 5. State True/False :
The separator character of csv files is called delimiter.

Option a.) True
Option b.) False
    Answer

#Answer a) True

    

    



Question 6. The full form of CSV is

Option a.) Comma Separated Values
Option b.) Comma Separated Value
Option c.) Comma Separated Variables
Option d.) Comma Separate Values

    Answer

#Answer a) Comma Separated Values

    

    



Question 7. State True/False:
The CSV files only take comma as delimiter.

Option a.) True
Option b.) False

    Answer

#Answer b) False

    

    



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

Option a.) \r
Option b.) \n
Option c.) \r\n
Option d.) \0

    Answer

#Answer c) \r\n

    

    



Question 9. The CSV files are popular because they are

Option a.) capable of storing large amount of data
Option b.) easier to create
Option c.) preferred export and import format for databases and spread sheets
Option d.) All the above

    Answer

#Answer d) All the above

    

    




Question 10. The default delimiter character of CSV file is____.

Option a.) : (colon)
Option b.)\t (tab)
Option c.) , (comma)
Option d.) ; (semi-colon)

    Answer

#Answer c) , (comma)

    

    



Question 11. State True/False:
A CSV file is open in the same way as text file.

Option a.) True
Option b.) False

    Answer

#Answer a) True

    

    



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


Option a.) a
Option b.) w
Option c.) ab
Option d.) r

    Answer

#Answer c) ab

    

    



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


Option a.) a+
Option b.) w+
Option c.) r+
Option d.) All the above.

    Answer

#Answer a) a+

    

    



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


Option a.) a+
Option b.) w+
Option c.) r+
Option d.) All the above.

    Answer

#Answer c) r+

    

    



Question 15. The CSV files are ____ files.

Option a.) Text
Option b.) Binary
Option c.) Data
Option d.) Python

    Answer

#Answer a) Text

    

    


CSV Case Study Based Questions

Question 16. 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.


Option a.) include
Option b.) add
Option c.) Import
Option d.) import

    Answer

#Answer d) import

    

    



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


Option a.) Customer
Option b.) reader
Option c.) Cuswriter
Option d.) writer

    Answer

#Answer c) Cuswriter

    

    



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


Option a.) Row
Option b.) Rec
Option c.) row
Option d.) rec

    Answer

#Answer b) Rec

    

    



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


Option a.) Customer
Option b.) Customer.csv
Option c.) Customer.txt
Option d.) Customer.dat

    Answer

#Answer b) Customer.csv

    

    



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


Option a.) i
Option b.) Rec
Option c.) row
Option d.) rec

    Answer

#Answer d) rec

    

    



Question 17. 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.


Option a.) math
Option b.) csv
Option c.) pickle
Option d.) random

    Answer

#Answer 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)


Option a.) "File_extent.csv","r"
Option b.) "File_extent.csv","w"
Option c.) "File_extent.csv","a"
Option d.) "File_extent.csv","w+"


    Answer

#Answer c) "File_extent.csv","a"

    

    



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


Option a.) read()
Option b.) readline()
Option c.) load()
Option d.) reader


    Answer

#Answer d) reader

    

    






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


Option a.) end()
Option b.) close()
Option c.) close
Option d.) from


    Answer

#Answer b) close()

    

    





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


Option a.) "File_extent.csv"
Option b.) File_extent.csv
Option c.) File_extent
Option d.) .csv


    Answer

#Answer b) File_extent.csv

    

    



Question 18. 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.

Option a.) math
Option b.) pickle
Option c.) csv
Option d.) random

    Answer

#Answer c) csv

    

    



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


Option a.) writerows(fields)
Option b.) writerow(field)
Option c.) writerow(fields)
Option d.) writerows(fields)

    Answer

#Answer c) writerow(fields)

    

    



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


Option a.) writerows(row)
Option b.) writerow(row)
Option c.) writerow(rows)
Option d.) write_row(row)


    Answer

#Answer

    

    

Option b.) writerow(row)


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


Option a.) csv.reader(f)
Option b.) csv.read(f)
Option d.) pickle.load(f)
e) f.read()

    Answer

#Answer

    

    


Option a.) csv.reader(f)


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


Option a.) Row[0]
Option b.) Rec[0]
Option c.) row[0]
Option d.) rec[0]

    Answer

#Answer c) row[0]

    

    






Question 19. 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.


Option a.) AddNewRec
Option b.) Addnew
Option c.) Addrec
Option d.) AddNewRec()

    Answer

#Answer a) AddNewRec

    

    



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


Option a.) w
Option b.) r
Option c.) w+
Option d.) a

    Answer

#Answer d) a

    

    






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


Option a.) country,capital
Option b.) Country,Capital
Option c.) Coun,Cap
Option d.) [Country,Capital]

    Answer

#Answer b) Country,Capital

    

    



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


Option a.) Reader()
Option b.) reader()
Option c.) read
Option d.) reader

    Answer

#Answer d) reader

    

    



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


Option a.) ‘INDIA NEW DELHI’
‘CHINA BEIJING’

Option b.) ‘INDIA’ ‘NEW DELHI’
‘CHINA’ ‘BEIJING’

Option c.) INDIA NEW DELHI
CHINA BEIJING

Option d.) All the above

    Answer

#Answer c) INDIA NEW DELHI

    

    




Question 20. 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()



Option a.) KRISH 9678564534
SOMU 9867564534

Option b.) SOMU 9867564534
KRISH 9678564534

Option c.) somu 9867564534
krish 9678564534

Option d.) ‘SOMU’ 9867564534
‘KRISH’ 9678564534

    Answer

#Answer b) SOMU 9867564534   KRISH 9678564534