[go: up one dir, main page]

0% found this document useful (0 votes)
0 views5 pages

Python Plus Two Test Paper Answers

The document is an answer key for a Computer Science (Python) test paper, containing answers to various questions across multiple sections. It includes code snippets for file handling, database operations, and data manipulation using Python. The answers cover topics such as CSV, stacks, and the use of libraries like pickle and mysql.connector.

Uploaded by

reshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views5 pages

Python Plus Two Test Paper Answers

The document is an answer key for a Computer Science (Python) test paper, containing answers to various questions across multiple sections. It includes code snippets for file handling, database operations, and data manipulation using Python. The answers cover topics such as CSV, stacks, and the use of libraries like pickle and mysql.connector.

Uploaded by

reshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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('students.csv', 'r') as f:
reader = csv.reader(f)
for i, row in enumerate(reader):
if i < 3:
print(row)
```

9. [5, 10, 15, 25]

10. ```python
import mysql.connector
mydb = mysql.connector.connect(host='localhost', user='root', password='1234',
database='school')
```

11. Advantage: Faster and compact storage.


Disadvantage: Not human-readable.

12. (a) csv (b) mysql.connector

Section C – Answers
13. ```python
import pickle
f = open('emp.dat', 'wb')
for i in range(5):
empno = int(input('EmpNo: '))
name = input('Name: ')
sal = float(input('Salary: '))
pickle.dump([empno, name, sal], f)
f.close()
```

14. ```python
def Push(Stack, item):
Stack.append(item)

def Pop(Stack):
if Stack:
return Stack.pop()
else:
return None
```

15. ```python
import csv
with open('marks.csv', 'r') as f:
reader = csv.DictReader(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 stk.append(12) → [12]


After stk.append(34) → [12, 34]
After stk.append(56) → [12, 34, 56]
After stk.pop() → [12, 34]
After stk.append(78) → [12, 34, 78]

18. ```python
import pickle
f = open('data.dat', 'rb')
try:
while True:
print(pickle.load(f))
except EOFError:
f.close()
```

Section D – Answers
19. ```python
import csv
with open('product.csv', 'w', newline='') as f:
writer = csv.writer(f)
for i in range(10):
pid = int(input('ProductID: '))
name = input('Name: ')
price = float(input('Price: '))
writer.writerow([pid, name, price])

with open('product.csv', 'r') as f:


reader = csv.reader(f)
for row in reader:
if float(row[2]) > 500:
print(row)
```

20. ```python
def push(stack):
item = input('Enter item: ')
stack.append(item)

def pop(stack):
if stack:
print('Popped:', stack.pop())
else:
print('Stack Empty')

def display(stack):
print(stack)

stack = []
while True:
ch = input('1.Push 2.Pop 3.Display 4.Exit: ')
if ch == '1': push(stack)
elif ch == '2': pop(stack)
elif ch == '3': display(stack)
else: break
```
21. ```python
import mysql.connector
mydb = mysql.connector.connect(host='localhost', user='root', password='',
database='school')
cursor = mydb.cursor()
cursor.execute('CREATE TABLE Student(RollNo INT, Name VARCHAR(30), Marks INT)')
cursor.execute("INSERT INTO Student VALUES(1, 'Amit', 85)")
cursor.execute("INSERT INTO Student VALUES(2, 'Ria', 90)")
mydb.commit()
cursor.execute('SELECT * FROM Student')
for row in cursor.fetchall():
print(row)
```

22. ```python
import pickle
name = input('Enter player name: ')
records = []
with open('player.dat', 'rb') as f:
try:
while True:
rec = pickle.load(f)
if rec[0] == name:
rec[1] += 500
records.append(rec)
except EOFError:
pass
with open('player.dat', 'wb') as f:
for rec in records:
pickle.dump(rec, f)
```

23. ```python
import csv
total = 0
with open('sales.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
total += float(row[1])
print('Total Sales:', total)
```
Section E – Answers
```python
import pickle
f = open('emp.dat', 'wb')
for i in range(5):
empid = int(input('EmpID: '))
name = input('Name: ')
dept = input('Department: ')
sal = float(input('Salary: '))
pickle.dump([empid, name, dept, sal], f)
f.close()

f = open('emp.dat', 'rb')
try:
while True:
rec = pickle.load(f)
if rec[2].lower() == 'hr':
print(rec)
except EOFError:
f.close()
```

You might also like