Computer Science

Python MySQL Connectivity MCQs | 50 Practice Questions with Answers | CBSE Class 12 Computer Science

Class 12 · Computer Science

Python MySQL Connectivity MCQs (50 Questions)

Practice these multiple-choice questions to strengthen your understanding of Python MySQL Connectivity. Click Show Answer to reveal the correct answer and explanation.


Q1. Which module is commonly used to connect Python with MySQL?

A. mysql.database
B. mysql.connector
C. pymysql.database
D. sql.connector

Show Answer

Answer: B. mysql.connector

Explanation: The mysql.connector module provides the required functions to connect Python programs with a MySQL database.


Q2. Which statement imports the MySQL Connector module?

A. import mysql
B. import mysql.connector
C. include mysql.connector
D. using mysql.connector

Show Answer

Answer: B.

Explanation: The correct import statement is import mysql.connector.


Q3. Which function is used to establish a connection with a MySQL database?

A. cursor()
B. connect()
C. execute()
D. open()

Show Answer

Answer: B. connect()

Explanation: The connect() function establishes a connection between Python and the MySQL server.


Q4. Which of the following information is generally required while establishing a MySQL connection?

A. Host Name
B. User Name
C. Password
D. All of these

Show Answer

Answer: D. All of these

Explanation: A connection usually requires the host, username, password, and optionally the database name.


Q5. Which statement correctly creates a MySQL connection object?

con = mysql.connector.connect(
host="localhost",
user="root",
password="root123"
)

A. Correct
B. Incorrect because host cannot be localhost.
C. Incorrect because user should always be admin.
D. Incorrect because password is not required.

Show Answer

Answer: A.

Explanation: This is the correct syntax for creating a MySQL connection.


Q6. Which method creates a cursor object?

A. execute()
B. cursor()
C. fetchall()
D. commit()

Show Answer

Answer: B. cursor()

Explanation: A cursor object is required to execute SQL statements.


Q7. Why is a cursor object required?

A. To create Python variables.
B. To execute SQL queries.
C. To close the database connection.
D. To install MySQL.

Show Answer

Answer: B.

Explanation: The cursor object provides methods for executing SQL statements and retrieving results.


Q8. Which statement correctly creates a cursor object?

A. cur = con.cursor()
B. cur = cursor(con)
C. con.createCursor()
D. cursor = con()

Show Answer

Answer: A.

Explanation: The cursor() method of the connection object returns a cursor object.


Q9. Which object is used to execute SQL commands in Python?

A. Connection Object
B. Cursor Object
C. Database Object
D. Host Object

Show Answer

Answer: B. Cursor Object

Explanation: SQL statements are executed through the cursor object.


Q10. Which statement about Python-MySQL connectivity is correct?

A. connect() establishes the connection.
B. cursor() creates a cursor object.
C. SQL queries are executed through the cursor.
D. All of these.

Show Answer

Answer: D. All of these.

Explanation: A database connection is created using connect(), a cursor is obtained using cursor(), and SQL statements are executed through the cursor.


Q11. Which cursor method is used to execute an SQL query?

A. commit()
B. fetchall()
C. execute()
D. cursor()

Show Answer

Answer: C. execute()

Explanation: The execute() method sends an SQL statement to the MySQL server for execution.


Q12. Which Python statement executes the following SQL query?

SELECT * FROM Student;

A. cur.execute("SELECT * FROM Student")
B. con.execute("SELECT * FROM Student")
C. mysql.execute("SELECT * FROM Student")
D. execute("SELECT * FROM Student")

Show Answer

Answer: A.

Explanation: The execute() method belongs to the cursor object.


Q13. Which method permanently saves changes made to the database?

A. save()
B. execute()
C. commit()
D. close()

Show Answer

Answer: C. commit()

Explanation: The commit() method permanently saves INSERT, UPDATE, and DELETE operations.


Q14. Which statement is generally required after executing an INSERT query?

A. cursor()
B. commit()
C. fetchone()
D. fetchall()

Show Answer

