26. Consider the following tables – LOAN and BORROWER:
Table: LOAN
| LOAN_NO |
B_NAME |
AMOUNT |
| L-170 |
DELHI |
3000 |
| L-230 |
KANPUR |
4000 |
Table: BORROWER
| CUST_NAME |
LOAN_NO |
| JOHN |
L-171 |
| KRISH |
L-230 |
| RAVYA |
L-170 |
(a) How many rows and columns will be there in the natural join of these two tables?
Ans: Natural join is when only common rows are displayed. Hence, 2 rows and 4 columns.
(b) Write the output of the queries i) to (iv) based on the table, WORKER given below:
Table: WORKER
| W_ID |
F_NAME |
L_NAME |
CITY |
STATE |
| 102 |
SAHIL |
KHAN |
KANPUR |
UTTAR PRADESH |
| 104 |
SAMEER |
PARIKH |
ROOP NAGAR |
PUNJAB |
| 105 |
MARY |
JONES |
DELHI |
DELHI |
| 106 |
MAHIR |
SHARMA |
SONIPAT |
HARYANA |
| 107 |
ATHARVA |
BHARDWAJ |
DELHI |
DELHI |
| 108 |
VEDA |
SHARMA |
KANPUR |
UTTAR PRADESH |
(i) SELECT F_NAME, CITY FROM WORKER ORDER BY STATE DESC;
Ans:
| F_NAME |
CITY |
| SAHIL | KANPUR |
| VEDA | KANPUR |
| SAMEER | ROOP NAGAR |
| MAHIR | SONIPAT |
| MARY | DELHI |
| ATHARVA | DELHI |
(ii) SELECT DISTINCT(CITY) FROM WORKER;
Ans:
| CITY |
| KANPUR |
| ROOP NAGAR |
| DELHI |
| SONIPAT |
(iii) SELECT F_NAME, STATE FROM WORKER WHERE L_NAME LIKE '_HA%';
Ans:
| F_NAME |
STATE |
| SAHIL | UTTAR PRADESH |
| MAHIR | HARYANA |
| VEDA | UTTAR PRADESH |
(iv) SELECT CITY, COUNT(*) FROM WORKER GROUP BY CITY;
Ans:
| CITY |
COUNT(*) |
| KANPUR | 2 |
| ROOP NAGAR | 1 |
| DELHI | 2 |
| SONIPAT | 1 |
27. (a) Write the definition of a Python function named LongLines ( ) which reads the contents of a text file named 'LINES. TXT' and displays those lines from the file which have at least 10 words in it. For example, if the content of 'LINES.TXT' is as follows:
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 the woods, whistling happily.
The girl was followed by a big gray wolf.
Then the function should display output as:
He lived in a little house in a beautiful, green wood.
He saw a little girl skipping through the woods, whistling happily.
Ans:
def LongLines():
try:
with open("LINES.TXT", 'r') as file:
lines = file.readlines()
for line in lines:
words = line.split()
if len(words) >= 10:
print(line.strip())
except FileNotFoundError:
print("File not found.")
# Call the function to display long lines
LongLines()
OR
(b) Write a function count_Dwords () in Python to count the words ending with a digit in a text file "Details.txt".
Example:
If the file content is as follows:
On seat2 VIP1 will sit and
On seat1 VVIP2 will be sitting
Output will be:
Number of words ending with a digit are 4
Ans:
import re
def count_Dwords(file_path):
try:
with open(file_path, 'r') as file:
text = file.read()
words = text.split()
count = 0
for word in words:
if re.search(r'\d$', word):
count += 1
return count
except FileNotFoundError:
return -1 # Return -1 if the file is not found
# Provide the path to the text file
file_path = "Details.txt"
# Call the function to count words ending with a digit
result = count_Dwords(file_path)
if result == -1:
print("File not found.")
else:
print("Number of words ending with a digit are", result)
28. (a) Write the outputs of the SQL queries (i) to (iv) based on the relations COMPUTER and SALES given below:
Table: COMPUTER
| PROD_ID |
PROD_NAME |
PRICE |
COMPANY |
TYPE |
| P001 |
MOUSE |
200 |
LOGITECH |
INPUT |
| P002 |
LASER PRINTER |
4000 |
CANON |
OUTPUT |
| P003 |
KEYBOARD |
500 |
LOGITECH |
INPUT |
| P004 |
JOYSTICK |
1000 |
IBALL |
INPUT |
| P005 |
SPEAKER |
1200 |
CREATIVE |
OUTPUT |
| P006 |
DESKJET PRINTER |
4300 |
CANON |
OUTPUT |
Table: SALES
| PROD_ID |
QTY_SOLD |
QUARTER |
| P002 |
4 |
1 |
| P003 |
2 |
2 |
| P001 |
3 |
2 |
| P004 |
2 |
1 |
(i) SELECT MIN(PRICE), MAX(PRICE) FROM COMPUTER;
Ans:
| MIN(PRICE) |
MAX(PRICE) |
| 200 |
4300 |
(ii) SELECT COMPANY, COUNT(*) FROM COMPUTER GROUP BY COMPANY HAVING COUNT(COMPANY) > 1;
Ans:
| COMPANY |
COUNT(*) |
| LOGITECH |
2 |
| CANON |
2 |
(iii) SELECT PROD_NAME, QTY_SOLD FROM COMPUTER C, SALES S WHERE C.PROD_ID = S.PROD_ID AND TYPE = 'INPUT';
Ans:
| PROD_NAME |
QTY_SOLD |
| KEYBOARD |
2 |
| MOUSE |
3 |
| JOYSTICK |
2 |
(iv) SELECT PROD_NAME, COMPANY, QUARTER FROM COMPUTER C, SALES S WHERE C.PROD_ID = S.PROD_ID;
Ans:
| PROD_NAME |
COMPANY |
QUARTER |
| LASER PRINTER |
CANON |
1 |
| KEYBOARD |
LOGITECH |
2 |
| MOUSE |
LOGITECH |
2 |
| JOYSTICK |
IBALL |
1 |
(b) Write the command to view all databases.
29. Write a function EOReplace() in Python, which accepts a list L of numbers. Thereafter, it increments all even numbers by 1 and decrements all odd numbers by 1.
Example:
If Sample Input data of the list is :
L= [10,20,30,40,35,55]
Output will be:
L=[11, 21,31, 41, 34, 54]
Ans:
def EOReplace(L):
for i in range(len(L)):
if L[i] % 2 == 0: # Check if the number is even
L[i] += 1 # Increment even numbers by 1
else:
L[i] -= 1 # Decrement odd numbers by 1
# Sample Input data
L = [10, 20, 30, 40, 35, 55]
# Call the function
EOReplace(L)
# Print the modified list
print("Modified List:", L)
30. (a) A list contains following record of customer:
[Customer_name, Room Type]
Write the following user defined functions to perform given operations on the stack named 'Hotel' :
(i) Push_Cust () – To Push customers’ names of those customers who are staying in Delux Room Type.
(ii) Pop_Cust () – To Pop the names of customers from the stack and display them. Also, display “Underflow” when there are no customers in the stack.
For example:
If the lists with customer details are as follows:
["Siddarth"; "Delux"]
[ "Rahul", "Standard"]
["Jerry", "Delux"]
The stack should contain
Jerry
Siddharth
The output should be:
Jerry
Siddharth
Underflow
Ans:
class HotelStack:
def __init__(self):
self.Hotel = [] # Initialize an empty stack
def Push_Cust(self, customer_name, room_type):
if room_type.lower() == "delux":
self.Hotel.append(customer_name)
def Pop_Cust(self):
if len(self.Hotel) == 0:
print("Underflow")
else:
customer = self.Hotel.pop()
print(customer)
# Create a HotelStack object
hotel_stack = HotelStack()
# Customer records
customer_records = [
["Siddharth", "Delux"],
["Rahul", "Standard"],
["Jerry", "Delux"]
]
# Push customers staying in Delux Room Type
for record in customer_records:
customer_name, room_type = record
hotel_stack.Push_Cust(customer_name, room_type)
# Pop and display customer names
while len(hotel_stack.Hotel) > 0:
hotel_stack.Pop_Cust()
OR
(b) Write a function in Python, Push (Vehicle) where, Vehicle is a dictionary containing details of vehicles – {Car Name: Maker}.
The function should push the name of car manufactured by TATA’ (including all the possible cases like Tata, TaTa, etc.) to the stack.
For example:
If the dictionary contains the following data:
Vehicle={"Santro": "Hyundai", "Nexon":"TATA", "Safari": "Tata")
The stack should contain
Safari
Nexon
Ans:
def Push(Vehicle):
stack = [] # Create an empty stack to store car names
# Iterate through the dictionary
for car, maker in Vehicle.items():
if maker.lower() == "tata":
stack.append(car) # Push the car name to the stack (ignoring case)
return stack
# Sample input dictionary
Vehicle = {"Santro": "Hyundai", "Nexon": "TATA", "Safari": "Tata"}
# Call the function
result_stack = Push(Vehicle)
# Print the stack
for car in result_stack:
print(car)