Python Practice Paper for Class 12


1. To open a file c:\ss.txt for appending data we use
a. file = open(‘c:\\ss.txt’,’a’) b. file = open(r‘c:\ss.txt’,’a’)
c. file = open(‘c:\\ss.txt’,’w’) d. both a and b

2. To read the next line of the file from the file object infi, we use
a. b. c. infi.readline() d. infi.readlines()

3. Which function is used to ensure that the data in written in the file immediately?
a. <filehandle>.write() b. <filehandle>.writelines()
c. flush() d. <filehandle>.close()

4. The process of converting byte stream back to the original structure is known as
a. Pickling. b. Unpickling c. Packing d. Zipping

5. Which file mode is used to handle binary file for reading.
a. rb b. rw c. r d. w

6. Which of the following is not a correct statement for binary files?
a. Easy for carrying data into buffer b. Much faster than other file systems
c. Characters translation is not required d. Every line ends with new line character ‘\n’

7. What does the acronym CSV stand for in its full form?
a. Common Separated Value b. Comma System Value
c. Comma Separated Value d. Common System Vault

8. What is the default delimiter of a CSV file
a. Tab b. Comma c. Semicolon d. Space

9. In regards to separated value files such as .csv and .tsv, what is the delimiter?
a. Any character such as the comma (,) or tab (\t) that is used to separate the row data b. Anywhere the comma (,) character is used in the file
c. Delimiters are not used in separated value files
d. Any character such as the comma (,) or tab (\t) that is used to separate the column data.

10. Which list method can be used to perform Push operation in a stack implemented by list?
(a) append() (b) extend() (c) push() (d) insert()


12. What is the difference between opening mode ‘a’ and ‘w’?

13. What is the purpose of flush() in file handling operations?

14. What is the output of following code?
fh = open(‘main.txt’,’r’)
size = len(

15. Differentiate between pickle.load() and pickle.dump() methods with suitable example.

16. Differentiate between file modes r+ and w+ with respect to python.

17. Identify the error in the following code.
import pickle
data = [‘one’, 2, [3, 4, 5]]
with open(‘data.dat’, ‘wb’:
pickle. dump(data)

18. Write statements to open a binary file C:\Myfiles\Text1.txt in read and write mode by specifying the file path in two different formats.

19. What are the advantages of saving data in:
(i) Binary form
(ii) Text form

20. Write a statement in Python to perform the following operations:
• To open a text file “MYPET.TXT” in write mode
• To open a text file “MYPET.TXT” in read mode

21. hat is the output of following code fragment? Explain.
out = open(“output.txt”, “w”)
out.write(“Hello, world! \n”)
out.write(“How are you?”)
print (open(“output.txt”).read())

22. What is the function of csv.reader object?


23. Write a program to read a text file line by line and display each word separated by a #.
24. Following code is written to update a record in a file opened with following code:
import pickle
fin = open(“Stu.dat’, ‘rb’)
while True:
_____ = fin.tell() # Line 1: store file-pointer position before reading the record
stu = pickle.load(fin))
if stu[‘Marks’] in [92, 93, 94]:
stu[ ‘Marks’] += 3 #changes made in the record
fin._____ (_____) # Line 2 :place the file-pointer at the exact location of the record
pickle.dump(stu, fin) # now write the updated record on the exact location
Fill in the blanks in Lines 1 and 2 to complete the code.

25. Radha Shah is a programmer, who has recently been given a task to write a python code to perform the following CSV file operations with the help of two user defined functions/modules:
a. CSVOpen() : to create a CSV file called BOOKS.CSV in append mode containing information of books – Title, Author and Price.
b. CSVRead() : to display the records from the CSV file called BOOKS.CSV where the field title starts with ‘R’.
She has succeeded in writing partial code and has missed out certain statements, so she has left certain queries in comment lines.
import csv
def CSVOpen():
with open(‘books.csv’,’______’,newline=”) as csvf: #Statement-1
cw=______ #Statement-2
______ #Statement-3
def CSVRead():
with open(‘books.csv’,’r’) as csvf:
for r in cr:
if r[0][0]==’R’:
print(‘File Not Found’)
You as an expert of Python have to provide the missing statements and other related queries based on the following code of Radha.
Answer any four questions (out of five) from the below mentioned questions.
i) Choose the appropriate mode in which the file is to be opened in append mode (Statement 1)
a. w+
b. ab
c. r+
d. a
ii) Which statement will be used to create a csv writer object in Statement 2.
a. csv.writer(csvf)
b. csv.writer(csvf)
c. csvf.writer()
d. cs.writer(csvf)
iii) Choose the correct option for Statement 3 to write the names of the column headings in the CSV file, BOOKS.CSV.
a. cw.writerow(‘Title’,’Author’,’Price’)
b. cw.writerow([‘Title’,’Author’,’Price’])
c. cw.writerows(‘Title’,’Author’,’Price’)
d. cw.writerows([‘Title’,’Author’,’Price’])

