CS Practical file Class 12th.docx
CS Practical file Class 12th.docx
Computer Science
PRACTICAL
FILE
1.User-Defined Functions
2.Text File
3.Binary File
4.CSV File
5.Stacks
6.SQL Assignment 1
7.SQL Assignment 2
8.SQL Assignment 3
9.SQL Assignment 4
10.SQL Connectivity
2
Topic I
User-Defined Functions
ANS.
3
2. Write a program using a user defined function myMean() to calculate the
mean of floating values stored in a list.
ANS.
3. Write a program using a user defined function that accepts an integer and
increments the value by 5. Also display the id of argument (before function
call), id of parameter before increment and after increment.
ANS.
4
4. Write a program using a user defined function that displays the sum of
first n natural numbers, where n is passed as an argument.
ANS.
5
5. The company performs the following tasks to fix the selling price of each
tent.
1. Accept user requirements for the tent, such as
a) height
b) radius
c) slant height of the conical part
2. Calculate the area of the canvas used
3. Calculate the cost of the canvas used for making the tent
4. Calculate the net payable amount by the customer that is inclusive of the
18% tax. Write separate user defined functions for part 2,3 and 4.
ANS.
6
6. Write a program that simulates a traffic light. The program should consist
of the following:
1. A user defined function trafficLight( ) that accepts input from the user,
displays an error message if the user enters anything other than RED,
YELLOW, and GREEN.
Function light() is called and following is displayed depending upon return
value from light().
a) “STOP, your life is precious” if the value returned by light() is 0.
b) “Please WAIT, till the light is Green“ if the value returned by light() is 1
c) “GO! Thank you for being patient” if the value returned by light() is 2.
2. A user defined function light() that accepts a string as input and returns 0
when the input is RED, 1 when the input is YELLOW and 2 when the input is
GREEN. The input should be passed as an argument.
3. Display “SPEED THRILLS BUT KILLS” after the function trafficLight( ) is
executed.
ANS.
7
7. Create a user defined module basic_math that contains the following user
defined functions:
1. To add two numbers and return their sum.
2. To subtract two numbers and return their difference.
3. To multiply two numbers and return their product.
4. To divide two numbers and return their quotient and print “Division by
Zero” error if the denominator is zero.
Also add a docstring to describe the module. After creating the module,
import and execute functions at shell prompt.
ANS.
8
9
Topic II
Text File
Note - All Func calls are shown together in the last image
- Same Story.txt has been used for all Ques
(Additions have been made for Q12)
ANS.
10
9. Write a function in Python to read a text file line by line and display each
word separated by #.
ANS.
11
10. Write a function in Python to read a text file and display the number of
vowels/ consonants/ uppercase / lowercase characters in a file.
ANS.
12
11. Write a program in python to Reverse and display each line of text.
ANS.
12. Write a program in python to display lines of text ending with ‘Y’ or ‘y’.
ANS.
13
13. Write a function in Python to read a text file and convert the uppercase
letter into lowercase and vice versa.
ANS.
14
All Func Calls-
Topic III
Binary File
Note - All Func calls are shown together in the last image
- All Ques are done in the same file therefore pickle
is shown to be imported once (i.e. Q14)
- All Passed arguments are shown along with the Func
calls
14. Write a function in PYTHON to search for a BookNo from a binary file
“BOOK.DAT”, after creating the binary containing the records of the
following type: {BookNo":value, "Book_name":value} The
BookNo is an integer.
ANS.
15
15. Assuming that a binary file VINTAGE.DAT contains records of the
following type, {VNO, VDesc, price} write a function in PYTHON to read the
data VINTAGE.DAT and display those vintage vehicles, which are priced
between 200000 and 250000.
ANS.
16
16. Write a function in Python to search for a laptop from a binary file
“laptop.dat” containing records of following type [ ModelNo, RAM, HDD,
Details] Where ModelNo, RAM, HDD are integers and Details is a string. The
user should enter the model number and the function should display the
details of the laptop.
ANS.
17
17. Write a function in Python to search for the details of those mobile
phones which have more than 1000 calls from a binary file “mobile.dat”.
Assume that binary file contains records of the following type: (Number,
Calls)
ANS.
18
18. Write a function in Python to read each record of a binary file
“items.dat”, find and display those items which cost less than 2500.
Assume that the file is created with the help of objects of the following type:
{ ID:string, GIFT:string, Cost:integer}
ANS.
19
19. Write a function bumper() in Python to read each object of a binary file
“gifts.dat”, find and display details of those gifts, which have remarks as “on
discount”. Assume the file is created with the help of lists of following type:
[ID, Gift, Remarks, Price]
ANS.
20
All Func calls-
21
Topic IV
CSV File
Note - All Func calls are shown together in the last image
- All Ques are done in the same file therefore csv is
shown to be imported once (i.e. Q20)
ANS.
22
23
24
25
26
21. A CSV file TEACHER.CSV has following data:
CODE,NAME,SUBJECT,DESIG,NOP
1001,ADITI JAIN,PHYS,HOD,20
Write an interactive menu driven to do the following (using CSV module):
● Read the CSV file and display details of teachers whose DESIG is either
TGT or PGT and SUBJECT is ENG; if no record is found then display a
message 'Record not found in the file'
● Read the CSV file and display details if DESIG is either PRT or TGT; at the
end display number of such records found in the file
● Exit from the menu and quit the program
ANS.
27
28
29
ALL Func Calls-
30
Topic V
Stacks
22. Write a function in Python that reverses a string using Stack data structures.
ANS.
ANS.
31
24. Monika has created a dictionary containing names and marks as
key-value pairs of 5 students.
Write a program, with separate user-defined functions to perform the
following operations:
1. Push the keys (name of the student) of the dictionary into a stack, where
the corresponding value
(marks) is greater than 70.
2. Pop the content of the stack
3. Display the content of the stack.
32
ANS.
33
Topic VI
SQL Assignment - 1
Q-1 Write SQL Command for (a) to (o) and output of (p)
TABLE : GRADUATE
S.N NAME STIPEND SUBJECT AVERAGE DIVISI
O ON
1 KARAN 400 PHYSICS 68 I
2 DIWAKAR 450 COMP Sc 68 I
3 DIVYA 300 CHEMISTRY 62 I
4 REKHA 350 PHYSICS 63 I
5 ARJUN 500 MATHS 70 I
6 SABINA 400 CHEMISTRY 55 II
7 JOHN 250 PHYSICS 64 I
8 ROBERT 450 MATHS 68 I
9 RUBINA 500 COMP Sc 62 I
10 VIKAS 400 MATHS 57 II
34
a. Create Database DB1 and Create Table Graduate in DB1 database.
35
b. Insert 10 rows in Graduate table.
36
d. Display a report, listing NAME, STIPEND, SUBJECT and amount of
stipend received in a year assuming that the STIPEND is paid every month.
37
f. To insert a new 11th row in the GRADUATE table:(11,”KAJOL”, 300, “Comp
Sc”, 75, “I”).
38
h. Display Name and division only in the Graduate table.
39
j. To display students details sorted by NAME in descending order.
40
m. Delete KAJOL’s details from the table.
41
o. Give the output of following sql statement based on table GRADUATE:-
42
iv.)Select distinct SUBJECT from GRADUATE;
Table: Loan_Accounts
1 19-07-
R. K. Gupta 300000 36 12.00
2009
2 22-03-
S. P. Sharma 500000 48 10.00
2008
3 08-03-
K. P. Jain 300000 36 NULL
2007
43
4 06-12-
M. P. Yadav 800000 60 10.00
2008
5 03-01-
S. P. Sinha 200000 36 12.50
2010
6 05-06-
P. Sharma 700000 60 12.50
2008
7 05-03-
K. S. Dhall 500000 48 NULL
2008
44
2. Use the database LOANS.
45
C.) Simple Select
6. Display the details of all the loans with less than 40 installments.
46
7. Display the AccNo and Loan_Amount of all the loans started before
01-04-2009.
E) Using NULL-
9. Display the details of all the loans whose rate of interest is NULL.
47
10. Display the details of all the loans whose rate of interest is not NULL.
11. Display the amounts of various loans from the table Loan_Accounts. A loan
amount should appear only once.
48
12. Display the number of installments of various loans from the table
Loan_Accounts. An installment should appear only once.
13. Display the details of all the loans started after 31-12-2008 for which the
number of installments are more than 36.
14. Display the Cust_Name and Loan_Amount for all the loans which do not have
36 installments.
15. Display the Cust_Name and Loan_Amount for all the loans for which the loan
amount is less than 500000 or int_rate is more than 12.
49
16. Display the details of all the loans which started in the year 2009.
17. Display the details of all the loans whose Loan_Amount is in the range 400000
to500000.
18. Display the details of all the loans whose rate of interest is in the range 11%
to 12%.
50
H.) Using IN Operator
19. Display the Cust_Name and Loan_Amount for all the loans for which the
number of installments are 24, 36, or 48. (Using IN operator)
20. Display the details of all the loans whose Loan_Amount is in the range
400000 to 500000. (Using BETWEEN operator)
51
21. Display the details of all the loans whose rate of interest is in the range 11%
to 12%.(Using BETWEEN operator)
22. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which
theCust_Name ends with ‘Sharma’.
23. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which
the Cust_Name ends with ‘a’.
24. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which
the Cust_Name contains ‘a’.
52
25. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which
the Cust_Name does not contain ‘P’.
26. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which
the Cust_Name contains ‘a’ as the second last character.
27. Display the details of all the loans in the ascending order of their
Loan_Amount.
53
28. Display the details of all the loans in the descending order of their
Start_Date.
29. Display the details of all the loans in the ascending order of their
Loan_Amount and within Loan_Amount in the descending order of their
Start_Date.
30. Put the interest rate 11.50% for all the loans for which interest rate is NULL.
54
31. Increase the interest rate by 0.5% for all the loans for which the loan amount
is more than 400000.
55
32. For each loan compute Interest as
(Loan_Amount*(Int_Rate/100)*Installments).
33. Delete the records of all the loans whose start date is before 2007.
56
34. Delete the records of all the loans of ‘K.P. Jain’;
35. Add another column Category of type CHAR(1) in the Loan table.
57
Practical Assignment -3 (SQL Commands)
Table:Worker
EMPNO ENAME JOB HIREDATE SAL COMM DEPTNO
58
8654 BINA ANALYST 1992-12-09 30000 NULL 20
2. Create the table WORKER and add records as shown below. Write SQL
commands for the statements (a) to (g).
59
c. To display the details in ascending order of empno.
60
g. To display the sum of salary, average of salary, maximum and minimum salary of
each job title.
3. Create a table EMPLOYEE with the following data in the Work database.
Write SQL commands for the statements (a) to (h).
Table: EMPLOYEE
No Name Salary Area Age Grade Dept
61
262 KIRTI 35000 SOUTH 38 A ELEC
62
c. To display the grade and number of employees in each grade.
63
e. To display the sum and average salary of the employees of grade ‘B’
.
f. To display grade and maximum, minimum and average salary of each grade.
h. To display the grade and the number of employees in each grade where the
number of employees are more than two.
64
Practical Assignment -4 (SQL
Commands)
4. Create Database Cust and create two tables HANDSETS and CUSTOMER in it
as given below:
Table:- Handsets
SetCode SetName TouchScreen Phone Cost
N1 Nokia 2G N 5000
N2 Nokia 3G Y 8000
B1 BlackBerry 2G N 14000
Table:- Customer
CustNo SetNo CustAddress
1 N2 Delhi
2 B1 Mumbai
3 N2 Mumbai
4 N1 Kolkata
5 B1 Delhi
65
With reference to these tables, Write commands in SQL for (a) and (b) and
output for (c) below:
b. Display the Customer Details for each customer who uses a Nokia handset.
c. Select SetNo, SetName from Handsets, customer where SetNo = SetCode and
CustAddress = ‘Delhi’;
66
5. Create the following tables SENDER and RECIPIENT in Cust database. Write
SQL commands for the statements (i) to (iv) and give outputs for SQL queries (v)
to (viii)
Table: SENDER
SenderID SenderName SenderAddress SenderCity
Table: RECIPIENT
RecID SenderID RecName RecAddress RecCity
67
a. Display the names of all senders from MUMBAI.
68
d. Display the number of recipients in each city.
GIVE OUTPUTS:-
69
g. SELECT RecName, RecAddress FROM Recipient Where RecCity NOT IN
(‘Mumbai’,’Kolkata’);
70
Practical Assignment -5
(SQL Commands)
Q6(I).Create the following tables PRODUCT and CLIENT. Write SQL commands
for the statements (a) to (d) and give outputs for SQL queries (e) to (h)
Table: PRODUCT
P_ID ProductName Manufacturer Price
71
Table:- CLIENT
C_ID ClientName City P_ID
j. To display the details of the products whose price is in the range 50 to 100
(Both values included).
72
k. To display the ClientName, City from table Client, and ProductName and Price
from table Product, with their corresponding matching P_ID.
73
n. SELECT Manufacturer , MAX(price) , MIN(Price), COUNT(*)FROM Product
GROUP BY Manufacturer;
74
6. (II) Create the following tables WORKER and PAYLEVEL. Write SQL
commands for the statements (a) to (d) and give outputs for SQL queries (e) to
(h)
Table:- WORKER
ECODE NAME DESIG PLEVEL DOJ DOB
Table: PAYLEVEL
PLEVEL PAY ALLOWANCE
75
a. To display the details of all WORKERS in descending order of DOB.
b. To display NAME and DESIG of those WORKERS, whose PLEVEL is either P001
or P002.
c. To display the content of all the WORKERs, whose DOB is in between ’19-JAN-
1984’ and ’18-JAN-1987’.
76
d. To add a new row with the following:
19, ‘Daya Kishore’, ‘Operator’, ‘P003’, ’19-Jun-2008’, ’11-Jul-1984’
77
g. SELECT Name, Pay FROM WORKER W, PAYLEVEL P WHERE W.PLEVEL = P.
PLEVEL AND W.ECODE < 13;
78
79
80
a. Input Rollno, Name, Total marks and Grade and insert the record in a
table Student.
81
b. Input Rollno and update the corresponding record in a table Student with
the value entered by the user.
82
a.Write Python code to increase the price of all products by 50.
83
a. Input Rollno and display the corresponding information from the table
Student.
b. Input Rollno and delete the corresponding record from the table Student.
84
c. Display all the records of a grade entered by the user from the table
Student.
d. Display all the records in descending order of total marks from the table
Student.
85