Comp 1
Comp 1
**General Instructions:**
---
i) Router
ii) Switch
iii) Compiler
iv) Hub
i) Application Layer
i) 255.0.0.0
ii) 255.255.0.0
iii) 255.255.255.0
iv) 255.255.255.255
i) HTTP
ii) FTP
iii) SMTP
iv) POP3
i) www.google.com
ii) 192.168.1.1
iii) [email protected]
iv) https://example.com
---
---
a) Explain the OSI model in brief. Mention the functions of the Transport Layer.
---
i) sqlite3
ii) mysql.connector
iii) pyodbc
iv) psycopg2
i) cursor.query()
ii) cursor.execute()
iii) execute.sql()
iv) query.execute()
c) Which command is used to fetch all rows from a MySQL query result in Python?
i) fetchrow()
ii) fetchall()
iii) fetch()
iv) fetchrows()
i) PRIMARY
iii) PK
iv) PRIMARYKEY
i) 3306
ii) 8080
iii) 1433
iv) 1521
---
**5. Short Answer Questions** (2 Marks each) (4 x 2 = 8 Marks)
d) Write a Python statement to insert a record into a MySQL table named `Students` with columns
`RollNo`, `Name`, and `Class`.
---
a) Write a Python program to display all records from a MySQL table named `Books`.
b) Explain the process of creating a database and a table in MySQL using Python code.
c) Write a Python program to update the `marks` of a student in the `Results` table where `RollNo` is
101.
d) Discuss the importance of exception handling in Python while connecting with a MySQL database.
Write a small code snippet demonstrating the use of `try` and `except` for a database connection.
---
```python
import mysql.connector
try:
cursor = con.cursor()
cursor.execute(query)
records = cursor.fetchall()
print(record)
con.close()
print("Error:", err)
```
a) What is the purpose of the `connect()` method in the above script? (1 Mark)
b) Identify the SQL query used in the script and explain its function. (1 Mark)
c) How can the script be modified to display only the names of students in Class 12? (2 Marks)
---
A school wants to store information about its staff in a table named `Staff` with columns: `StaffID`,
`Name`, `Designation`, and `Salary`.
b) A Python code snippet to insert a new record into this table with the following details:
---
A company wants to track its sales using a database. The `Sales` table has the following columns:
a) Write a Python program to calculate the total revenue by summing up `QuantitySold * Price` for
all rows in the `Sales` table. (2 Marks)
b) What will happen if the database connection is not closed after execution? Explain briefly. (2
Marks)
---
**10. Case Study Question (8 Marks)**
A network administrator needs to secure a company's database server. Answer the following:
a) What are two common security threats to a database? How can these threats be mitigated? (4
Marks)
b) Write a Python program that uses parameterized queries to prevent SQL injection while fetching
records from a `Users` table where `username` is input by the user. (4 Marks)