Professional Documents
Culture Documents
CBSE CS PracticalExamQP ANS 2023-24
CBSE CS PracticalExamQP ANS 2023-24
1 Write a random number generator that generates random numbers between 1 and 6 8
(simulates a dice).
import random
print("Dice game ")
print("Game starts...")
ans='y'
while ans=='y':
print("Dice rolling ")
s=random.randint(1,6)
print("You got:",s)
ans=input("Do you want to roll again the dice (y/n):")
print("See you again.. Bye")
i) To display the gcode, type, Price of all garments whose qty is greater than 20.
Ans. select gcode, type, price from garment where qty>20;
ii) Add column named City to the table garment .
Ans. alter table garment add city varchar(20);
iii) Display the information in the descending order of gcode.
Ans. select * from garment order by gcode desc;
iv) Delete all the information whose gcode is 10023.
Ans. delete from garment where gcode=10023;
-2 SET
1 Read a text file line by line and display each word separated by a #. 8
a=open('sample.txt','r')
b=a.readline()
while b:
c=b.split()
for i in range(len(c)-1):
print(c[i],'#',end='')
print(c[-1])
print()
b=a.readline()
a.close()
a=open('sample.txt','r')
b=a.read()
v=c=uc=lc=0
for i in b:
if i.isalpha():
if i in 'aeiouAEIOU':
v+=1
else:
c+=1
if i.isupper():
uc+=1
elif i.islower():
lc+=1
print("The number of vowels in the file is :",v)
print("The number of consonants in the file is :",c)
print("The number of upper case letters in the file is :",uc)
print("The number of lower case letters in the file is :",lc)
a.close()
2 Write SQL queries for (i) to (iv) 4
Table: garment
gcode type price qty readydate
10023 Pencil Skirt 1150 25 2010-12-19
10001 Formal Skirt 1250 15 2010-01-12
10012 Baby Top 750 20 2009-04-09
10009 Shrug 1500 35 2012-12-20
10020 Frock 850 20 2012-01-01
10089 Slacks 750 10 2010-10-31
import pickle
def addrec():
a=open("student.bin",'wb')
print("Add student details")
l=[]
ans='y'
while ans=='y':
r=int(input("Enter the roll no:"))
n=input("Enter name :")
m=float(input("Enter mark:"))
l.append([r,n,m])
print(l)
ans=input("Do you want to add another record(y/n):")
pickle.dump(l,a)
print("Records stored successfully!.")
def searchrec():
a=open("student.bin",'rb')
r=int(input("Enter the roll no which you want to search:"))
b=pickle.load(a)
for i in b:
if r==i[0]:
print("Roll no:",i[0],"\nName:",i[1],"\nMark:",i[2])
break
else:
print("Entered Roll no not found in the file")
print("Student record search program")
addrec()
searchrec()
2 Write SQL queries for (i) to (iv) 4
Table: garment
gcode type price qty readydate
10023 Pencil Skirt 1150 25 2010-12-19
10001 Formal Skirt 1250 15 2010-01-12
10012 Baby Top 750 20 2009-04-09
10009 Shrug 1500 35 2012-12-20
10020 Frock 850 20 2012-01-01
10089 Slacks 750 10 2010-10-31
def updaterec():
a=open("student.bin",'rb+')
r=int(input("Enter the roll no which you want to update:"))
b=pickle.load(a)
print(b)
l=[]
for i in b:
l.append(i)
for i in l:
if r==i[0]:
nm=float(input("Enter new marks:"))
i[2]=nm
break
else:
print("Entered Roll no not found in the file")
a.seek(0)
pickle.dump(l,a)
print("Record updated successfully")
print(l)
a.close()
print("Student record update program")
addrec()
updaterec()
2 Write SQL queries for (i) to (iv) 4
Table: Garment
gcode type price qty readydate
10023 Pencil Skirt 1150 25 2023-12-19
10001 Formal Shirt 1250 15 2022-01-12
10012 Baby Top 750 20 2009-04-09
10009 Shrug 1500 35 2012-12-20
10020 Frock 850 20 2012-01-01
10089 Slacks 750 10 2010-10-31
i) To display the gcode, type, Price of all garments whose qty is greater than 20.
Ans. select gcode, type, price from garment where qty>20;
ii) Add column named City to the table garment .
Ans. alter table garment add city varchar(20);
iii) Display the information in the descending order of gcode.
Ans. select * from garment order by gcode desc;
iv) Delete all the information whose gcode is 10023.
Ans. delete from garment where gcode=10023;
SET -7
1 Create the csv file which should contains the employee details and to search 8
the particular employee based on empno and display the details.
import csv
def addrec():
with open('emp.csv','a',newline='') as a:
b=csv.writer(a)
l=[]
ans='y'
while ans=='y':
eno=int(input("Enter the emp no:"))
ename=input("Enter emp name :")
s=int(input("Enter emp salary:"))
l.append([eno,ename,s])
ans=input("Do you want to add another record(y/n):")
b.writerows(l)
print("record stored")
def searchrec():
l=[]
with open('emp.csv','r') as a:
b=csv.reader(a)
s=int(input('Enter the emp no which you want to search:'))
s=str(s)
for i in b:
l.append(i)
for i in l:
if i[0]==s:
print("Emp no:",i[0],'\nEmp name :',i[1],'\nEmp salary:',i[2])
break
else:
print('no record found')
def PUSH(stk):
a=input('Enter a city:')
stk.append(a)
def POP(stk):
if len(stk)==0:
print('STACK UNDERFLOW')
else:
c=stk.pop()
print('The popped city is:',c)
def DISPLAY(stk):
if len(stk)==0:
print('STACK EMPTY')
else:
print(stk[-1],'<--TOP')
for i in range(len(stk)-2,-1,-1):
print(stk[i])