Pushpathur, Palani – 624 618
Class: XII Session: 2023-24
Computer Science (083) – Revision Test 04
Maximum Marks: 70 Time Allowed: 3
Hrs
General Instructions:
1.This question paper contains five sections, Section A to E.
2. All questions are compulsory.
3. Section A have 18 questions carrying 01 mark each.
4. Section B has 07 Very Short Answer type questions carrying 02 marks each.
5. Section C has 05 Short Answer type questions carrying 03 marks each.
6. Section D has 03 Long Answer type questions carrying 05 marks each.
7. Section E has 02 questions carrying 04 marks each.
8.All programming questions are to be answered using Python Language only.
SECTION A [ 18 X 1 = 18 Marks ]
1. 1
A). To read three characters from a file object f, we use ……… .
(a) f.read(3) (b) f.read() (c) f.readline() (d) f.readlines()
B). The files that consists of human readable characters
(a) binary file (b) text file (c) Both (a) and (b) (d) None of these
2. 1
A). Which function is used to write a list of string in a file?
(a) writeline() (b) writelines() (c) writestatement() (d) writefullline()
B). What will be the output of the following Python code?
myFile = None
for i in range (8):
with open(“data.txt”, “w”) as myFile:
if i > 5:
break
print(myFile.closed)
(a) True (b) False (c) None (d) Error
3. 1
A) What will be the output of the following Python code?
myFile = open(“story.txt”, “wb”)
print(“ Name of the file: ”, myFile.name)
myFile.flush()
myFile.close()
Suppose the content of the file ‘‘story.txt’’ is
Education Hub
Learning is the key of success.
(a) Compilation error (b) Runtime error (c) No output (d) Flushes the file when closing them
B) What is the output of following code?
myfile=open(‘story.txt’,‘r’)
s=myfile.read(10)
print(s)
s1=myfile.read(15)
print(s1)
myfile.close()
a) Education b) Education c) Education d) Education Hub
Hub Learning Hub Learning is Hub Learning is
is Learning
is
4. Suppose the content of the file ‘‘story.txt’’ is 1
Education Hub
Learning is the key of success.
A) What is the output of following code?
f=open(‘story.txt’, ‘r’)
s=f.readline()
lcount=len(s)
print(lcount)
f.close( )
(a) 4 ( b) 2 (c) 1 (d) 8
B) What is the output of following code?
f=open(‘story.txt’, ‘r’)
str=“ ”
s=0
ts=0
while str:
str=f.readline()
ts=ts+len(str)
print(ts)
f.close()
(a) 44 (b) 43 (c) 37 (d) 45
5. Suppose the content of file ‘‘Para.txt’’ is 1
Education Hub
Electronic learning
Learning is the key of success
A) What is the output of following code?
def test () :
s=open (“story.txt”, “r”)
f=s.read()
z=f.split( )
count=0
for i in z:
count=count+1
print(count)
(a) 7 (b) 8 (c) 10 (d) 9
B) What is the output of following code?
def test() :
f=open (“Para.txt”, “r”)
lines=0
l=f.readlines( )
for i in l:
if i [0] = = ‘E’ :
lines+=1
print(lines)
(a) 2 (b) 1 (c) 3 (d) Error
6. A) Binary files commonly have the extension : 1
a) .TXT b) .DAT c) .DOC d) .PDF
B) The method ________________is used to read data from a binary file. (Ans : load())
7. A) State whether True or False : 1
When we open a file in append mode the previous data is erased. – Ans : False
B) Which module is required to use the built-in function dump() ?
a) math b) flush c) pickle d) unpickle
8. A) The mode used for both writing and reading data from a binary file : 1
a) wb+ b) w c) wb d) a
B) State whether True or False :
The type of operation that can be performed on a file depends upon the mode in which the file
is opened. (Ans : True)
9. A) The module pickle is used for which of the methods in file handling : 1
(a) open( ) and close( ) (b) read( ) and write( ) (c) dump( ) and load( ) (d) None of the above
B)The _____________statement automatically closes the file after processing on the file gets
over.(Ans : With)
10 A) The ___________module is used to read data from a binary file. (ans : pickle 1
B) The method used to write an object into a binary file is :
a) load() b) dump() c) write() d) None of these
A) If a text file is opened in w+ mode, then what is the initial position of file pointer/cursor? 1
11 a. Beginning of file
b. End of the file
c. Beginning of the last line of text file Undetermined
B) Which of the following statements are true?
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 iscreated
c. When you open a file for writing, if the file exists, the existing file is overwritten
with the new file
d. All of the mentioned
12 A) Ans : C 1
B)
Ans : D
13 A) Ans :C 1
B) Ans : A
14 A)To read the entire remaining contents of the file as a string from a file object myfile,we use 1
a. myfile.read(2) b. myfile.read() c. myfile.readline() d. myfile.readlines()
B) A text file is opened using the statement f = open(‘story.txt’). The file has a total of
10 lines. Which of the following options will be true if statement 1 and statement 2 are
executed in order.
Statement 1: L1 = f.readline( )
Statement 2: L2 = f.readlines( )
a. L1 will be a list with one element and L2 will be list with 9 elements.
b. L1 will be a string and L2 will be a list with 10 elements.
c. L1 will be a string and L2 will be a list with 9 elements.
d. L1 will be a list with 10 elements and L2 will be an empty list.
15 A) Which function of a file object can be used to fetch the current cursor position in 1
terms of number of bytes from beginning of file?
a. seek( ) b. bytes( ) c. tell( ) d. fetch( )
B) Ans:a
a. AllngsAll b. AllcanAll c. Allcancan d. Allngscan
16 A)Ans : d 1
B)Ans : C
17 A)Which functions is used to close a file in python? 1
(a) close (b) cloose() (c) Close() (d) close()
Ans: (D) close()
B). Which of the following python statement will bring the read pointer to 10th character from
the end of a file containing 100 characters, opened for reading in binary mode.
a) File.seek(10,0) b) File.seek(-10,2) c) File.seek(-10,1) d) File.seek(10,2)
Ans: File.seek(-10,2)
18 A). Which of the file opening mode will open the file for reading and writing in binary mode. 1
a) rb b) rb+ c) wb d) a+
Ans: b) rb+
B)ASSERTION AND REASONING based questions.
Mark the correct choice as
(a) Both A and R are true and R is the correct explanation for A
(b) Both A and R are true and R is not the correct explanation for A
(c) A is True but R is False
(d) A is false but R is True
Assertion (A): A binary file in python is used to store collection objects like lists and
dictionaries that can be later retrieved in their original form using pickle module.
Reasoning (R): A binary files are just like normal text files and can be read using a text editor
like notepad.
Ans: A is True and R is False
SECTION – B [ 7 X 2 = 14 Marks ]
19. Write a single loop to display all the contens of a text file file1.txt after removing leading and 2
trailing WHITESPACES
out=open('output.txt','w')
out.write('hello,world!\n')
out.write('how are you')
out.close( )
open('output.txt').read( )
Ans:
for line in open(“file1.txt”):
print(line.strip())
20 Read the code given below and answer the questions 2
f1=open('main.txt','w')
f1.write('bye')
f1.close()
If the file contains 'GOOD' before execution, what will be the content of the file after execution
of the code
Ans : The file would now contains “Bye”only because when an existing file is openend in write
mode .it truncates the existing data in file .
21 Observe the following code and answer the follow 2
f1=open("mydata","a")
_____________#blank1
f1.close()
(i) what type of file is mydata
(ii) Fill in the blank1 with statement to write "abc" in the file "mydata"
Ans
i) Text file
ii) f1.write(“abc”)
22 Q5. A given text file data.txt contains : 2
Line1\n
\n line3
Line 4
\n
line6
What would be the output of following code?
f1=open('data.txt')
L=f1.readlines()
print(L[0])
print(L[2])
print(L[5])
print(L[1])
print(L[4])
print(L[3])
Ans
Line1
Line3
Line 6
Line 4
23 Write in short : 2
i) What is the use of open( ) method ?
ii) What is the use of tell( ) method ?
24 i)What is the use of load( ) and dump( ) methods? 2
ii)Explain seek( ) method
25 Answer in brief : 2
i) Purpose of close( ) function.
ii) Purpose of flush( ) function.
SECTION – C [ 5 X 3 = 15 Marks ]
26 Differentiate Text file and Binary file 3
27 3
What do you mean by serialization and de-serialization ?
ANs :
a) Serialization is to convert objects into bitstreams which can be stored into files, whereas de-
serialization is used to reconstruct the original objects. It is also known as pickling
28 Write a program that reads a binary file “emp.dat” and displays all records of employees one by 3
one
Ans :
import pickle
f1=open("emp.dat","rb")
e=pickle.load(f1)
for x in e :
print(x)
f1.close( )
29 Write a program to count the words “to” or “the” present in a text file “python.txt”. 3
Ans.
fname = "python.txt"
num_words = 0
f= open(fname, 'r')
words = f.read().split()
for a in words:
if (a.tolower() == “to” or a.tolower() == “the” ):
num_words = num_words + 1
print("Number of words:", num_words)
f.close()
30 Write the different types of file modes available in python 3
SECTION – D [ 3 X 5 = 15 Marks ]
31 5
Ans : (i). (b) (ii). (d) (iii) (c) (iv). (b) (v).
(c)
32 5
i) In which mode Ramesh 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 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 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 statement-4?
a) rec[0]==2 b) rec[1]==2 c) rec[2]=2 d) rec[0]==1
v) Which statement should Ramesh use at blank space in statement-5 to close the file?
a) file.close( ) b) close(file) c) f.close( ) d) close( )
Ans :
i) Correct option is c) rb
ii) Correct option is c) try
iii) Correct option is c) load(f)
iv) Correct option is d) rec[0]==1
v) Correct option is c) f.close( )
33 5
A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price].
1. Write a user defined function CreateFile() to input data for a record and add to Book.dat .
2. Write a function CountRec(Author) in Python which accepts the Author name as parameter and
count and return number of books by the given Author are stored in the binary file “Book.dat
Ans:
import pickle
def createfile():
fobj=open("Book.dat","ab")
BookNo=int(input("Enter Book Number : "))
Book_name=input("Enter book Name :")
Author = input("Enter Author name: ")
Price = int(input("Price of book : "))
rec=[BookNo, Book_name ,Author, Price]
pickle.dump(rec, fobj)
fobj.close()
createfile() # This function is called just to verify result and not required in exam
def countrec(Author):
fobj=open("Book.dat", "rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if Author==rec[2]:
num = num + 1
print(rec[0],rec[1],rec[2],rec[3])
except:
fobj.close()
return num
n=countrec("amit") # This function is called just to verify result and not required in exam
print("Total records", n) # This statement is just to verify result and not required in exam
SECTION – E [ 2 X 4 = 8 Marks ]
34 4
10. A binary file “salary.DAT” has structure [employee id, employee name, salary]. Write a
function countrec() in Python that would read contents of the file “salary.DAT” and display the
details of those employee whose salary is above 20000.
As :
def countrec():
num=0
fobj=open("data.dat","rb")
try:
print("Emp id\tEmp Name\tEmp Sal")
while True:
rec=pickle.load(fobj)
if rec[2]>20000:
print(rec[0],"\t\t",rec[1],"\t\t",rec[2])
except:
fobj.close()
countrec()# This function is called to verify the result
35 Write a function countVowels() in Python, which should read 4
each character of a text file “myfile.txt”, count the number of vowels and display the
count.
Example:
If the “myfile.txt” contents are as follows:
This is my first class on Computer Science.
The output of the function should be: Count of vowels in file: 10
Answer:
def countVowels():
fobj = open(“myfile.txt”)
data = fobj.read()
count = 0
vowels=[‘a’,’e’,’I’,’o’,’u’]
for ch in data:
if ch in vowels:
count +=1
print(“Count of vowels in file:”, count)