0% found this document useful (0 votes)
18 views5 pages

Class XII Computer Science Test July 2022

The document is a monthly test paper for Class XII Computer Science at Kendriya Vidyalaya Malanjkhand, dated July 2022. It includes multiple-choice questions, programming tasks related to text and binary file handling in Python, and CSV file operations. The test assesses students' understanding of file operations, data handling, and programming skills in Python.

Uploaded by

singhakhlesh186
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Class XII Computer Science Test July 2022

The document is a monthly test paper for Class XII Computer Science at Kendriya Vidyalaya Malanjkhand, dated July 2022. It includes multiple-choice questions, programming tasks related to text and binary file handling in Python, and CSV file operations. The test assesses students' understanding of file operations, data handling, and programming skills in Python.

Uploaded by

singhakhlesh186
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

KENDRIYA VIDYALAYA MALANJKHAND

Monthly Test July 2022


Class XII Subject : Computer Science
Time Duration: 90 minutes Max Marks : 50
---------------------------------------------------------------------------------------------------------------
Instructions:
1. Attempt all questions. All questions are compulsory.
2. In MCQ, choose only one correct option from the given options.

Section ‘A’ Multiple choice questions (MCQ)


1. Which function opens file in python? 1
(a) OPEN()
(b) Open()
(c) open()
(d) None of the above
Ans (c)
2. Which mode create a new file if the file does not exist? 1
(a) read()
(b) write()
(c) append()
(d) Both (b) and (c)
Ans (d)
3. Which statement will return one line from a file (file object is ‘f’)? 1
(a) [Link]( )
(b) [Link]( )
(c) [Link]( )
(d) [Link]()
Ans (a)
4. readlines() method return _________ 1
(a) String
(b) List
(c) Dictionary
(d) Tuple
Ans (b)
5. Which symbol is used for append mode? 1
(a) ap
(b) a
(c) aw
(d) w
Ans (b)
6. Which function is used to write data in binary mode? 1
(a) write()
(b) writelines ()
(c) pickle ()
(d) dump ()
Ans (d)
7. Which of the following is an invalid mode of file opening? 1
(a) read only mode
(b) write only mode
(c) read and write mode
(d) write and append mode
Ans (d)
8. What is full form of CSV 1
(a) Comma Separation Value
(b) Comma Separated Value
(c) Common Syntax Value
(d) Comma Separated Variable

1
Ans (b)
9. Which statement will open file “[Link]” in append mode? 1
(a) f = open(“[Link]” , “a”)
(b) f = Open(“[Link]” , “ab”)
(c) f = new(“[Link]” , “a”)
(d) open(“[Link]” , “a”)
Ans (a)
10. _______ function returns the current position of file pointer. 1
(a) get( )
(b) tell( )
(c) cur( )
(d) seek( )
Ans (b)
Section ‘B’ Text File Handling in Python
11. Write a program in Python to write the following lines in a text file 4
"[Link]". Read the file and show the contents on the screen.

 Python is an easy language.


 The Python language is designed to make developers life easy
