9 CS Questions
9 CS Questions
a#
Solution :
fh=open(r"myfile.txt","r")
item=[]
a=""
while True:
a=fh.readline()
words=a.split()
for j in words:
item.append(j)
if a =="":
break
print("#".join(item))
Solution :
def count_characters(file_path):
vowels = "aeiouAEIOU"
consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
uppercase_count = 0
lowercase_count = 0
vowel_count = 0
consonant_count = 0
try:
content = file.read()
if char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1
if char in vowels:
vowel_count += 1
consonant_count += 1
print(f"Vowels: {vowel_count}")
print(f"Consonants: {consonant_count}")
except FileNotFoundError:
except Exception as e:
QST -3 : Remove all the lines that contain`a' in a file and write it to
another file.
Solution :
fo=open("hp.txt","w")
fo.write("Harry Potter")
fo.close()
fo=open('hp.txt','r')
fi=open('writehp.txt','w')
l=fo.readlines()
for i in l:
if 'a' in i:
i=i.replace('a','')
fi.write(i)
fi.close()
fo.close()
QST – 4 : Create a binary file with name and roll number. Search for a given
roll number and display the name, if not found display appropriate message
Solution :
import pickle
def search_roll_number(roll_number):
while True:
try:
if number == roll_number:
return name
except EOFError:
break
return None
create_file('John Doe', 1)
create_file('Jane Doe', 2)
name = search_roll_number(1)
print(f'Name: {name}')
else:
QST – 5 : Create a binary file with roll number, name and marks. Input a roll
number and update the marks.
Solution :
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
self.items.append(item)
print(f"Pushed: {item}")
def pop(self):
if not self.is_empty():
popped_item = self.items.pop()
print(f"Popped: {popped_item}")
return popped_item
else:
def display(self):
if not self.is_empty():
print("Stack Contents:")
print(item)
else:
print("Stack is empty.")
# Example usage
stack = Stack()
stack.push(10)
stack.push(20)
stack.push(30)
stack.display()
stack.pop()
stack.pop()
stack.display()
QST – 6 : Write a random number generator that generates random numbers
between 1 and 6 (simulates a dice).
Solution :
import math.random
def generate_random_number():
return random.randint(1, 6)
print(generate_random_number())
class Stack:
def __init__(self):
self.stack = []
self.stack.append(item)
def pop(self):
if len(self.stack) == 0:
return None
else:
return self.stack.pop()
def peek(self):
if len(self.stack) == 0:
return None
else:
return self.stack[-1]
def is_empty(self):
return len(self.stack) == 0
QST- 8 : Create a CSV file by entering user-id and password, read and search
the password for given userid.
Solution :
import csv
def create_csv():
writer = csv.writer(file)
writer.writerow(["user_id", "password"])
while True:
if user_id.lower() == 'quit':
break
writer.writerow([user_id, password])
def search_password(user_id):
# Open the CSV file in read mode
reader = csv.reader(file)
if row[0] == user_id:
return row[1]
create_csv()
password = search_password(user_id)
else:
QST – 9 : Create a student table and insert data. Implement the following SQL
commands on
the student table:
o ALTER table to add new attributes / modify data type / drop attribute
o UPDATE table to modify data
o ORDER By to display data in ascending / descending order
o DELETE to remove tuple(s)
o GROUP BY and find the min, max, sum, count and average
Solution :
age INT,
marks INT
);
VALUES
UPDATE student
SET age = 21
WHERE student_id = 3;
-- GROUP BY and find the min, max, sum, count, and average
SELECT
MIN(age) AS min_age,
MAX(age) AS max_age,
SUM(age) AS total_age,
COUNT(*) AS total_students,
AVG(age) AS avg_age
FROM student;