Pre 1 Cs Ans
Pre 1 Cs Ans
python
CopyEdit
List1 = list("Examination")
List2 = List1[1:-1]
new_list = []
for i in List2:
j = List2.index(i)
if j % 2 == 0:
List1.remove(i)
print(List1)
Answer:
['E', 'x', 'm', 'i', 'n', 'a', 't', 'i', 'n']
Explanation:
List1.remove(i) modifies List1, removing elements from it based on their positions in
List2. This removal happens only for indices in List2 where j % 2 == 0.
myexam = "@@PREBOARDEXAMINATION2025@@"
print(myexam[::-2])
Answer:
'@502NAIETRAEP@'
Explanation: The slice [::-2] traverses the string in reverse, skipping every other
character.
Answer:
dict_items([('name', 'Aman'), ('age', 27), ('address', 'Delhi')])
5. Find the correct outputs and max/min values of Y:
import random
X = random.random()
Y = random.randint(0, 4)
print(int(X), ":", Y + int(X))
Answer:
o Correct Outputs:
A) 0:0
D) 0:3
o Maximum Value for Y: 4
o Minimum Value for Y: 0
def prime():
n = int(input("Enter number to check: "))
for i in range(2, n // 2 + 1):
if n % i == 0:
print("Number is not prime\n")
break
else:
print("Number is prime\n")
Corrections Made:
SQL Questions
SHOW TABLES;
Miscellaneous Questions
1. Method SHOWLINES()
def SHOWLINES():
with open("EXAMCS.txt", "r") as file:
lines = file.readlines()
for line in lines:
if 'ke' not in line:
print(line.strip())
# Example Output:
# We all pray for everyone’s safety.
2. Function RainCount()
def RainCount():
with open("RAIN.txt", "r") as file:
content = file.read().lower() # Convert to lowercase for case-
insensitive search
count = content.count("rain")
print(f"Rain – {count}")
# Example Output:
# Rain – 2
3. Stack Operations
(i) Push_element()
def Push_element(stack, customers):
for customer in customers:
if customer[2].lower() == "goa":
stack.append((customer[0], customer[1]))
(ii) Pop_element()
def Pop_element(stack):
while stack:
print(stack.pop())
print("Stack Empty")
Example Usage:
Output:
('Bob', '5678901234')
('John', '1234567890')
Stack Empty
# Example Usage:
Ditem = {"Pen": 106, "Pencil": 59, "Notebook": 80, "Eraser": 25}
Push(Ditem)
# Output:
# Notebook Pen
# The count of elements in the stack is 2
(a) Code 1
s = "All The Best"
n = len(s)
m = ""
for i in range(0, n):
if s[i] >= 'a' and s[i] <= 'm':
m = m + s[i].upper()
elif s[i] >= 'n' and s[i] <= 'z':
m = m + s[i - 1]
elif s[i].isupper():
m = m + s[i].lower()
else:
m = m + '#'
print(m)
Output:
ALLthe bBes#
(b) Code 2
F1 = "WoNdERFUL"
F2 = "StuDenTS"
F3 = ""
for I in range(0, len(F2) + 1):
if F1[I] >= 'A' and F1[I] <= 'F':
F3 = F3 + F1[I]
elif F1[I] >= 'N' and F1[I] <= 'Z':
F3 = F3 + F2[I]
else:
F3 = F3 + "*"
print(F3)
Output:
The code contains an error: IndexError occurs when I exceeds the length of F1. The loop
condition should be range(len(F2)) instead of len(F2)+1.
W**
1. To display the details of the courses with names starting with ‘D’:
def ADD():
with open('record.csv', mode='a', newline='') as file:
writer = csv.writer(file)
empid = input("Enter Employee ID: ")
name = input("Enter Employee Name: ")
mobile = input("Enter Employee Mobile: ")
writer.writerow([empid, name, mobile])
print("Record added successfully.")
1. Primary Key:
The best attribute to be declared as a primary key is Roll no.
2. Degree of the Table:
Degree = Number of attributes = 5.
3. Insert Data into the Table:
sql
CopyEdit
INSERT INTO Fees (Rollno, Name, Class, Fees, Qtr)
VALUES (1, 'Arjun', '10th', 2500, 1),
(2, 'Meera', '9th', 2300, 2),
(3, 'Ravi', '8th', 2400, 3),
(4, 'Anita', '10th', 2500, 4);
sql
CopyEdit
DROP TABLE Fees;
sql
CopyEdit
DESCRIBE Fees;
def DataDisplay():
try:
# Establish database connection
conn = mysql.connector.connect(
host="localhost",
user="root",
password="tiger",
database="school"
)
cursor = conn.cursor()
python
CopyEdit
import pickle
def CreateFile():
with open("Book.dat", "ab") as file: # Open the file in append-binary
mode
while True:
BookNo = int(input("Enter Book Number: "))
Book_Name = input("Enter Book Name: ")
Author = input("Enter Author Name: ")
Price = float(input("Enter Price: "))
record = [BookNo, Book_Name, Author, Price]
pickle.dump(record, file) # Write the record to the binary file
choice = input("Do you want to add another record? (yes/no): ")
if choice.lower() != 'yes':
break
print("Data added to Book.dat successfully.")
python
CopyEdit
def CountRec(author_name):
count = 0
try:
with open("Book.dat", "rb") as file: # Open the file in read-binary
mode
while True:
try:
record = pickle.load(file) # Load each record
if record[2].lower() == author_name.lower(): # Check
author name
count += 1
except EOFError: # End of file reached
break
except FileNotFoundError:
print("Book.dat not found.")
return count
# Example Usage:
author = input("Enter the author's name to search: ")
print(f"Number of books by {author}: {CountRec(author)}")
Example Layout:
Building 1 ─────┐
│
Building 2 ─────┤
│
Building 3 ─────┤─── Administrative Office
│
Building 4 ─────┘