AmbifulWorksheets
Practice that never repeats
CBSE · Class 12 · Computer Science

Interface Python with MySQL

Standard difficulty · 20 questions · 40 marks · Time: 40 minutes

Name: Date: Score: ____ / 40

Show your working for Sections B and C. Answers are on the last page — try every question first!

Section A — Choose the correct answer8 × 1 = 8 marks
1.
Which function opens a connection from Python to a MySQL database?
  1. execute()
  2. open()
  3. cursor()
  4. connect()
2.
Table: STUDENT
RollNo | Name | Sec | City | Marks
101 | Dev | C | Delhi | 47
102 | Ananya | A | Delhi | 58
103 | Zoya | B | Delhi | 92
104 | Isha | B | Delhi | 46
105 | Vikram | C | Indore | 68
106 | Rahul | C | Pune | 40
107 | Priya | C | Delhi | 96
108 | Kabir | A | Indore | 83
What does this program print? (Output lines are separated by commas.)
import mysql.connector as m
con = m.connect(host='localhost', user='root', password='****', database='school')
cur = con.cursor()
cur.execute("SELECT Name, Marks FROM STUDENT WHERE Marks BETWEEN 58 AND 83 ORDER BY Marks")
data = cur.fetchall()
print(len(data), cur.rowcount)
  1. 3 3
  2. 3 1
  3. 1 3
  4. 3 0
3.
Table: STUDENT
RollNo | Name | Sec | City | Marks
101 | Zoya | C | Kochi | 65
102 | Dev | A | Kochi | 73
103 | Ananya | A | Indore | 56
104 | Vikram | A | Kochi | 94
105 | Rohan | C | Kochi | 66
106 | Kabir | A | Kochi | 76
After cur.execute("UPDATE STUDENT SET Marks = Marks + 4 WHERE Marks < 66") and con.commit(), what is cur.rowcount?
  1. 1
  2. 6
  3. 2
  4. 3
4.
What query string does this make?
q = "SELECT * FROM STUDENT WHERE City = '{}' AND Marks > {}".format('Shimla', 70)
print(q)
  1. SELECT * FROM STUDENT WHERE City = 'Shimla' AND Marks > 70
  2. SELECT * FROM STUDENT WHERE City = '70' AND Marks > Shimla
  3. SELECT * FROM STUDENT WHERE City = {} AND Marks > {}
  4. SELECT * FROM STUDENT WHERE City = Shimla AND Marks > 70
5.
In Python–MySQL connectivity, rowcount:
  1. returns the next row of the result as a tuple
  2. runs one SQL statement
  3. gives the number of rows fetched or changed
  4. creates the object that runs queries
6.
Table: STUDENT
RollNo | Name | Sec | City | Marks
101 | Dev | A | Indore | 91
102 | Vikram | C | Shimla | 67
103 | Ananya | C | Indore | 75
104 | Isha | B | Kochi | 82
105 | Arjun | B | Shimla | 42
106 | Rohan | B | Kochi | 93
107 | Meera | C | Indore | 58
108 | Kabir | B | Indore | 60
How many rows does this query give the cursor?
cur.execute("SELECT Name FROM STUDENT WHERE Sec IN ('A', 'C') ORDER BY Marks DESC")
  1. 5
  2. 8
  3. 3
  4. 4
7.
Which statement correctly inserts a row using %s placeholders?
  1. cur.execute("INSERT INTO STUDENT (RollNo, Name, Marks) VALUES (%d, %d, %d)", 40, 'Kabir', 47)
  2. cur.insert("STUDENT", 40, 'Kabir', 47)
  3. cur.execute("INSERT INTO STUDENT (RollNo, Name, Marks) VALUES (%s, %s, %s)", (40, 'Kabir', 47))
  4. cur.execute("INSERT INTO STUDENT VALUES (%s, %s, %s)" % 40, 'Kabir', 47)
8.
Table: STUDENT
RollNo | Name | Sec | City | Marks
101 | Isha | C | Indore | 49
102 | Priya | C | Pune | 77
103 | Vikram | A | Pune | 73
104 | Arjun | B | Indore | 53
105 | Rohan | A | Pune | 67
106 | Kabir | A | Indore | 79
107 | Zoya | C | Pune | 61
A program runs cur.execute("UPDATE STUDENT SET Marks = Marks + 2 WHERE Marks > 61") but never calls con.commit(), then closes the connection. What happens to the table?
  1. all its rows are removed
  2. it is left unchanged
  3. 4 rows are changed for good
  4. the table is deleted
