DAV Centenary Public School, Haldwani, Uttarakhand
Computer Science(083) Practical File
Class: XII, Session: 2025-26
Note: Write these questions and their solutions in your Computer Science Practical File.
These questions are from previous year and sample question paper.
Python Programs
1 Write a function remove_element() in Python that accepts a list L and a number
n. Ifthe number n exists in the list, it should be removed. If it does not exist, print
a message saying "Element not found".
2 Write a Python function add_contact() that accepts a dictionary phone_book, a
name, and a phone number. The function should add the name and phone number
to the dictionary. If the name already exists, print "Contact already exists"
instead of updating it.
3 Write a Python function that displays the number of times the word "Python"
appears in a text file named "[Link]"
4 Write and call a Python function to read lines from a text file [Link] and
display those lines which doesn’t start with a vowel (A, E, I, O, U) irrespective of
their case.
5 A list containing records of products as
L = [("Laptop", 90000), ("Mobile", 30000), ("Pen", 50), ("Headphones", 1500)]
Write the following user-defined functions to perform operations on a stack
named Product to:
I. Push_element() – To push an item containing the product name and price of
products costing more than 50 into the stack.
Output: [('Laptop', 90000), ('Mobile', 30000), ('Headphones', 1500)]
II. Pop_element() – To pop the items from the stack and display them. Also,
display "Stack Empty" when there are no elements in the stack.
Output:
('Headphones', 1500)
('Mobile', 30000)
('Laptop', 90000)
Stack Emply
6 Raj is the manager of a medical store. To keep track of sales records, he has
created a CSV file named [Link], which stores the details of each sale.
The columns of the CSV file are: Product_ID, Product_Name, Quantity_Sold
and Price_Per_Unit.
Help him to efficiently maintain the data by creating the following user-defined
functions:
I. Accept() – to accept a sales record from the user and add it to the file [Link].
II. CalculateTotalSales() – to calculate and return the total sales based on the
7Quantity_Sold and Price_Per_Unit.
8 Mr. Ravi, a manager at a tech company, needs to maintain records of employees.
Each
record should include: Employee_ID, Employee_Name, Department and Salary.
Write the Python functions to:
I. Input employee data and append it to a binary file.
II. Update the salary of employees in the "IT" department to 200000.
9 Write a Python function that displays all the words containing @cmailfrom a text
file "[Link]".
10 Write a Python function that finds and displays all the words longer than
5 characters from a text file "[Link]".
11 You have a stack named BooksStack that contains records of books. Each book
record is represented as a list containing book_title, author_name, and
publication_year.
Write the following user-defined functions in Python to perform the
specified operations on the stack BooksStack:
(I) push_book(BooksStack, new_book): This function takes the stack
BooksStack and a new book record new_book as arguments and pushes the new
book record onto the stack.
(II) pop_book(BooksStack): This function pops the topmost book record from
the stack and returns it. If the stack is already empty, the function should display
"Underflow".
(III) peep(BookStack): This function displays the topmost element of the stack
without deleting it. If the stack is empty, the function should display 'None'.
For example:
If the integers input into the list `VALUES` are:
[10, 5, 8, 3, 12]
Then the stack `EvenNumbers` should store:
[10, 8, 12]
12 A csv file "[Link]" contains the data of a survey. Each record of the file
contains the following data:
● Name of a country
● Population of the country
● Sample Size (Number of persons who participated in the survey in that
country)
● Happy (Number of persons who accepted that they were Happy) For example,
a sample record of the file may be:
[‘Signiland’, 5673000, 5000, 3426]
Write the following Python functions to perform the specified operations on this
file:
(I) Read all the data from the file in the form of a list and display all those
records for which the population is more than 5000000.
(II) Count the number of records in the file.
13 Surya is a manager working in a recruitment agency. He needs to manage the
records of various candidates. For this, he wants the following information of
each candidate to be stored:
- Candidate_ID – integer
- Candidate_Name – string
- Designation – string
- Experience – float
You, as a programmer of the company, have been assigned to do this job for
Surya.
(I) Write a function to input the data of a candidate and append it in a binary file.
(II) Write a function to update the data of candidates whose experience is more
than 10 years and change their designation to "Senior Manager".
(III) Write a function to read the data from the binary file and display the data of
all those candidates who are not "Senior Manager".
14 Write a Python function that displays all the lines containing the word 'vote' from
a text file "[Link]". For example, if the file contains :
In an election many people vote to choose their representative.
The candidate getting the maximum share of votes stands elected.
Normally, one person has to vote once.
The process of voting may vary with time and region.
Then the output should be :
In an election many people vote to choose their
representative.
Normally, one person has to vote once.
15 Write a Python function that displays all the words starting and ending with
a vowel from a text file "[Link]". The consecutive words should be
separated by a space in the output. For example, if the file contains :
Once there was a wise man in a village.
He was an awesome story-teller.
He was able to keep people anchored while listening to him.
Then the output should be :
Once a a awesome able
16 A stack, named ClrStack, contains records of some colors. Each record is
represented as a tuple containing four elements – ColorName, RED,
GREEN, BLUE. ColorName is a string, and RED, GREEN, BLUE are
integers. For example, a record in the stack may be ('Yellow', 237,
250, 68)
Write the following user-defined functions in Python to perform the
specified operations on ClrStack:
(i) push_Clr(ClrStack, new_Clr): This function takes the stack
ClrStack and a new record new_Clr as arguments and pushes this
new record onto the stack.
(ii)pop_Clr(ClrStack): This function pops the topmost record from the stack and
returns it. If the stack is already empty, the function should display the message
"Underflow".
(iii)isEmpty(ClrStack): This function checks whether the stack is empty. If the
stack is empty, the function should return True, otherwise the function should
return False.
7 Write the following user-defined functions in Python :
(i) push_trail(N,myStack): Here N and myStack are lists, and myStack represents
a stack. The function should push the last 5 elements from the list N onto the
stack myStack. For example, if the list N is [1,2,3,4,5,6,7], then the function
push_trail() should push the elements 3,4,5,6,7 onto the stack. Therefore the
value of stack will be [3,4,5,6,7].
Assume that N contains at least 5 elements.
(ii) pop_one(myStack): The function should pop an element from the stack
myStack, and return this element. If the stack is empty, then the function should
display the message 'Stack Underflow', and return None.
(iii) display_all(myStack): The function should display all the elements of the
stack myStack, without deleting them. If the stack is empty, the function should
display the message 'Empty Stack'.
18 A csv file "P_record.csv" contains the records of patients in a hospital. Each
record of the file contains the following data :
● Name of a patient
● Disease
● Number of days patient is admitted
● Amount
For example, a sample record of the file may be :
["Gunjan","Jaundice",4,15000]
Write the following Python functions to perform the specified operations on
this file :
(i) Write a function read_data() which reads all the data from the file and
displays the details of all the 'Cancer' patients.
(ii) Write a function count_rec() which counts and returns the number of records
in the file.
19 A file, [Link], stores the records of passengers using the
following structure :
[PNR, PName, BRDSTN, DESTN, FARE]
where :
PNR – Passenger Number (string type)
PName – Passenger Name (string type)
BRDSTN – Boarding Station Name (string type)
DESTN – Destination Station Name (string type)
FARE – Fare amount for the journey (float type)
Write user defined functions in Python for the following tasks :
(i) Create() – to input data for passengers and write it in the binary file
[Link].
(ii) SearchDestn(D) –to read contents from the file [Link]
and display the details of those Passengers whose DESTN matches with the
value of D.
(iii) UpdateFare() – to increase the fare of all passengers by 5% and rewrite the
updated records into the file [Link].
20 Write a Python function that displays all the lines ending with a dot (.) from a
text file "[Link]". For example, if the file contains :
White is a mix of seven colors.
What are these seven colors ?
VIBGYOR – violet, indigo, blue, green, orange and red.
When we mix all these colors we just get one light which is the WHITE light.
Then the output should be :
White is a mix of seven colors.
VIBGYOR – violet, indigo, blue, green, orange and red. which is the WHITE
light.
(Hint : You will have to ignore trailing white spaces to check the last character)
21 Write a function in Python to display the line which has the
maximum number of vowels from a text file, "[Link]".
22 A stack named KeyStack contains records of some computer keyboards. Each
record is represented as a list containing Make, Keys, Connectivity. The Make
and Connectivity are strings, and Keys is an integer. For example, a record in the
stack may be ('Hitech', 105, 'USB').
Write the following user-defined functions in Python to perform the specified
operations on KeyStack :
(I) push_key(KeyStack, new_key): This function takes the stack KeyStack and a
new record new_key as arguments and pushes this new record onto the stack.
(II) pop_key(KeyStack): This function pops the topmost record from the stack
and returns it. If the stack is already empty, the function should display the
message “Underflow”.
(III) isEmpty(KeyStack): This function checks whether the stack is empty. If the
stack is empty, the function should return True, otherwise the function should
return False.
23 Write the following user-defined functions in Python :
(I) push_vowels(S,St): Here S is a string and St is a list representing a stack. The
function should push all the vowels of the string S onto the stack St. For
example, if the string S is "Easy Concepts", then the
function push_vowels() should push the elements
'E','a','o','e' onto the stack.
(II) pop_one(St) : The function should pop an element from the stack St, and
return this element. If the stack is empty, then the function should display the
message ‘Stack Underflow’, and return None.
(III) display_all(St): The function should display all the elements of the stack St,
without deleting them. If the stack is empty, the function should display the
message ‘Empty Stack’.
24 Suman is an intern at a software startup. The company has assigned her
a task to create a CSV file named [Link], to store the records of the
Club members. After discussing with Club Incharge, Suman has planned
to store the following content of members in the file [Link] :
[Mno, Name, Mobile, Fee]
Where
Mno – Member Number
Name – Name of the Member
Mobile – Member’s Mobile Number
Fee – Fee amount
Assuming you are asked to help Suman in her assignment, write a Python code
for performing the following tasks with the help of user-defined functions :
NewMembers() : to accept records of members from the user and add
them to the file [Link].
PriorityMember() : to find and display those members from the file [Link],
who are paying Fee more than 35000.
25 Keshav is the IT Head in a hospital. He needs to manage the records of all the
doctors in the hospital. For this, he wants to store the following information of
each doctor in a file :
D_ID – An integer to store Doctor ID.
D_Name – A string to store doctor’s name.
D_Dept – A string to store the Department of the doctor.
(Surgery, Radiology, etc.)
Experience – An integer to store doctor’s experience (in years)
For example, a doctor’s information may be :
[1256, 'R. Gupta', 'Cardiology', 15]
As an applicant for the post of a Programmer, you have to answer the
following questions in this context :
(I) Write one difference of storing this data in a binary file over a CSV
file.
(II) Assume that the data is stored in a binary file, named [Link], and
each record is stored as a list. Write a function, in Python, to read and display all
the records from the file [Link].
(III) Write a function addDoctor(), in Python, which accepts a doctor’s
data from the user and writes it in the file [Link]
SQL
1 Write suitable commands to do the following in MySQL.
I. View the table structure.
II. Create a database named SQP
2 Consider the table SALES as given below:
A. Write the following queries:
I. To display the total quantity sold for each product whose total quantity sold
exceeds 12.
II. To display the records of SALES table sorted by Product name in descending
order.
III. To display the distinct Product names from the SALES table.
IV. To display the records of customers whose names end with the letter 'e'.
B. Predict the output of the following:
I. SELECT * FROM Sales where product='Tablet';
II. SELECT sales_id, customer_name FROM Sales WHERE product LIKE 'S%';
III. SELECT COUNT(*) FROM Sales WHERE product in ('Laptop', 'Tablet');
IV. SELECT AVG(price) FROM Sales where product='Tablet';
3 Pranav is managing a Travel Database and needs to access certain information
from the
Hotels and Bookings tables for an upcoming tourism survey. Help him extract
the
required information by writing the appropriate SQL queries as per the tasks
mentioned
below:
I. To display a list of customer names who have bookings in any hotel of 'Delhi'
city.
II. To display the booking details for customers who have booked hotels in
'Mumbai', 'Chennai', or 'Kolkata'.
III. To delete all bookings where the check-in date is before 2024-12-03.
IV. A. To display the Cartesian Product of the two tables.
B. To display the customer’s name along with their booked hotel’s name.
4 A) Write an SQL command to remove the Primary Key constraint
from a table, named MOBILE. M_ID is the primary key of the
table.
B) Write an SQL command to make the column M_ID the Primary
Key of an already existing table, named MOBILE.
5 Consider the table ORDERS as given below
Note: The table contains many more records than shown here.
A) Write the following queries:
(I) To display the total Quantity for each Product, excluding Products with total
Quantity less than 5.
(II) To display the orders table sorted by total price in descending order.
(III) To display the distinct customer names from the Orders table.
(IV) Display the sum of Price of all the orders for which the quantity
is null.
B) Write the output
(I) Select c_name, sum(quantity) as total_quantity from orders group by c_name;
(II) Select * from orders where product like '%phone%';
(III) Select o_id, c_name, product, quantity, price from orders where price
between 1500 and 12000;
(IV) Select max(price) from orders;
6 Saman has been entrusted with the management of Law University Database. He
needs to access some information from FACULTY and COURSES tables for a
survey analysis. Help him extract the following information by writing the
desired SQL queries as mentioned below.
(I) To display complete details (from both the tables) of those Faculties whose
salary is less than 12000.
(II) To display the details of courses whose fees is in the range of 20000 to 50000
(both values included).
(III) To increase the fees of all courses by 500 which have "Computer" in their
Course names.
(IV) (A) To display names (FName and LName) of faculty taking System
Design.
(B) To display the Cartesian Product of these two tables.
7 Suman has created a table named WORKER with a set of records to maintain
the data of the construction sites, which consists of WID, WNAME, WAGE,
HOURS, TYPE, and SITEID. After creating the table, she entered data in it,
which is as follows :
(a) Based on the data given above, answer the following questions :
(i) Write the SQL statement to display the names and wages of those workers
whose wages are between 800 and 1500.
(ii) Write the SQL statement to display the record of workers whose SITEID is
not known.
(iii) Write the SQL statement to display WNAME, WAGE and HOURS of all
those workers whose TYPE is 'Skilled'.
(iv) Write the SQL statement to change the WAGE to 1200 of the workers where
the TYPE is "Semiskilled
(b) Considering the above given table WORKER, write the output on
execution of the following SQL commands:
(i) SELECT WNAME, WAGE*HOURS FROM WORKER WHERE SITEID =
103;
(ii) SELECT COUNT(DISTINCT TYPE) FROM WORKER ;
(iii) SELECT MAX(WAGE), MIN(WAGE), TYPE FROM WORKER
GROUP BY TYPE;
(iv) SELECT WNAME, SITEID FROM WORKER WHERE TYPE="Unskilled"
ORDER BY HOURS;
8 Assume that you are working in the IT Department of a Creative Art Gallery
(CAG), which sells different forms of art creations like Paintings, Sculptures etc.
The data of Art Creations and Artists are kept in tables Articles and Artists
respectively. Following are few records from these two tables :
Note :
● The tables contain many more records than shown here.
● DOC is Date of Creation of an Article.
As an employee of CAG, you are required to write the SQL queries for the
following :
(i) To display all the records from the Articles table in descending order of price.
(ii) To display the details of Articles which were created in the year 2020.
(iii) To display the structure of Artists table.
(iv) (a) To display the name of all Artists whose Article is Painting through Equi
Join.
(b) To display the name of all Artists whose Article is 'Painting' through Natural
Join.
9 Consider the table STAFF given below :
Table : STAFF
(a) Write the suitable SQL queries to perform the following tasks : 41=4
(I) To display the average salary of each department.
(II) To insert the following record in the table, STAFF.
STAFF_ID : S333
STAFF_NAME : GURMEET
SALARY : 15000
DEPARTMENT : ADMIN
DESIGNATION : CLERK
(III) To display the unique designations from the table.
(IV) To display all the details of the staff whose name is of four letters.
(b) Write the output for the queries given below :
(I) SELECT STAFF_NAME FROM STAFF
WHERE SALARY BETWEEN 25000 AND 30000;
(II) SELECT * FROM STAFF WHERE DEPARTMENT =
"MATHS" AND SALARY > 25000;
(III) SELECT STAFF_NAME, STAFF_ID FROM STAFF WHERE
DEPARTMENT LIKE "%S";
(IV) SELECT MAX(SALARY)FROM STAFF;
Assume that you are working for ABC Corporation (ABCC). ABCC allots
contracts to different contractors for some of its works. The data of Contracts and
Contractors are kept in the tables Work and Contractor respectively. Following
are a few records from these two tables of ABCC’s database.
Note : The tables contain many more records than shown here.
As an employee of ABCC, you are required to write the SQL queries for
the following : 41=4
(I) To display all the records from the Work table in alphabetical
order of W_Name.
(II) To display the names of contractors where W_Amt is more than
15000.
(III) To display the structure of Work table.
(IV) (a) To count total number of records present in Work table.
(b) To delete the records of contractors whose phone number is not known.
Database Connectivity with MySQL through Python
1 MySQL database named WarehouseDB has a product_inventory table in MySQL
which
contains the following attributes:
• Item_code: Item code (Integer)
• Product_name: Name of product (String)
• Quantity: Quantity of product (Integer)
• Cost: Cost of product (Integer)
Consider the following details to establish Python-MySQL connectivity:
• Username: admin_user
• Password: warehouse2024
• Host: localhost
Write a Python program to change the Quantity of the product to 91 whose
Item_code is
208 in the product_inventory table.
2 A table, named STATIONERY, in ITEMDB database, has the following
structure:
Write the following Python function to perform the specified operation:
AddAndDisplay(): To input details of an item and store it in the table
STATIONERY. The function should then retrieve and display all records from
the STATIONERY table where the Price is greater than 120.
Assume the following for Python-Database connectivity:
Host: localhost, User: root, Password: Pencil
3 A table, named THEATRE, in CINEMA database, has the following structure :
Write a function Delete_Theatre(), to input the value of Th_ID from the user and
permanently delete the corresponding record from the table. Assume the
following for Python-Database connectivity :
Host : localhost, User : root, Password : Ex2025
4 Nutan Kumar is using Python connectivity with MySQL for maintaining
data for a table named MEDICINES in a database PHARMACY. The table
has the following attributes :
• MId (Medicine number) – string
• Mname (Medicine Name) – string
• Expiry (Expiry Date) – Date
• Status (Active/Discard) – string
Consider the following to establish connectivity between Python and
MySQL :
• Username – root
• Password – tiger
• Host – localhost
Help Nutan to write the definition of a user-defined function named
ChangeStatus() in Python to change the Status of the Medicines whose
Expiry is before '2022-12-31' as 'DISCARD'.