Answer: B. commit()

Explanation: Without commit(), the changes may not be permanently stored in the database.


Q15. Which attribute returns the number of rows affected by the last SQL statement?

A. rows()
B. count()
C. rowcount
D. totalrows

Show Answer

Answer: C. rowcount

Explanation: The rowcount attribute indicates the number of rows affected by the last executed SQL statement.


Q16. Which SQL operation usually requires the commit() method?

A. SELECT
B. INSERT
C. UPDATE
D. Both B and C

Show Answer

Answer: D. Both B and C

Explanation: INSERT, UPDATE, and DELETE modify the database, so commit() is required to save the changes.


Q17. What will the following statement return after successfully inserting one record?

print(cur.rowcount)

A. 0
B. 1
C. -1
D. None

Show Answer

Answer: B. 1

Explanation: After inserting one record successfully, rowcount returns 1.


Q18. Which sequence correctly inserts a record into a database?

A. execute() → commit()
B. commit() → execute()
C. cursor() → fetchall()
D. connect() → fetchone()

Show Answer

Answer: A.

Explanation: First execute the SQL statement using execute(), then save the changes using commit().


Q19. Which object contains the commit() method?

A. Cursor Object
B. Connection Object
C. Table Object
D. Database Object

Show Answer

Answer: B. Connection Object

Explanation: The commit() method belongs to the connection object, not the cursor object.


Q20. Which statement about execute() and commit() is correct?

A. execute() sends SQL commands to MySQL.
B. commit() permanently saves changes.
C. Both are commonly used together after INSERT, UPDATE, and DELETE operations.
D. All of these.

Show Answer

Answer: D. All of these.

Explanation: The execute() method runs SQL statements, while commit() saves database changes permanently.


Q21. Which cursor method retrieves the first row of the query result?

A. fetchall()
B. fetchone()
C. rowcount
D. execute()

Show Answer

Answer: B. fetchone()

Explanation: The fetchone() method returns the next available row from the result set, usually the first row when called immediately after execute().


Q22. Which cursor method retrieves all rows returned by a SELECT query?

A. fetch()
B. fetchone()
C. fetchall()
D. rowcount()

Show Answer

Answer: C. fetchall()

Explanation: The fetchall() method returns all the rows of the query result as a list of tuples.


Q23. Which SQL statement should be executed before calling fetchone() or fetchall()?

A. INSERT
B. DELETE
C. SELECT
D. UPDATE

Show Answer

Answer: C. SELECT

Explanation: The fetchone() and fetchall() methods retrieve records returned by a SELECT query.


Q24. What is returned by the following statement?

row = cur.fetchone()

A. A list of rows
B. One record (tuple)
C. Number of rows
D. Table structure

Show Answer

Answer: B. One record (tuple)

Explanation: fetchone() returns a single record in the form of a tuple.


Q25. What is returned by the following statement?

rows = cur.fetchall()

A. A tuple
B. A list of tuples
C. A dictionary
D. A string

Show Answer

Answer: B. A list of tuples

Explanation: The fetchall() method returns all matching records as a list containing tuples.


Q26. Which loop is commonly used to display all records returned by fetchall()?

A. while
B. for
C. if
D. switch

Show Answer

Answer: B. for

Explanation: Since fetchall() returns a list of tuples, a for loop is commonly used to display each record.


Q27. Consider the following code:

cur.execute("SELECT * FROM Student")
rows = cur.fetchall()

for r in rows:
    print(r)

What will this program display?

A. Only the first record
B. All records of the Student table
C. Only the number of records
D. An error

Show Answer

Answer: B. All records of the Student table

Explanation: fetchall() retrieves all records, and the for loop displays each record one by one.


Q28. Which method should be used if only the first matching record is required?

A. fetchall()
B. fetchone()
C. execute()
D. commit()

Show Answer

Answer: B. fetchone()

Explanation: fetchone() retrieves only one record from the result set.


Q29. Which method is more suitable for displaying an entire table?

A. fetchone()
B. fetchall()
C. commit()
D. cursor()

