0% found this document useful (0 votes)
17 views11 pages

Practice Questions

The document contains various programming tasks and SQL queries related to text file processing, database operations, and stack implementation. It includes Python code for counting words, connecting to a MySQL database, and performing CRUD operations on tables. Additionally, it outlines functions for managing book records and teacher details in a structured format.

Uploaded by

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

Practice Questions

The document contains various programming tasks and SQL queries related to text file processing, database operations, and stack implementation. It includes Python code for counting words, connecting to a MySQL database, and performing CRUD operations on tables. Additionally, it outlines functions for managing book records and teacher details in a structured format.

Uploaded by

shravan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Q1.

Counting the number of lines with at least 10 words and counting


the number of occurrences of word ‘was’ from a text file Lines.txt
The file contains following data:
Once upon a time, there was a woodcutter.
He lived in a little house in a beautiful, green wood.
One day, he was merrily chopping some wood.
He saw a little girl skipping through woods, whistling happily.
The girl was followed by a big grey wolf.

CODE:
def Longlines():
f=open("Lines.txt","r")
print("No. of lines with atleast 10 words are:")
for line in f:
words=line.split()
if len(words)>=10:

print(line)
f.close()

def CountWas():
count=0
f=open("Lines.txt","r")
for line in f:
words=line.split()
for word in words:
if word=="was":
count+=1
print("No. of occourances of word 'was' is:",count)
f.close()

Longlines()
CountWas()

Q2. Create the given table Tech_course in MYSQL and insert the following records in it.
Write the Python - MYSQL connectivity code to display all course details whose fees is more
than 10000. [5M]
C_ID C_Name Fees Startdate Term_ID
C201 MBA 90000 2024-07-02 101
C202 MTech 30000 2023-11-15 105
C203 Data Science 65000 2022-10-01 102
C204 Cyber Security 9000 2021-09-15 104
C205 Cloud Computing 18000 2022-11-01 101
C206 Machine Learning 16000 2024-07-25 103
Note the following to establish connectivity between Python and MYSQL.

 Username: root
 Host name: localhost
 Password: tiger
 Database name: Courses
a. Consider the table Tech_course and solve the following queries. [4M]
(i) Display minimum and maximum fees of the course offered.
(ii) Display all courses details whose name starts with ‘D’.
(iii) Display course name in order of startedate.
(iv) Count and Display no. of courses offered Term_ID wise.

CODE:
import mysql.connector as mycon

mydb=mycon.connect(host="localhost",user="root",password="",datab
ase="practice")
cursor=mydb.cursor()
querry="SELECT * FROM tech_course WHERE Fees>10000;"
cursor.execute(querry)
data=cursor.fetchall()
for i in range(len(data)):
print(data[i])

print("\n")

q1="SELECT MIN(Fees) AS 'Minimum fees',MAX(Fees) AS 'Maximum


fees' FROM tech_course;"
cursor.execute(q1)
d1=cursor.fetchall()
print(d1)

print("\n")
q2="SELECT * FROM tech_course where CName LIKE 'D%';"
cursor.execute(q2)
d2=cursor.fetchall()
for j in range(len(d2)):
print(d2[j])

print("\n")

q3="SELECT CName FROM tech_course ORDER BY Startdate;"


cursor.execute(q3)
d3=cursor.fetchall()
for k in range(len(d3)):
print(d3[k])

print("\n")

q4="SELECT TID,COUNT(*) FROM tech_course GROUP BY TID;"


cursor.execute(q4)
d4=cursor.fetchall()
print(d4)
Q3.Function to count the number of vowels and consonants in the
given text file test.txt
The file contains following data:
An apple a day keeps the doctor away.
We all pray for everyone's safety.
A marked difference will come in our country.

CODE:
def CountVowels():
vowel="aeiouAEIOU"
vcount=ccount=0
f=open("test.txt","r")
for line in f:
for char in line:
if char in vowel:
vcount+=1
elif char not in vowel:
ccount+=1
print("No. of vowels in file:",vcount)
print("No. of consonent in file:",ccount)