Ans. fileobj = open('[Link]','w')
[Link]('Python is an easy language\n')
[Link]('The Python language is designed to make developers life
easy')
[Link]()

fileobj = open('[Link]','r')
data = [Link]()
print(data)
12. What is the difference between
(a) readline() and readlines() function in python 2
(b) ‘w’ and ‘a’ file mode 2
Ans. (a) readline() method will return a line from the file when called. readlines()
method will return all the lines in a file in the format of a list where each
element is a line in the file.
(b) ‘w’ mode used to open file in write mode. Python will create a new file.
Overwrites the file if file exist.
‘a’ is used to add more contents to the file if it exist. Creates a new file if file
does not exist.
13. Write program in python to 4
(a) count number of alphabets in a file
(b) to show only those lines which begins with the letter ‘T’
Ans. (a) filename = input('Enter file name ')
fobj = open(filename,'r')
string = [Link]()
count = 0
for ch in string:
if [Link]():
count = count + 1
print('Total alphabets ',count)
(b) filename = input('Enter file name ')
fobj = open(filename,'r')
line = [Link]()
while line!='':
if line[0]=='T':
print(line)
line = [Link]()
14. (a) Consider following lines for the file [Link] and predict the output: 2

2
Friends are crazy, Friends are naughty !
Friends are honest, Friends are best !
Friends are like keygen, friends are like license key !
We are nothing without friends, Life is not possible without friends !

f = open("[Link]")
[Link]()
print([Link]())
[Link]()
print([Link]())
[Link]()
Ans Friends are honest, Friends are best ! 2

We are nothing without friends, Life is not possible without friends !


(b) f = open(“[Link]”)
Consider the code given above and write the answers of the following:
(i) Identify name of the file.
(ii) Identify name of the function used above.
(iii) What is ‘f’ in above code?
(iv) The above statement will __________ file in ________ mode.
Ans (i) [Link]
(ii) open()
(iii) File object
(iv) open read

Section ‘C’ Binary File Handling in Python


15. Write a program in python to search and display details, whose destination is 4
“Balaghat” from binary file “[Link]”. Assuming the binary file is
containing the following elements in the list:

Bus Number
Bus Starting Point
Bus Destination
Ans. import pickle
fobj=open("[Link]","rb")
num = 0
try:
while True:
rec=[Link](fobj)
if rec[2]=="Balaghat" or rec[2]=="balaghat":
num = num + 1
print(rec[0],rec[1],rec[2])
except:
[Link]()
16. A binary file “[Link]” has structure [worker id, worker name, pay]. Write 4
a program in Python that would read contents of the file “[Link]” and
display the details of those workers whose payment is above 21000.
Ans. num=0
fobj=open("[Link]","rb")
try:
print("Worker ID\tWorker Name\tPay")
while True:
rec=[Link](fobj)
if rec[2]>20000:
print(rec[0],"\t\t",rec[1],"\t\t",rec[2])
except:
[Link]()

3
17. Find errors in the following program code:
import pickled 4
fobj=open("[Link]", "br")
Author = input('Enter author name ')
num = 0
try:
WHILE True:
rec=[Link](fobj)
if Author==rec[2]:
num = num + 1
print(rec[0],rec[1],rec[2],rec[3])
except:
[Link]()
Ans. import pickle
fobj=open("[Link]", "rb")
Author = input('Enter author name ')
num = 0
try:
while True:
rec=[Link](fobj)
if Author==rec[2]:
num = num + 1
print(rec[0],rec[1],rec[2],rec[3])
except:
[Link]()
Section ‘D’ csv File Handling in Python
18. Write
(a) Difference between writerow() and writerows() function 2
(b) Name of the module used in csv file handling 1
Ans. (a) The technical difference is that writerow is going to write a list of values into
a single row whereas writerows is going to write multiple rows from a buffer
that contains one or more lists.
(b) csv module

19. Write a program to copy the data from “[Link]” to “[Link]” 3


Ans import csv
f=open("[Link]","r")
f1=open("[Link]",'w')
d=[Link](f)
d1=[Link](f1)
for i in d:
[Link](i)
[Link]( )
[Link]( )

20. Sushmita has a CSV file “[Link]” which has name, class and marks 2
separated by comma. She is writing a Python program to copy only the name
and class to another CSV file “[Link]”. She has written the following code.
As a programmer, help her to fill up the blanks for successfully executing the
given task.
import csv
file = open('[Link]', _________ , newline="");
writer = [Link](file)

________ open('[Link]') as csvfile:


data = csv.______ (csvfile)

4
for row in data:
[Link]([_________ , ________ ])
[Link]()
Ans: w
with
reader
row[0],row[1]
21. What is the output of the following program if the [Link] file contains 2
following data?

Mohit, 200
Pankaj, 400

import csv
d = [Link](“[Link]”)
next (d)
for row in d:
print (row);
Ans Pankaj,400
22. Write a program to write into file “[Link]” Rollno, Name and Marks 2
separated by comma. It should have header row and then take in input from the
user for all following rows. The format of the file should be as shown if user
enters 2 records.

[Link],Name,Marks
20,Harish,67
56,Nikita,69
Ans import csv
f1=open('[Link]','w',newline=‘ ')
w1=[Link](f1,delimiter = ",")
[Link](['[Link]', 'Name', 'Marks'])
while True:
print ("Enter 1 to continue adding, 0 to exit")
op = int(input("Enter Option"))
if (op == 1):
rollno = int(input("Enter Roll No"))
name = input("Enter Name")
marks = int(input("Enter Marks"))
wlist = [rollno,name,marks]
[Link](wlist)
elif op == 0:
break;
[Link]()

You might also like