Answer Key – Computer Science (Python) Test Paper
Section A – Answers
1. rb, wb (also ab, rb+, wb+, ab+).
2. Comma (`,`).
3. Done
4. Stack.
5. DESCRIBE table_name;
6. (a) Comma Separated Values, (b) Database Management System.
Section B – Answers
7. Similarity: Both store data for later use.
Difference: CSV is human-readable, binary is not.
8. ```python
import csv
with open('[Link]', 'r') as f:
reader = [Link](f)
for i, row in enumerate(reader):
if i < 3:
print(row)
```
9. [5, 10, 15, 25]
10. ```python
import [Link]
mydb = [Link](host='localhost', user='root', password='1234',
database='school')
```
11. Advantage: Faster and compact storage.
Disadvantage: Not human-readable.
12. (a) csv (b) [Link]
Section C – Answers
13. ```python
import pickle
f = open('[Link]', 'wb')
for i in range(5):
empno = int(input('EmpNo: '))
name = input('Name: ')
sal = float(input('Salary: '))
[Link]([empno, name, sal], f)
[Link]()
```
14. ```python
def Push(Stack, item):
[Link](item)
def Pop(Stack):
if Stack:
return [Link]()
else:
return None
```
15. ```python
import csv
with open('[Link]', 'r') as f:
reader = [Link](f)
for row in reader:
if int(row['Science']) > 80:
print(row['Name'])
```
16. (a) CREATE TABLE Books(BookID INT, Title VARCHAR(50), Price FLOAT);
(b) INSERT INTO Books VALUES(1, 'Python', 550);
(c) SELECT * FROM Books;
17. After [Link](12) → [12]
After [Link](34) → [12, 34]
After [Link](56) → [12, 34, 56]
After [Link]() → [12, 34]
After [Link](78) → [12, 34, 78]
18. ```python
import pickle
f = open('[Link]', 'rb')
try:
while True:
print([Link](f))
except EOFError:
[Link]()
```
Section D – Answers
19. ```python
import csv
with open('[Link]', 'w', newline='') as f:
writer = [Link](f)
for i in range(10):
pid = int(input('ProductID: '))
name = input('Name: ')
price = float(input('Price: '))
[Link]([pid, name, price])
with open('[Link]', 'r') as f:
reader = [Link](f)
for row in reader:
if float(row[2]) > 500:
print(row)
```
20. ```python
def push(stack):
item = input('Enter item: ')
[Link](item)
def pop(stack):
if stack:
print('Popped:', [Link]())
else:
print('Stack Empty')
def display(stack):
print(stack)
stack = []
while True:
ch = input('[Link] [Link] [Link] [Link]: ')
if ch == '1': push(stack)
elif ch == '2': pop(stack)
elif ch == '3': display(stack)
else: break
```
21. ```python
import [Link]
mydb = [Link](host='localhost', user='root', password='',
database='school')
cursor = [Link]()
[Link]('CREATE TABLE Student(RollNo INT, Name VARCHAR(30), Marks INT)')
[Link]("INSERT INTO Student VALUES(1, 'Amit', 85)")
[Link]("INSERT INTO Student VALUES(2, 'Ria', 90)")
[Link]()
[Link]('SELECT * FROM Student')
for row in [Link]():
print(row)
```
22. ```python
import pickle
name = input('Enter player name: ')
records = []
with open('[Link]', 'rb') as f:
try:
while True:
rec = [Link](f)
if rec[0] == name:
rec[1] += 500
[Link](rec)
except EOFError:
pass
with open('[Link]', 'wb') as f:
for rec in records:
[Link](rec, f)
```
23. ```python
import csv
total = 0
with open('[Link]', 'r') as f:
reader = [Link](f)
for row in reader:
total += float(row[1])
print('Total Sales:', total)
```
Section E – Answers
```python
import pickle
f = open('[Link]', 'wb')
for i in range(5):
empid = int(input('EmpID: '))
name = input('Name: ')
dept = input('Department: ')
sal = float(input('Salary: '))
[Link]([empid, name, dept, sal], f)
[Link]()
f = open('[Link]', 'rb')
try:
while True:
rec = [Link](f)
if rec[2].lower() == 'hr':
print(rec)
except EOFError:
[Link]()
```