Section B — Short answer8 × 2 = 16 marks
9.
What is the use of a cursor in Python–MySQL connectivity?
10.
Table: STUDENT
RollNo | Name | Sec | City | Marks
201 | Rahul | B | Jaipur | 80
202 | Arjun | A | Kochi | 69
203 | Kabir | B | Jaipur | 75
204 | Ananya | B | Jaipur | 91
205 | Priya | B | Kochi | 70
206 | Meera | C | Shimla | 90
207 | Rohan | A | Kochi | 83
What does this program print?
import mysql.connector as m
con = m.connect(host='localhost', user='root', password='****', database='school')
cur = con.cursor()
cur.execute("SELECT Name, Marks FROM STUDENT WHERE Marks BETWEEN 70 AND 75 ORDER BY Marks DESC")
data = cur.fetchall()
print(len(data), cur.rowcount)
11.
Table: STUDENT
RollNo | Name | Sec | City | Marks
101 | Rohan | B | Kochi | 72
102 | Asha | C | Pune | 46
103 | Ananya | C | Kochi | 45
104 | Kabir | A | Indore | 49
105 | Priya | B | Pune | 86
106 | Vikram | B | Indore | 64
107 | Isha | A | Indore | 61
108 | Dev | A | Pune | 75
After cur.execute("DELETE FROM STUDENT WHERE Marks < 72") and con.commit(), (a) what is cur.rowcount? (b) how many rows does the table have?
12.
Write the string printed by:
m = 50
c = 'Indore'
q = "SELECT Name FROM STUDENT WHERE Marks >= {} AND City = '{}'".format(m, c)
print(q)
13.
Put the steps of a Python program that displays the rows of a MySQL table in order. Write the letters in order.
(a) Create a cursor with cursor()
(b) Connect to the database with connect()
(c) Run the query with execute()
(d) Import the connector module
(e) Fetch the rows with fetchall() or fetchone()
(f) Close the connection with close()
14.
Table: STUDENT
RollNo | Name | Sec | City | Marks
101 | Arjun | B | Pune | 60
102 | Rohan | A | Shimla | 67
103 | Isha | B | Shimla | 72
104 | Dev | C | Pune | 75
105 | Vikram | B | Pune | 61
106 | Zoya | C | Pune | 98
107 | Meera | A | Shimla | 86
108 | Rahul | B | Indore | 82
The program runs cur.execute("SELECT Name FROM STUDENT WHERE Sec <> 'A' ORDER BY Marks DESC") and then data = cur.fetchall(). What is len(data)?
15.
Table: STUDENT
RollNo | Name | Sec | City | Marks
201 | Priya | C | Delhi | 58
202 | Meera | C | Delhi | 76
203 | Ananya | B | Indore | 69
204 | Rahul | C | Delhi | 80
205 | Kabir | C | Jaipur | 65
206 | Isha | C | Jaipur | 94
207 | Rohan | C | Indore | 41
The program runs cur.execute("SELECT Name, Marks FROM STUDENT WHERE Marks BETWEEN 58 AND 80 ORDER BY Marks DESC"), then calls cur.fetchone() 3 times, then data = cur.fetchall(). What is len(data)?
16.
Table: STUDENT
RollNo | Name | Sec | City | Marks
101 | Arjun | B | Kochi | 42
102 | Dev | A | Jaipur | 88
103 | Isha | C | Jaipur | 54
104 | Zoya | C | Delhi | 80
105 | Vikram | B | Kochi | 47
106 | Ananya | C | Delhi | 82
What does this print?
cur.execute("SELECT Name, Marks FROM STUDENT ORDER BY Marks DESC")
print(cur.fetchone())
Section C — Word problems4 × 4 = 16 marks
17.
Explain the steps of connecting Python with MySQL to display records, with the code for each step.
18.
Table: STUDENT
RollNo | Name | Sec | City | Marks
1 | Asha | A | Shimla | 45
2 | Rohan | B | Pune | 76
3 | Meera | A | Shimla | 66
4 | Arjun | C | Jaipur | 85
5 | Kabir | C | Shimla | 73
6 | Vikram | B | Jaipur | 83
7 | Isha | C | Pune | 77
8 | Priya | B | Pune | 75
(a) What does this program print? (b) What does each of connect(), cursor() and execute() do? (c) Why is con.close() good practice?
import mysql.connector as m
con = m.connect(host='localhost', user='root', password='****', database='school')
cur = con.cursor()
cur.execute("SELECT Name, Marks FROM STUDENT WHERE Sec IN ('B', 'C') ORDER BY Marks DESC")
data = cur.fetchall()
print(len(data), cur.rowcount)
19.
Table: STUDENT
RollNo | Name | Sec | City | Marks
1 | Meera | B | Pune | 72
2 | Asha | C | Indore | 93
3 | Vikram | C | Indore | 86
4 | Arjun | C | Indore | 65
5 | Priya | C | Jaipur | 59
6 | Zoya | B | Indore | 53
7 | Kabir | A | Indore | 77
A program runs these lines:
cur.execute("UPDATE STUDENT SET Marks = Marks + 3 WHERE Marks < 59")
con.commit()
print(cur.rowcount)
cur.execute("SELECT COUNT(*) FROM STUDENT")
print(cur.fetchone()[0])
(a) What does it print? (b) Why is commit() needed?
20.
Table: STUDENT
RollNo | Name | Sec | City | Marks
201 | Meera | C | Indore | 78
202 | Rohan | C | Indore | 53
203 | Ananya | B | Delhi | 41
204 | Rahul | B | Indore | 69
205 | Priya | C | Delhi | 55
206 | Zoya | B | Kochi | 97
207 | Vikram | C | Kochi | 67
Write a Python program that asks for a city and a mark, and prints the name and marks of every student of STUDENT (database school) from that city who scored more than that mark, using %s placeholders. What does it print for Delhi and 55?
AmbifulWorksheets
Practice that never repeats
CBSE · Class 12 · Computer Science

