Practice Questions For Practical
Practice Questions For Practical
Q – 1 a) Write a menu drive program to perform following operations into a binary file
shoes.dat. 8M
1. Add record
2. Display records
3. Search record
4. Exit
The structure of file content is: [s_id, name, brand, type, price]
Ans :
import pickle
while True:
print('''
1. Add Record
2. Display Record
3. Search Record
4. Exit
''')
ch=int(input("Enter your choice:"))
l=[]
if ch==1:
f=open("shoes.dat","ab")
s_id=int(input("Enter Shoes ID:"))
name=input("Enter shoes name:")
brand=input("Enter Brand:")
typ=input("Enter Type:")
price=float(input("Enter Price:"))
l=[s_id,name,brand,typ,price]
pickle.dump(l,f)
print("Record Added Successfully.")
f.close()
elif ch==2:
f=open("shoes.dat","rb")
while True:
try:
dt=pickle.load(f)
print(dt)
except EOFError:
break
f.close()
elif ch==3:
si=int(input("Enter shoes ID:"))
f=open("shoes.dat","rb")
flag=False
while True:
try:
dt=pickle.load(f)
for i in dt:
if i==si:
flag=True
print("Record Found...")
print("ID:",dt[0])
print("Name:",dt[1])
print("Brand:",dt[2])
print("Type:",dt[3])
print("Price:",dt[4])
except EOFError:
break
if flag==False:
print("Record not found...")
f.close()
elif ch==4:
break
else:
print("Invalid Choice")
b) Observe the following code and fill in the given blanks as directed: 4M
The partial code is given for inserting a record in customer table created in Rajwadi
Store. The customer table is given as following:
CustomerID CustomerName City BillAmt MobileNo
111 Abhishek Ahmedabad 1500 9999999999
i. Write the parameters and values required to fill statement 1. The parameters
values are as follows:
DatabaseServer User Pasword Database
main_rajwadi rajwadi_admin Rajwadi@2023 rajwadi
ii.Write function name to create cursor and fill in the gap for statement 2.
iii. Write a query to fill statement 3 with desired values.
iv. Write a query to fill statement 4 to save the records into table.
Ans. 1:
mycon.connect(host=”main_rajwadi”, username=”rajwadi_admin”, password=’Rajwadi@2023′,
database=’rajwadi’ )
Ans. 2: cursor
Ans. 3:
“insert into customer values ({},'{}’,'{}’,{},'{}'”.format(cust_id,cust_name,city,ba,mno)
OR
“insert into customer values(%d,’%s’,’%s’,%f,’%s’)”%cust_id,cust_name,city,ba,mno
Ans. 4:
commit()
Q 2) a) Write a menu drive program in python to implement stack using a list and perform
following functions: 8M
1. Push
2. Pop
3. Display
4. Exit
stk=[ ]
top=-1
def isEmpty():
if stk==[]:
print("Stack is Empty")
else:
None
def push():
global stk
global top
n=int(input("Enter value to append:"))
stk.append(n)
top=len(stk)-1
print("Value is Pushed into stack.")
def display():
global stk
global top
if top==-1:
isEmpty()
else:
top=len(stk)-1
print("Top->",stk[top])
for i in range(top-1,-1,-1):
print(stk[i])
def Pop():
global stk
global top
if top==-1:
isEmpty()
else:
print("Elment Popped:",stk.pop())
top=top-1
def menu():
while True:
print('''
1. Push
2. Pop
3. Display
4. Exit
''')
ch=int(input("Enter any number:"))
if ch==1:
push()
elif ch==2:
Pop()
elif ch==3:
display()
elif ch==4:
print("Bye Bye!")
break
else:
print("Invalid choice")
menu()
Q – 3 a) Write a menu drive program to perform following operations into telephone.csv.
1. Add record
2. Display records
3. Search record
4. Exit
The structure of file content is: [s_id, name, brand, type, price]
Ans:
Q – 4 a) WAP a menu-driven program in python to implement following on binary file of the
given structure:
[Doctor_Id, Doctor_Name, Hospital_Id, Joining_Date, Speciality, Salary]
2) The code given below reads the following record from the table named student and
displays only those records who have marks greater than 75:
RollNo – integer
Name – string
Clas – integer
Marks – integer
Ans :
1. mysql.connector as mysql
2. con1.cursor()
4. data = mycursor.fetchall()