26. Alam has a list containing 10 integers. You need to help him create a program with separate user defined functions to perform the following operations based on this list.
● Traverse the content of the list and push the even numbers into a stack.
● Pop and display the content of the stack.
For Example: If the sample Content of the list is as follows:
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be: 38 22 98 56 34 12


27. Write a program to read a text file and display the count of vowels and consonants

28. Write the use and syntax for the following methods:
a) open() b) read() c) seek() d) dump()

29. Write the file mode that will be used for opening the following files. Also, write the Python statements to open the following files:

a) a text file “example.txt” in both read and write mode

b) a binary file “bfile.dat” in write mode

c) a text file “try.txt” in append and read mode

d) a binary file “btry.dat” in read only mode.

30. SHRUTHI has to complete her file-based assignment by tonight. She has been given the following text file (Education.txt):
Higher education improves quality of life. College graduates have longer life spans. Education is birthright.
Shruthi has also received the following incomplete code.
def fileFunction1 (____,____) #Fill_Line5
fi = ___(fname,___) #Fill_Line6
fi._______ #Fill_Line7
fi._______ #Fill_Line8
def fileFunction2(fname, N1, N2):
fi = open(fname)
print( )
a = fi.readlines ()
print(a) N1 = 16 #Line1
N2 = 22 #Line2
String = “India strengthening” #Line3
fileFunction1( ____,____) #Fill_Line4
fileFunction2 ( ‘Education.txt’, N1, N2)
Help her to complete her assignment as per the following instructions.
a. Complete Fill_Line4 so that the function call to FileFunction1() passes two arguments: First as the filename and the second as the string given in Line 3.
b. Complete Fill_Line5 so that the function header is as per its function call.
c. Complete Fill_Line6 so that the file is opened in a mode that will allow it to write the string at the end of the file without deleting anything from the file.
d. Complete Fill_Line7 and FilI_Line8 so that the passed string is written on to the file, followed by a newline character.

31. Rohit, a student of class 12th, is learning CSV File Module in Python. During examination, he has been assigned an incomplete python code (shown below) to create a CSV File ‘Student.csv’ (content shown below). Help him in completing the code which creates the desired CSV File.
CSV File
Incomplete Code
import_____ #Statement-1
fh = open(_____, _____, newline=”) #Statement-2
stuwriter = csv._____ #Statement-3
data = []
header = [‘ROLL_NO’, ‘NAME’, ‘CLASS’, ‘SECTION’]
for i in range(5):
roll_no = int(input(“Enter Roll Number : “))
name = input(“Enter Name : “)
Class = input(“Enter Class : “)
section = input(“Enter Section : “)
rec = [_____] #Statement-4
stuwriter. _____ (data) #Statement-5

i) Identify the suitable code for blank space in line marked as Statement-1.
a) csv file
b) CSV
c) csv
d) Csv
ii) Identify the missing code for blank space in line marked as Statement-2?
a) “School.csv”,”w”
b) “Student.csv”,”w”
c) “Student.csv”,”r”
d) “School.csv”,”r”

iii) Choose the function name (with argument) that should be used in the blank space of line marked as Statement-3
a) reader(fh)
b) reader(MyFile)
c) writer(fh)
d) writer(MyFile)
iv) Identify the suitable code for blank space in line marked as Statement-4.
c) ‘roll_no’,’name’,’Class’,’section’
d) roll_no,name,Class,section c) co.connect()

v) Choose the function name that should be used in the blank space of line marked as Statement-5 to create the desired CSV File?
a) dump()
b) load()
c) writerows()
d) writerow()

32. Arun, during Practical Examination of Computer Science, has been assigned an incomplete search() function to search in a pickled file student.dat. The File student.dat is created by his Teacher and the following information is known about the file.
• File contains details of students in [roll_no,name,marks] format.
• File contains details of 10 students (i.e. from roll_no 1 to 10) and separate list of each student is written in the binary file using dump().

Arun has been assigned the task to complete the code and print details of roll number 1.
def search():
f = open(“student.dat”,____) #Statement-1
____: #Statement-2
while True:
rec = pickle.____ #Statement-3
if(____): #Statement-4
____ #Statement-5

i) In which mode Arun should open the file in Statement-1?
a) r
b) r+
c) rb
d) wb
ii) Identify the suitable code to be used at blank space in line marked as Statement-2
a) if(rec[0]==1)
b) for i in range(10)
c) try
d) pass
iii) Identify the function (with argument), to be used at blank space in line marked as Statement-3.
a) load()
b) load(student.dat)
c) load(f)
d) load(fin)
iv) What will be the suitable code for blank space in line marked as Statement-4.
a) rec[0]==2
b) rec[1]==2
c) rec[2]==2
d) rec[0]==1
v) Which statement Arun should use at blank space in line marked as Statement-4 to close the file.
a) file.close()
b) close(file)
c) f.close()
d) close()


Leave a Reply

Your email address will not be published. Required fields are marked *