Show Answer

Answer: B. fetchall()

Explanation: fetchall() retrieves every record returned by the query.


Q30. Which statement about fetchone() and fetchall() is correct?

A. fetchone() returns a single record.
B. fetchall() returns all records.
C. Both methods are used after executing a SELECT query.
D. All of these.

Show Answer

Answer: D. All of these.

Explanation: Both methods retrieve query results after a SELECT statement. fetchone() returns one record, whereas fetchall() returns all records.


Q31. Which SQL command is commonly used in Python to add a new record to a table?

A. UPDATE
B. INSERT
C. DELETE
D. SELECT

Show Answer

Answer: B. INSERT

Explanation: The INSERT statement adds new records to a database table.


Q32. Which Python statement correctly executes an INSERT query?

sql = "INSERT INTO Student VALUES(101,'Aman',95)"

A. cur.execute(sql)
B. con.execute(sql)
C. mysql.execute(sql)
D. execute(sql)

Show Answer

Answer: A.

Explanation: SQL statements are executed using the execute() method of the cursor object.


Q33. Which SQL command is used in Python to modify existing records?

A. INSERT
B. DELETE
C. UPDATE
D. ALTER

Show Answer

Answer: C. UPDATE

Explanation: The UPDATE statement modifies existing records in a database table.


Q34. Which SQL command removes records from a table?

A. DELETE
B. DROP
C. REMOVE
D. ALTER

Show Answer

Answer: A. DELETE

Explanation: The DELETE statement removes one or more records from a table.


Q35. Which method should be called after executing an INSERT, UPDATE, or DELETE query?

A. fetchall()
B. fetchone()
C. commit()
D. cursor()

Show Answer

Answer: C. commit()

Explanation: The commit() method permanently saves changes made to the database.


Q36. Which code correctly updates the marks of RollNo 101?

sql = "UPDATE Student SET Marks=96 WHERE RollNo=101"

A. cur.execute(sql)
B. cur.fetchall(sql)
C. con.cursor(sql)
D. mysql.update(sql)

Show Answer

Answer: A.

Explanation: SQL statements are executed using the cursor object's execute() method.


Q37. Which code correctly deletes the record of RollNo 101?

sql = "DELETE FROM Student WHERE RollNo=101"

A. cur.execute(sql)
B. con.delete(sql)
C. mysql.execute(sql)
D. execute(sql)

Show Answer

Answer: A.

Explanation: The cursor object's execute() method is used to execute DELETE statements.


Q38. After executing an UPDATE statement, which sequence is correct?

A. execute() → commit()
B. commit() → execute()
C. fetchall() → commit()
D. fetchone() → execute()

Show Answer

Answer: A.

Explanation: First execute the SQL statement, then save the changes using commit().


Q39. Which Python object is responsible for executing INSERT, UPDATE, and DELETE statements?

A. Connection Object
B. Cursor Object
C. Database Object
D. Host Object

Show Answer

Answer: B. Cursor Object

Explanation: The cursor object executes SQL statements, while the connection object manages the database connection.


Q40. Which statement about Python database applications is correct?

A. Python can insert records into a MySQL database.
B. Python can update existing records.
C. Python can delete records from a database.
D. All of these.

Show Answer

Answer: D. All of these.

Explanation: Using Python and MySQL Connector, applications can perform all major database operations such as INSERT, UPDATE, DELETE, and SELECT.


Q41. Which placeholder is commonly used for parameterized queries in MySQL Connector/Python?

A. ?
B. %d
C. %s
D. {}

Show Answer

Answer: C. %s

Explanation: MySQL Connector/Python uses %s as the placeholder for parameterized queries, regardless of the data type.


Q42. Which statement correctly executes a parameterized INSERT query?

sql = "INSERT INTO Student VALUES(%s,%s,%s)"
data = (101,"Aman",95)

A. cur.execute(sql, data)
B. cur.execute(sql)
C. con.execute(sql, data)
D. mysql.execute(sql, data)

Show Answer

Answer: A.

