XII-HALF CS 24-Set-C (24 BOARD) MS
XII-HALF CS 24-Set-C (24 BOARD) MS
SCHOOL
HALF YEARLY EXAMINATION: 2024-25
Class: XII
COMPUTER SCIENCE (083)
Maximum Marks: 100 SET-C Time Allowed : infinite hours
General Instructions:
Please check this question paper contains 36 questions.
The paper is divided into 4 Sections- A, B, C, D and E.
Section A, consists of 18 questions (1 to 18). Each question carries 100 Mark.
Section B, consists of 7 questions (19 to 25). Each question carries 0 Marks.
Section C, consists of 5 questions (26 to 30). Each question carries 0 Marks.
Section D, consists of 2 questions (31 to 32). Each question carries 0 Marks.
Section E, consists of 3 questions (33 to 36). Each question carries 0 Marks.
All programming questions are to be answered using Python Language only.
Ques
No Question Marks
SECTION - A
1 While defining a function in Python, the positional parameters in the function header must 1
Always be written after the default parameters.
===
False
2 Which of the following is an invalid statement? 1
a. abc=1,000,000300 b. a, b, c= 100,200,300
c. a b c=100 200 d. a=b=c=100
ans-c. a b c=100 200 300
3 Which of the following operators has the highest precedence? 1
(a) << (b) ** (c)%
4 What will be the output of the following Python code snippet? 1
d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 > d2
a) True b) False c) Error d) None
5 What will be the value of the given expression? 1
3+3.00, 3**3.0
(a) (6.0, 27.0) (b) (6.0, 9.00) (c) (6,27) (d) [6.0,27.0] (e) [6,27]
6 Select the correct output of the code: 1
a = "foobar"
a = a.partition("o")
(a) ["fo","","bar"] (b) ["f","oo","bar"]
(c) ["f","o","bar"] (d) ("f","o","obar")
7 If a text file is opened in w+ mode, then what is the initial position of file pointer/cursor? 1
(a) Beginning of file (b) End of the file
(c) Beginning of last line of text file (d) Undetermined
8 For a user defined function with header 1
def start (x, y, z=10):
Which of the following function call statements will generate an
error?
Anc-c
18 Assertion (A): Local Variables are accessible only within a function or block in which it is declared Reason 1
(R): Global variables are accessible in the whole program.
Ans-b
SECTION - B
19 Correct the following code segment: 2
DEF say(message, times = 1)
print(message * times , end : ’ ‘)
say(times=10, ‘Hello and’)
say('World')
===
(b) Sum = 0
for k in range(10 , 1, -2):
Sum = Sum+k
print(Sum)
=====
(a) typeerror
(b) 30
21 Predict the output of the Python code given below: 2
def modifyList(x):
x.append(sum(x))
print(x, end= ‘ ’)
L = [1,2,3,4]
print(L, end= ‘ ’)
modifyList(L)
print(L)
OR
25 Write A Function COUNTWORDS( ) That Read A Text File ‘PYTHON.TXT’ And Display The Total Number Of 2
Words which Begins By Uppercase Character.
def COUNTWORDS( ):
fin=open(‘PYTHON.TXT’, ‘r’)
count=0
for line in fin:
for i in line.split( ):
if i[0].isupper( ):
count+=1
print(count)
fin.close( )
COUNTWORDS( )
SECTION - C
26 Assume that a text file named TEXT1.TXT already contains some text written into it, write a program with 3
a function named vowelwords(), that reads the file TEXT1.TXT and create a new file named TEXT2.TXT ,
which shall contain only those words from the file TEXT1.TXT which don’t start with an uppercase
vowel(i.e. with ‘A’,’E’,’I’,’O’,’U’)
for example if the file TEXT1.TXT contains Carry Umbrella and Overcoat When it Rains then the file
TEXT2.TXT shall contain Carry and when it Rains.
OR
Write a Python program to count all the line having 'a' as last character.
=====
f1=open(“TEXT1.TXT”)
f2=open(“TEXT2.TXT”,”w”)
data=f1.read().split()
for word in data:
if word[0] not in “AEIOU”:
f2.write(word, “ “)
f1.close()
f2.close()
or
f1=open(“TEXT1.TXT”)
data=f1.readlines()
for line in data:
if line.strip()[-1]==”a”:
print(line)
f1.close()
27 Write a function merge_tuple(t1,t2), where t1 and t2 are the tuples of elements passed as argument to 3
the function. The function returns another tuple named that stores the sum of consecutive element of t1
and t2, only if they are of same length else return -1
For example:
T1=(1,2,3,4)
T2=(5,6,7,8)
Then function should return
(6,8,10,12)
And if
T1=(1,2,3)
T2=(5,6,7,8)
Then function should return -1
===
def merge_tuple(t1,t2):
t3=( )
if len(t1)==len(t2):
for i in range(len(t1)):
t3[i]=t1[i]+t2[i]
return t3
return -1
28 Rajiv has created a dictionary containing employee names and their salaries as key value pairs of 6 3
employees.
Write a program, with separate user defined functions to perform the following operations:
● Push the keys (employee name) of the dictionary into a stack, where the corresponding value (salary)
is less than 85000.
● Pop and display the content of the stack.
For example:
If the sample content of the dictionary is as follows:
Emp={"Ajay":76000, "Jyothi":150000, "David":89000, "Remya":65000, "Karthika":90000, "Vijay":82000}
The output from the program should be:
Vijay Remya Ajay
OR
Aruna has a list containing temperatures of 10 cities. You need to help her 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 negative temperatures into a stack.
● Pop and display the content of the stack.
For Example:
If the sample Content of the list is as follows:
T=[-9, 3, 31, -6, 12, 19, -2, 15, -5, 38]
Sample Output of the code should be:
-5-2-6-9
====
def push(d, stack):
for key in d:
if d[key]<85000:
stack.append(key)
def pop(stack):
if stack==[ ]:
print(“underflow”)
else:
print(stack.pop())
OR
def push(l, stack):
for ele in l:
if ele<0:
stack.append(ele)
def pop(stack):
if stack==[ ]:
print(“underflow”)
else:
print(stack.pop())
29 Write a function countbb() in Python, which reads the contents from a text file ‘article.txt’ and displays 3
number of occurrences of words ‘bat’ and ‘ball’ (in all possible cases) to the screen.
==
def countbb():
fin = open('article.txt', 'r')
data = fin.read()
fin.close()
L = data.split()
bat=ball=0
for i in L:
i = i.rstrip(".,;-'")
if i.upper() == 'BAT':
bat += 1
elif i.upper() == 'BALL':
ball += 1
print("Number of times bat occurs",bat)
print("Number of times ball occurs",ball)
30. Write a function SOnly() in Python, which reads the contents from a text file ‘news.txt’ and creates 3
another file ‘modnews.txt’. New file contains only those lines that starts with an alphabet.
===
def SOnly():
fin = open('news.txt','r')
L = fin.readlines()
fin.close()
for i in L:
if i[0].isalpha()==False:
L.remove(i)
fout = open('modnews.txt','w')
fout.writelines(L)
fout.close()
SECTION - D
31 A nested list contains the records of Mobiles in the following format: [[modelno, name, price], [modelno, 4
name, price], [modelno, name, price],….]
Write the following user defined functions to perform given operations on the stack named Mobile:
a. Push operation – To push details (modelno, name, price) of those mobiles which has price lower than
10000. Make a note that there cannot be more
than 20 elements in stack Mobile.
b. Pop operation – To pop elements from stack one by one and display them.
Also, display “Underflow” when stack becomes empty.
For example,
If the list contains
[[‘V20’, ‘Vivo 20 SE’, 18000], [‘S11’, ‘Lava S11’, 8900], [‘i88’, ‘IPro 88 SE’,
6500], [‘ip13’, ‘iPhone 13’, 125000]]
The stack should contain:
[‘i88’, ‘IPro 88 SE’, 6500]
[‘S11’, ‘Lava S11’, 8900]
The Output should be:
[‘i88’, ‘IPro 88 SE’, 6500]
[‘S11’, ‘Lava S11’, 8900]
Underflow
OR
A Dictionary Medal contains the details of schools and medals won by them in following format
{school_name:medals_won}.
Write a function Push(Medal) in Python that pushes those school names in stack named SCHOOL which
has won more than 3 medals. Maximum capacity of stack SCHOOL is 15. Function also shows number of
items pushed in stack. If number of items exceeds 15 then it shows OVERFLOW.
For example:
If dictionary Medal contains
{‘KV1’:5, ‘KV2’:2, ‘KV3’:4, ‘KV4’:1, ‘KV5’:7}
Then stack should contain
KV5
KV3
KV1
The output should be:
Number of item pushed in stack Medal are 3
====
def Push(L, mobile):
for i in L:
if i[2]<10000 and len(mobile)<20:
mobile.append(i)
elif len(mobile)>=20:
print("OVERFLOW")
break
def Pop(mobile):
while len(mobile)>0:
print(mobile.pop())
print("Underflow")
OR
def Push(Medal):
x=0
for sc in Medal:
if Medal[sc]>3 and len(SCHOOL)<15:
SCHOOL.append(sc)
x+=1
elif len(SCHOOL)>=15:
print("OVERFLOW")
break
print("Number of item pushed in stack Medal are",x)
32 A list contains following record of a student: 4
[SID, Name, Marks]
Write the following user defined functions to perform given operations on the
stack named STUD:
(i) Push_student(s) – To push an object containing SID and Name of
student whose marks are more than 75 to the stack.
(ii) Pop_student( ) – To Pop the objects from the stack and display them.
Also display ‘Stack empty’ when there is no element in the stack.
====
STUD= [ ]
def Push_Student(s):
if s[2] > 75:
L1 = [s[0],s[1]]
STUD.append(L1)
def Pop_student( ):
num=len(STUD)
while len(STUD) ! = 0:
d=STUD.pop()
print(d)
num=num-1
else:
print(“Stack empty”)
Section - E (Any 3 Questions-33-36)
33 What is the advantage of using a csv file for permanent storage? Write a program to 5
(a) add/insert records in file “data.csv”. Structure of a record is roll number, name and class.
(b) search and display record for a given class
===
(b) f=open(“data.csv”,”r”)
data=csv.reader()
cls=input(“class to search record”)
for row in data:
If row[2]==Cls:
print(row)
34. A binary file “emp.dat” has structure [EID, Ename, designation, salary].
I.Write a user defined function CreateEmp() to input data for a record and add to emp.dat.
II.Write a function display() in Python to display the detail of all employees.
===
import pickle
def CreateEmp():
f1=open("emp.dat",'wb')
eid=input("Enter E. Id")
ename=input("Enter Name")
designation=input("Enter Designation")
salary=int(input("Enter Salary"))
l=[eid,ename,designation,salary]
pickle.dump(l,f1)
f1.close()
II.
import pickle
def display():
f2=open("emp.dat","rb")
while True:
try:
rec=pickle.load(f2)
print(rec['eid'],rec['ename'],rec['designation'],rec['salary'])
except EOFError:
break
f2.close()
35. What is the difference between CSV file and Text File? Write a program in Python that defines and calls 5
the following functions:
Insert() – To accept details of clock from the user and stores it in a csv file ‘watch.csv’. Each record of
clock contains following fields – ClockID, ClockName, YearofManf, Price. Function takes details of all
clocks and stores them in file in one go.
Delete() – To accept a ClockID and removes the record with given ClockID from the file ‘watch.csv’. If
ClockID not found then it should show a relevant message. Before removing the record it should print
the record getting removed.
OR
Give one advantage of using CSV file over Binary file. Write a program in Python that defines and calls
the following functions:
saving() – To accepts details of equipments from the user in following format (ID, Name, Make, Price)
and save it in a csv file ‘parts.csv’. Function saves one record at a time. search() – To accept two prices
and displays details of those equipments which has
price between these two values.
===
Text files are plain text files which are used to store any kind of text which has no fixed format. whereas,
CSV files are also text files but they can store information in a specific format only.
def Insert():
L=[]
while True:
ClockID = input("Enter Clock ID = ")
ClockName = input("Enter Clock Name = ")
YearofManf = int(input("Enter Year of Manufacture = "))
price = float(input("Enter Price = "))
R = [ClockID, ClockName, YearofManf, price]
L.append(R)
ans = input("Do you want to enter more records (Y/N)=")
if ans.upper()=='N':
break
import csv
fout = open('watch.csv','a',newline='')
W = csv.writer(fout)
W.writerows(L)
fout.close()
print("Records successfully saved")
def Delete():
ClockID = input("Enter Clock ID to be removed = ")
found = False
import csv
fin = open('watch.csv','r')
R = csv.reader(fin)
L = list(R)
fin.close()
for i in L:
if i[0] == ClockID:
found=True
print("Record to be removed is:")
print(i)
L.remove(i)
break
if found==False:
print("Record not found")
else:
fout = open('watch.csv','w',newline='')
W = csv.writer(fout)
W.writerows(L)
fout.close()
print("Record Successfully Removed")
while True:
print("1. Add Clock Details")
print("2. Remove Clock Details")
print("3. Exit")
ch = int(input("Enter your choice(1-3)="))
if ch==1:
Insert()
elif ch==2:
Delete()
elif ch==3:
print("Thanks for using this program")
break
else:
print("Incorrect Choice. Please re-enter")
OR
CSV files stores records in text format that can be read and manipulated by other spreadsheet software
like excel. However, binary files stores records in raw format and can only be read and managed by
programs only.
def saving():
L=[]
ID = input("Enter ID = ")
name = input("Enter Name = ")
Make = input("Enter Make = ")
price = int(input("Enter Price = "))
R = [ID, name, Make, price]
L.append(R)
import csv
fout = open('parts.csv','a',newline='')
W = csv.writer(fout)
W.writerows(L)
fout.close()
print("Record successfully saved")
def search():
price1 = int(input("Enter Starting Price Range = "))
price2 = int(input("Enter Ending Price Range = "))
found = False
import csv
fin = open('watch.csv','r')
R = csv.reader(fin)
L = list(R)
fin.close()
for i in L:
if int(i[3])>=price1 and int(i[3])<=price2:
found=True
print(i)
if found==False:
print("Record not found")
while True:
print("1. Add Equipment Details")
print("2. Search Equipments based on Price Range")
print("3. Exit")
ch = int(input("Enter your choice(1-3)="))
if ch==1:
saving()
elif ch==2:
search()
elif ch==3:
print("Thanks for using this program")
break
else:
print("Incorrect Choice. Please re-enter")
36 Aaruni Shah is learning to work with Binary files in Python using a process known 5
as pickling/de-pickling. Her teacher has given her the following incomplete code
which is creating a binary file namely Mydata.dat and then opens, reads and
displays the content of this created file
import___________ # Fill_Line1
sqlist=list()
for k in range(10):
sqlist.append(k*k)
fout=________________ #Fill_Line 2
_______________________ #Fill_Line 3
fout.close()
fin=_____________________ #Fill_Line 4
________________________ #Fill_Line 5
fin.close()
(a) Complete Fill_Line1 so that the required library becomes available to the
program
(b) Complete Fill_Line 2 so the above mentioned binary file is opened for
writing in the file object fout
(c) Complete Fill_Line 3 so that list created in nthe code , namely Sqlist, is
written in the open file.
(d) Complete Fill_Line 4 which will open the same binary file for reading in the
file object fin.
(e) Complete Fill_Line 5 so that the contents of the open file in the file handle
fin are read in a list namely mylist.
=====
a) import pickle
b) fout=open(“Mydata.dat”,’wb’)
c) pickle.dump(sqlist,fout)
d) fin=open(‘Mydata.dat’,’rb’)
e) mylist=pickle.load(fin)