Answer key

Interface Python with MySQL · AW-12CS-PSQ-2026-V1 · For parents — fold back or tear off before handing the sheet to your child.

QAnswerWorking
1(d) connect()—
2(a) 3 3fetchone() gives the next row as a tuple; fetchall() gives all remaining rows as a list of tuples
3(c) 2rows where Marks < 66
4(a) SELECT * FROM STUDENT WHERE City = 'Shimla' AND Marks > 70format() puts its arguments into the {} places in order
5(c) gives the number of rows fetched or changed—
6(d) 4rows where Sec IN ('A', 'C')
7(c) cur.execute("INSERT INTO STUDENT (RollNo, Name, Marks) VALUES (%s, %s, %s)", (40, 'Kabir', 47))execute() takes the query and a tuple of values for the %s places
8(b) it is left unchangedwithout commit() the change is not saved
9It is the object that sends SQL statements to the database with execute() and holds the result rows to be fetched.—
102 2fetchone() returns a tuple; fetchall() a list of tuples
11(a) 5; (b) 3DELETE removes the matching rows
12SELECT Name FROM STUDENT WHERE Marks >= 50 AND City = 'Indore'format() fills the {} places in order
13d, b, a, c, e, fImport the connector module → Connect to the database with connect() → Create a cursor with cursor() → Run the query with execute() → Fetch the rows with fetchall() or fetchone() → Close the connection with close()
146rows where Sec <> 'A'
1525 rows in the result; 3 already fetched
16('Dev', 88)the first row of the sorted result, as a tuple
171. import mysql.connector as m; 2. con = m.connect(host='localhost', user='root', password='…', database='school'); 3. cur = con.cursor(); 4. cur.execute('SELECT * FROM STUDENT'); 5. rows = cur.fetchall(); for r in rows: print(r); 6. con.close().—
18(a) 6 6; (b) connect() opens the connection to the database, cursor() makes the object that runs queries, execute() runs the SQL; (c) it frees the connection on the server.fetchone()/fetchall() read the result
19(a) 1, 7; (b) changes made by UPDATE or DELETE are not saved in the database until they are committed.UPDATE keeps every row
20import mysql.connector as m; con = m.connect(host='localhost', user='root', password='…', database='school'); cur = con.cursor(); c = input('City: '); k = int(input('Marks: ')); cur.execute("SELECT Name, Marks FROM STUDENT WHERE City = %s AND Marks > %s", (c, k)); for row in cur.fetchall(): print(row[0], row[1]); con.close(). For Delhi and 55 it prints: nothing.connect → cursor → execute with a tuple → fetchall → close