Class XII First Term Computer Science Examination 2022-23 Sample Question Paper Answer Key
Time: 2 Hours Maximum Marks: 35
1. Identify the correct options: [1]
a) int, float, string, bool and complex are the built-in data type of Python
b) int, float, string, bool and complex are the fundamental data type of Python
c) List, tuple and dictionary are not the built-in data type of Python
d) List, tuple and dictionary are also the fundamental data type of Python
2. Identify the correct options: [1]
a) None is a Python identifier
b) None is a Python literal
c) None is a Python keyword
d) None is a Python delimiter
3. Identify the correct options: [1]
a) Keywords break and continue can be used without a loop
b) Keyword return can be used outside a function
c) Keyword return can only be used inside a function
d) Keywords break and continue can only be used inside a loop
4. Identify the correct options: [1]
a) A Python formal parameter has no data type
b) A Python actual parameter has a data type
c) A Python formal parameter has a data type
d) A Python actual parameter has no data type
5. Identify the correct options: [1]
a) A binary file can be opened as a text file
b) Any CSV file can be opened as a text file
c) A binary file cannot be opened as a text file
d) To open a CSV file, one must use csv module
6. Identify the correct options: [1]
a) Default delimiter of a CSV file can be changed with sep
b) A CSV file does not have any default delimiter
c) A CSV file has a default delimiter comma (',')
d) Default delimiter of a CSV file can be changed with delimiter
7. Identify the correct options: [1]
a) pickle module function dump() has one positional argument
b) pickle module function dump() has two positional arguments
c) pickle module function load() has two positional arguments
d) pickle module function load() has one positional argument
8. Rewrite the Python script after removing all the errors. Underline the corrections. [2]
file=OPEN("HOLIDAY.TXT") file=open("HOLIDAY.TXT")
line=file.readline() line=file.readline()
while line: while line:
print(text.strip()) print(line.strip())
line=file.readline() line=file.readline()
c=tell() c=file.tell())
print(No. of characters=, c) print('No. of characters=', c)
file.close() file.close()
Page 1/4
Class XII First Term Computer Science Examination 2022-23 Sample Question Paper Answer Key
9. Give the output of the Python scripts given below: [2+3=5]
a) day='THURSDAY' Output
print(day[1:-3], day[-2:2:-1]) HURS ADSR
print(day[-7::2], day[6::-2]) HRDY ASUT
b) def pyfunc(a, b):
global c
a+=a+b+c
b+=a+b+c
c+=a+b+c
print(a, b, c)
x, y, c=3, 5, 7 Output
pyfunc(x, y) 18 35 67
pyfunc(y, c) 144 345 623
10. Write any two differences between value parameter and reference parameter. [2]
Value Parameter Reference Parameter
• Copy of actual parameter • Alias of actual parameter
• Change in value parameter does not update • Change in reference parameter, updates actual
actual parameter parameter
11. Write any two differences between mutable type and immutable type. Give an example of each. [3]
Mutable Type Immutable type
• Editing / updating mutable type, object retains • Editing / updating immutable type, object gets
its old ID a new ID
• Passed by reference to a function • Passed by value to a function
• Example: list, dictionary • Example: int, float, str, tuple
12. Write a Python function to read and display a text file 'UCL2022.TXT'. At the end count and display
number of white space characters (space / tab / new line) present in the text file. [3]
def countconsonants():
fobj=open('UCL2022.TXT')
text=fobj.read()
print(text)
c=0
for ch in text:
if ch in ' \t\n': c+=1 #OR, if ch.isspace(): c+=1
print('White Space characters=', c)
fobj.close()
13. Write a Python function to read and display a text file 'UCL2022.TXT'. At the end display number of
words and number of lines present in the text file. [3]
def countconsonants():
fobj=open('UCL2022.TXT')
text=fobj.read()
print(text)
words=text.split()
lines=text.split('\n')
print('Number of words=',len(words))
print('Number of lines=',len(lines))
fobj.close()
Page 2/4
Class XII First Term Computer Science Examination 2022-23 Sample Question Paper Answer Key
14. A CSV data file 'STOCK.CSV' containing following data: code,item,price,disc
• code is integer, item is string, price is float and disc (discount) is float
• disc is calculated as 15% of price
a) Write a Python function to append a record in the CSV data file 'STOCK.CSV'. [3]
import csv
def append():
fobj=open('STOCK.CSV', 'a', newline="")
cwobj=csv.writer(fobj)
code=int(input('Code? '))
item=input('Name? ').upper()
price=float(input('Price? '))
rec=[code,item,price,0.15*price]
cwobj.writerow(rec)
fobj.close()
b) Write a Python function to read the CSV data file 'STOCK.CSV' and display those records where
price<100. At the end display number of such records found. [4]
import csv
def search():
fobj=open('STOCK.CSV')
ilist=csv.reader(fobj)
c=0
for rec in ilist:
if float(rec[3])<100:
print(rec[0],rec[1],rec[2],rec[3],sep='\t')
c+=1
fobj.close()
print('Number of Records=', c)
15. A binary data file 'EMPLOYEE.DAT' storing employee records where every record is a list containing
following data: eno,name,desig,bsal
eno (employee number) is integer, name is string, desig (designation) is string and bsal (basic
salary) is float and
a) Write a Python function to append n (n is parameter to the function) records in the binary data
file 'EMPLOYEE.DAT'. [3]
import pickle
def addrecords(n):
fobj=open('EMPLOYEE.DAT', 'ab')
for x in range(n):
eno=int(input('Employee Number? '))
name=input('Name? ').upper()
desig=input('Designation? ').upper()
bsal=float(input('Basic Salary? '))
emp=[eno,name,desig,bsal]
pickle.dump(emp,fobj)
fobj.close()
b) Write a Python function to read the binary data file 'EMPLOYEE.DAT' and display those records
where desig is 'MANAGER' and bsal>=2.5L. If no such record is found then display an
appropriate message. [4]
import pickle
def searchrecs():
fobj=open('EMPLOYEE.DAT', 'rb')
Page 3/4
Class XII First Term Computer Science Examination 2022-23 Sample Question Paper Answer Key
fobj.seek(0,2)
eof=fobj.tell()
fobj.seek(0)
c=0
while fobj.tell()<eof:
emp=pickle.load(fobj)
if emp[2]=='MANAGER' and emp[3]>=250000.0:
print(emp]0],emp[1],emp[2],emp[3],sep='\t')
c+=1
if c==0: print('No Records Found!')
fobj.close()
Working of 9. b) output question:
def pyfunc(a,b): First call, pyfunc(x, y) Second call, pyfunc(y, c)
global c global c will update global variable c global c will update global variable c
a+=a+b+c created outside pyfunc(). created outside pyfunc().
b+=a+b+c a=3, b=5, c=7 a=5, b=67, c=67
c+=a+b+c a+=a+b+c => a=2*3+5+7 = 18 a+=a+b+c => a=2*5+67+67 = 144
print(a, b, c) a=18, b=5, c=7 a=144, b=67, c=67
b+=a+b+c => b=18+2*5+7 = 35 b+=a+b+c => b=144+2*67+67 = 345
x, y, c=3, 5, 7 a=18, b=35, c=7 a=144, b=345, c=67
pyfunc(x, y) c+=a+b+c => c=18+35+2*7= 67 c+=a+b+c => c=144+345+2*67= 623
pyfunc(y, c) Output: 18 35 67 Output: 144 345 623
Return to main, global variables x and
Output y don’t get updated but global variable
18 35 67
c gets updated to 67. After the first call
144 345 623
of pyfunc(), x=3, y=5, c=67
Page 4/4
Class XII Computer Science First Term Exam 2022-23 Answer Key Set A
1. Identify the correct options: [1]
a) A Python while-loop may not be nested
b) A Python for-loop must contain a range() function after in
c) A Python while-loop must contain an expression
d) A Python for-loop must contain an iterable object after in
2. Identify the correct options: [1]
a) Python data types int, float and str are immutable type
b) Python data types int, float and str are passed by value to a function
c) Python data types int, float and str are mutable type
d) Python data types int, float and str are passed by reference to a function
3. Identify the correct options: [1]
a) A tuple type object does not support negative index
b) A string type object supports negative index
c) A tuple type object supports negative index
d) A string type object does not support negative index
4. Identify the correct options: [1]
a) In a dictionary, key is immutable type and value can be immutable type
b) In a dictionary, key is immutable type and value can only be mutable type
c) In a dictionary, key is immutable type and value can be mutable type
d) In a dictionary, key is mutable type and value can only be immutable type
5. Identify the correct options: [1]
a) Using keyword with, a file object is required for any file operation
b) Using keyword with, a file can be opened without open()
c) Using keyword with, a file object is not required for any file operation
d) Using keyword with, a file is closed without close()
6. Identify the correct options: [1]
file=open('CLASS.DAT')
a) CLASS.DAT cannot be opened since mode is missing
b) CLASS.DAT is opened as a text file
c) CLASS.DAT is opened in read mode
d) CLASS.DAT is opened as a binary file
7. Identify the correct options: [1]
a) seek(0, 2) takes the file pointer to the end of the file
b) seek(2, 0) takes the file pointer to the end of the file
c) File pointer is positioned at the end of the file in append mode
d) File pointer is positioned at the beginning of the file in append mode
8. Rewrite the Python script after removing all the errors. Underline the corrections. [2]
myfile=open("SOCCER.TXT", rt) myfile=open("SOCCER.TXT", "rt")
mydata=read(myfile) mydata=myfile.read()
print(data) print(mydata)
mydata.close() myfile.close()
9. Give the output of the Python scripts given below: [2+3=5]
a) word='BRILLIANCE' Output
print(word[:5], word[5:]) BRILL IANCE
print(word[::2], word[9::-2]) BILAC ENILR
Page 1/4
Class XII Computer Science First Term Exam 2022-23 Answer Key Set A
b) def pyfunc(a, b):
global c
a*=b
b+=b+c
c+=a+b+c
print(a, b, c)
Output
x, y, c=2, 4, 5 10 15 35
pyfunc(x, c) 140 105 315
pyfunc(y, c)
10. Write any three differences between list type and tuple type. [3]
List type Tuple type
• Mutable type • Immutable type
• Elements are enclosed within [] • Elements are enclosed within ()
• Element can be deleted / updated • Element cannot be deleted / updated
• Pass by reference to a function • Pass by value to a function
11. Write any two differences between local variable and global variable. [2]
Local variable Global variable
• Created inside a function • Created either outside or inside a function
• Scope is in the function in which it is created • Scope is entire program script
12. Write a Python function to read and display a text file 'EIDBREAK.TXT'. At the end count and display
number of uppercase consonants present in the text file. [3]
def countconsonants():
fobj=open('EIDBREAK.TXT')
text=fobj.read()
print(text)
c=0
for ch in text:
if 'A'<=ch<='Z' and ch not in 'AEIOU': c+=1
#OR, if ch.isupper() and ch not in 'AEIOU': c+=1
print('Uppercase Consonants=', c)
fobj.close()
13. Write a Python function to read and display a text file 'EIDBREAK.TXT'. At the end count and display
number of words starting with a digit present in the text file. [3]
def countwords():
fobj=open('EIDBREAK.TXT')
text=fobj.read()
print(text)
wordlist=text.split()
c=0
for word in wordlist:
if '0'<=word[0]<='9': c+=1
#OR, if word[0].isdigit(): c+=1
print('Words=', c)
fobj.close()
14. A CSV data file 'STAFF.CSV' containing following data: code,name,sub,nop
code is integer, name is string, sub (subject) is string and nop (number of periods) is integer
Page 2/4
Class XII Computer Science First Term Exam 2022-23 Answer Key Set A
a) Write a Python function to append n (n is a parameter to the function) records in the CSV data
file 'STAFF.CSV'. [3]
import csv
def append(n):
fobj=open('STAFF.CSV', 'a', newline="")
cwobj=csv.writer(fobj)
for x in range(n):
code=int(input('Code? '))
name=input('Name? ').upper()
sub=input('Subject? ').upper()
nop=int(input('Number of Periods? '))
rec=[code,name,sub,nop] #rec=(code,name,sub,nop)
cwobj.writerow(rec)
fobj.close()
OR,
def append(n):
fobj=open('STAFF.CSV', 'a', newline="")
cwobj=csv.writer(fobj)
slist=[]
for x in range(n):
code=int(input('Code? '))
name=input('Name? ').upper()
sub=input('Subject? ').upper()
nop=int(input('Number of Periods? '))
rec=[code,name,sub,nop] #rec=(code,name,sub,nop)
slist+=[rec] #slist.append(rec)
cwobj.writerows(slist)
fobj.close()
b) Write a Python function to read and display the CSV data file 'STAFF.CSV'. At the end display
number of records where sub is 'MATH' and nop<26. [4]
import csv
def searchrec():
fobj=open('STAFF.CSV')
slist=csv.reader(fobj)
c=0
for rec in slist:
print(rec[0],rec[1],rec[2],rec[3],sep='\t')
if rec[2]=='MATH' and int(rec[3])<26: c+=1
fobj.close()
print('Number of Records=', c)
15. A binary data file 'HOUSE.DAT' storing student records where every record is a list containing
following data: adno,name,grade,house,area
adno is integer, name is string, grade is string ('12A', '12B' …, '11A', '11B', … '10A', '10B', …),
house is string and area is string
a) Write a Python function to append n (n is local variable in the function) records in the binary
data file 'HOUSE.DAT'. [3]
import pickle
def addrecords():
fobj=open('HOUSE.DAT', 'ab')
n=int(input('Number of Records? '))
for x in range(n):
adno=int(input('Admission Number? '))
Page 3/4
Class XII Computer Science First Term Exam 2022-23 Answer Key Set A
name=input('Name? ').upper()
grade=input('Grade? ').upper()
house=input('House? ').upper()
area=input('Area? ').upper()
student=[adno,name,grade,house,area]
pickle.dump(student,fobj)
fobj.close()
b) Write a Python function to read the binary data file 'HOUSE.DAT' and display those records where
house is either 'FAITH' or 'TRUST'. If no such record is found then display an appropriate
message. [4]
import pickle
def searchrecords():
fobj=open('HOUSE.DAT', 'rb')
fobj.seek(0,2)
eof=fobj.tell()
fobj.seek(0)
c=0
while fobj.tell()<eof:
stu=pickle.load(fobj)
if stu[3] in ['FAITH', 'TRUST']:
#OR, if stu[3]=='FAITH' or stu[3]=='TRUST':
print(stu]0], stu[1], stu[2], stu[3], stu[4], sep='\t')
c+=1
if c==0: print('No Records Found!')
fobj.close()
Page 4/4