Explanation: The values are passed separately as a tuple to the execute() method.


Q43. Why are parameterized queries preferred?

A. They improve readability.
B. They help prevent SQL Injection attacks.
C. They automatically commit transactions.
D. They eliminate the need for SQL.

Show Answer

Answer: B. They help prevent SQL Injection attacks.

Explanation: Parameterized queries separate SQL code from user data, making applications more secure.


Q44. Which Python method can also be used to create SQL queries dynamically?

A. append()
B. replace()
C. format()
D. join()

Show Answer

Answer: C. format()

Explanation: The format() method can be used to insert values into SQL query strings, although parameterized queries using %s are generally recommended.


Q45. Which of the following is the preferred approach for executing SQL queries in Python?

A. String concatenation
B. Parameterized queries using %s
C. Hard-coded values only
D. Using print() statements

Show Answer

Answer: B. Parameterized queries using %s

Explanation: Parameterized queries are more secure, easier to maintain, and recommended for database applications.


Q46. Which sequence correctly displays records from a MySQL table using Python?

A. connect() → cursor() → execute() → fetchall()
B. cursor() → connect() → fetchall()
C. fetchall() → execute()
D. connect() → commit() → fetchone()

Show Answer

Answer: A.

Explanation: First establish the connection, create a cursor, execute the SELECT query, and then fetch the records.


Q47. Which sequence correctly inserts a record into a MySQL table using Python?

A. connect() → cursor() → execute() → commit()
B. connect() → fetchall() → commit()
C. cursor() → fetchone() → commit()
D. execute() → connect()

Show Answer

Answer: A.

Explanation: After connecting to the database and creating a cursor, execute the INSERT statement and then save the changes using commit().


Q48. Which statement about Python-MySQL connectivity is correct?

A. connect() creates the database connection.
B. cursor() creates a cursor object.
C. execute() executes SQL statements.
D. All of these.

Show Answer

Answer: D. All of these.

Explanation: All three methods play important roles in database connectivity applications.


Q49. Which of the following methods retrieves all records from a SELECT query?

A. fetchone()
B. fetchall()
C. commit()
D. rowcount

Show Answer

Answer: B. fetchall()

Explanation: The fetchall() method returns all rows of the result set as a list of tuples.


Q50. Which statement best summarizes Python-MySQL connectivity?

A. Python can connect to MySQL using mysql.connector.
B. SQL statements are executed using the cursor object.
C. Data can be inserted, updated, deleted, and retrieved using Python programs.
D. All of these.

Show Answer

Answer: D. All of these.

Explanation: Python can establish a MySQL connection, execute SQL statements through a cursor, and perform complete database operations, making it suitable for developing database-driven applications.


Answer Key

Q.No. Answer Q.No. Answer Q.No. Answer
1B11C21B
2B12A22C
3B13C23C
4D14B24B
5A15C25B
6B16D26B
7B17B27B
8A18A28B
9B19B29B
10D20D30D
31B41C
32A42A
33C43B
34A44C
35C45B
36A46A
37A47A
38A48D
39B49B
40D50D

Practice Tips

  • Remember the basic workflow of Python-MySQL connectivity:
    • import mysql.connector
    • connect()
    • cursor()
    • execute()
    • fetchone() / fetchall()
    • commit() (for INSERT, UPDATE, DELETE)
    • close() (recommended after completing operations)
  • Understand the purpose of important methods:
    • connect() – Establishes a database connection.
    • cursor() – Creates a cursor object.
    • execute() – Executes SQL statements.
    • fetchone() – Retrieves one record.
    • fetchall() – Retrieves all records.
    • commit() – Saves changes permanently.
    • rowcount – Returns the number of affected rows.
  • Use parameterized queries with %s instead of string concatenation to improve security and prevent SQL Injection attacks.
  • Revise the sequence of operations for:
    • Displaying records
    • Inserting records
    • Updating records
    • Deleting records
  • Practice writing complete Python programs for database connectivity, as CBSE practical examinations often include application-based questions involving MySQL.