CountVowels()
Q4.Stack operations for list [custname,phoneno,city]
CODE:
def isEmpty(stk):
if stk==[]:
return True
else:
return False

def Push(stk,a):
stk.append(a)

def Pop(stk):
if isEmpty(stk):
return "Underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def Display(stk):
if isEmpty(stk):
return "Underflow"
else:
top=len(stk)-1
print(stk[top],"<---top")
for i in range(top-1,-1,-1):
print(stk[i])

def Peek(stk):
if isEmpty(stk):
print("Underflow")
else:
top=len(stk)-1
return stk[top]

#__main__

Stack=[]
top=None

while True:
print("STACK OPERATIONS")
print("1.Push")
print("2.Pop")
print("3.Display")
print("4.Peek")

ch=int(input("Enter your choice(1-5): "))


if ch==1:
custname=input("Enter customer name: ")
phoneno=int(input("Enter phone number: "))
city=input("Enter city: ")
lst=[custname,phoneno,city]
Push(Stack,lst)
elif ch==2:
Pop(Stack)
if lst=="Underflow":
print("Underflow! Stack is empty")
else:
print("Popped item is:",lst)
elif ch==3:
Display(Stack)
elif ch==4:
item=Peek(Stack)
if item=="Underflow":
print("Underflow! Stack is empty")
else:
print("Topmost item is:",item)
elif ch==5:
break
else:
print("Invalid choice")
Q.5 Define the following functions:
1. add_book(): Details of books and adds them to a csv file “Books.csv” . Each record
consists of a list with field elements as book_id, book_name and publisher to store
book Id, book name and publisher name respectively.
2. search_book(): Takes publisher name as input , counts and display no of books
published by them.

Answer is given in answer key.

Q.6 Consider the table Teacher and solve the following queries

T_ID Name Age Department DOJ Salary Gender


1 Arun 34 Computer Science 2019-01-10 27000 M
2 Suman 31 History 2024-03-24 13000 F
3 Deepa 32 Maths 2020-12-12 26000 F
4 Samir 35 History 2022-07-01 19000 M
5 Raman 42 Computer Science 2021-09-05 28000 M
6 Shyam 50 History 2022-06-27 20000 M
7 Shivani 44 Computer Science 2018-02-25 31000 F
8 Shalakha 33 Maths 2017-07-31 40000 F

a. Display all the female teacher details whose salary is more than 15000.
b. Display no. of teachers department wise.
c. Increase the salary by 5% of teachers from ‘Computer Science’ department.
d. Display all the teacher details in ascending order of DOJ.

Answer is given in answer key.


Q.7 Consider the following table Employee. Write the Python-Mysql connectivity code to
perform following queries.
[4M]

Table: Employee
Empid Name DOB DeptID Designation Salary
120 Alisha 23-01-1988 D001 Manager 75000
123 Nitin 10-10-1977 D002 Assistant Manager 59000
129 Navjot 12-07-1991 D003 Supervisor 40000
130 Jimmy 30-12-1990 D004 Sales Representative 34000
131 Faiza 06-04-1987 D001 Department Manager 65000

Note the following to establish connectivity between Python and MYSQL.

 Username: root
 Host name: localhost
 Password: tiger
 Database name: Company

i. To display the average salary of all employees


ii. To display the details of all employees in alphabetical order of name.
iii. To display DeptId from the table Employee without repetition.

Answer is given in answer key.

Q.8 Create the following table Cabhub given below


in MYSQL:
Vcode Vehiclename Make Colour Capacity charges
100 Innova Toyota White 7 15
102 Sx4 Suzuki Blue 4 14
104 C class Mercedes Red 4 35
105 A star Suzuki White 3 14
108 Indigo Tata Silver 3 12

Write the SQL queries given below and show the result to examiner.
(i) To display the names of all the white coloured vehicles.
(ii) To display name of vehicle and capacity of vehicles in ascending order of their sitting
capacity.
(iii) Update the charges of Mercedes car as 50 .
(iv) Add a column Pnum in the table Cabhub with datatype as integer.

Answer is given in answer key.

You might also like