{"id":65964,"date":"2026-04-21T21:45:49","date_gmt":"2026-04-21T21:45:49","guid":{"rendered":"https:\/\/www.askpython.com\/?p=65964"},"modified":"2026-04-21T21:46:15","modified_gmt":"2026-04-21T21:46:15","slug":"100-python-sql-database-mcqs","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/100-python-sql-database-mcqs","title":{"rendered":"Top 100 Python SQL &amp; Database MCQs with Answers (2026)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Python database programming isn\u2019t just about writing queries, it\u2019s also about understanding the right tools. From sqlite3 to MySQL and PostgreSQL (via mysql-connector-python and psycopg2), and even NoSQL with pymongo, each plays an important role. You also need a basic understanding of SQLAlchemy and pandas.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, I\u2019ve combined all these concepts into a set of 100 Python SQL &amp; Database MCQs, helping you prepare for your upcoming interviews or exams.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">100 Python SQL &amp; Database MCQs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Each of these 100 Python SQL &amp; Database MCQs covers a key concept. Master them, and you\u2019ll be interview-ready.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Q1. Which built-in Python module is commonly used to interact with SQLite databases?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. pymysql<br>\nB. sqlite3<br>\nC. mongodb<br>\nD. sqlalchemy\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>sqlite3<\/code> module is a standard library module in Python that provides a lightweight disk-based database without requiring a separate server process.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q2. What is the primary role of a cursor object in Python database programming?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. To establish a network connection<br>\nB. To execute SQL commands and traverse results<br>\nC. To close the database file<br>\nD. To encrypt the database\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The cursor object allows Python code to execute SQL commands and fetch results from the database query.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q3. Which method is used to save changes made to a database permanently in Python?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. save()<br>\nB. execute()<br>\nC. commit()<br>\nD. update()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>The <code>commit()<\/code> method is called on the connection object to commit the current transaction and save changes permanently.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q4. In Python&#8217;s sqlite3 module, which placeholder is recommended for parameter substitution to prevent SQL injection?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. %s<br>\nB. ?<br>\nC. :1<br>\nD. $ <\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>sqlite3<\/code> module uses the question mark <code>?<\/code> style as a placeholder for parameter substitution.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q5. Which method is used to retrieve all rows from the result set of a query?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. fetchone()<br>\nB. fetchmany()<br>\nC. fetchall()<br>\nD. getall()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>The <code>fetchall()<\/code> method retrieves all rows of a query result set and returns them as a list of tuples.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q6. What happens if you do not call commit() after executing an INSERT or UPDATE statement in a Python database script?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. The database is automatically updated<br>\nB. An error is raised immediately<br>\nC. No changes are saved to the database<br>\nD. The script crashes\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>Without calling <code>commit()<\/code>, the transaction is rolled back upon closing the connection, and no changes are saved.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q7. Which standard Python library feature acts as a generic interface for database API specifications?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. DB-API 2.0<br>\nB. JDBC<br>\nC. ODBC<br>\nD. PEP 8\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>Python DB-API 2.0 (PEP 249) defines a standard interface for database access modules in Python.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q8. To connect to a MySQL database using Python, which third-party library is most commonly installed?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. mysql-connector-python<br>\nB. mysql-server<br>\nC. python-mysql<br>\nD. db-mysql\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>The <code>mysql-connector-python<\/code> is the official Oracle driver that allows Python to connect to MySQL databases.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q9. What is the correct way to close a database connection object named conn?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. conn.close()<br>\nB. close(conn)<br>\nC. conn.end()<br>\nD. conn.disconnect()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>The <code>close()<\/code> method is called on the connection object to close the database connection properly.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q10. Which exception is raised for database errors defined by the Python DB-API?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. DatabaseError<br>\nB. SQLError<br>\nC. ConnectionError<br>\nD. DataError\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br><code>DatabaseError<\/code> is the base exception class for errors related to the database in the DB-API specification.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q11. Which method retrieves only the next row from the result set?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. fetchall()<br>\nB. fetchone()<br>\nC. fetchrow()<br>\nD. nextrow()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>fetchone()<\/code> retrieves the next row of a query result set, returning a single tuple or None if no more rows are available.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q12. In the context of SQL injection attacks, what is the safest way to construct SQL queries in Python?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. String concatenation<br>\nB. Using f-strings<br>\nC. Using parameter substitution<br>\nD. Using format()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>Parameter substitution allows the database driver to handle escaping of special characters, preventing SQL injection.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q13. Which method is used to execute a SQL command that modifies the database schema, like CREATE TABLE?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. commit()<br>\nB. execute()<br>\nC. run()<br>\nD. modify()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>execute()<\/code> method is used to execute any SQL statement, including DDL commands like CREATE TABLE.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q14. What does the rowcount attribute of a cursor object return?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. The total number of rows in the table<br>\nB. The number of rows affected by the last SQL statement<br>\nC. The number of columns in the result<br>\nD. The current row index\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>rowcount<\/code> attribute returns the number of rows affected by an UPDATE, INSERT, or DELETE statement.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q15. Which of the following is NOT a valid standard DB-API exception?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. IntegrityError<br>\nB. OperationalError<br>\nC. SyntaxError<br>\nD. ProgrammingError\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br><code>SyntaxError<\/code> is a standard Python exception, while the others are specific database exceptions defined in DB-API.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q16. When using the psycopg2 library for PostgreSQL, which placeholder style is commonly used?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. ?<br>\nB. %s<br>\nC. :var<br>\nD. @param\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>Psycopg2 uses the Python extended format codes, specifically <code>%s<\/code>, for variable binding.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q17. How can you create an in-memory SQLite database that exists only for the duration of the script?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. sqlite3.connect(&#8220;temp.db&#8221;)<br>\nB. sqlite3.connect(&#8220;:memory:&#8221;)<br>\nC. sqlite3.connect(&#8220;ram.db&#8221;)<br>\nD. sqlite3.memory_connect()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>Passing the string <code>\":memory:\"<\/code> to the connect function creates an in-memory database.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q18. Which method executes the same SQL command multiple times with different parameters?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. execute()<br>\nB. executemany()<br>\nC. executescript()<br>\nD. executeall()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>executemany()<\/code> method is efficient for bulk operations like inserting multiple rows.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q19. In SQLAlchemy, which class represents the core structure for database interaction without using ORM?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Session<br>\nB. Engine<br>\nC. Model<br>\nD. Schema\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>Engine<\/code> class is the starting point for SQLAlchemy, managing the connection pool and dialect.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q20. What is the purpose of the lastrowid attribute in a cursor object?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Returns the ID of the last deleted row<br>\nB. Returns the ID of the last inserted row<br>\nC. Returns the ID of the last updated row<br>\nD. Returns the total count of rows\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>lastrowid<\/code> attribute provides the row ID of the last inserted row (useful for auto-increment fields).<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q21. Which Python library is widely used for interacting with MongoDB?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. pymongo<br>\nB. mysqlclient<br>\nC. sqlite3<br>\nD. psycopg2\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br><code>PyMongo<\/code> is the recommended driver for connecting Python applications to MongoDB databases.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q22. In SQLite, which method allows executing multiple SQL statements separated by semicolons at once?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. execute()<br>\nB. executescript()<br>\nC. executemany()<br>\nD. batch()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>executescript()<\/code> executes a script of multiple SQL statements issued as a single string.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q23. What is SQL Injection?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. A method to insert data faster<br>\nB. A security vulnerability allowing malicious SQL code execution<br>\nC. A database backup technique<br>\nD. A Python library for SQL\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>SQL Injection occurs when untrusted user data is sent to an interpreter as part of a command or query.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q24. Which of the following correctly describes the behavior of the &#8216;with&#8217; statement when opening a database connection?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. It automatically closes the connection<br>\nB. It automatically commits the transaction<br>\nC. It automatically creates a table<br>\nD. It disables error handling\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>The context manager ensures that resources like database connections are closed properly after execution.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q25. In MySQL Connector Python, which argument in the connect() method specifies the database name?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. db<br>\nB. schema<br>\nC. database<br>\nD. name\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>The <code>database<\/code> parameter is used to specify the name of the database to connect to.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q26. Which method on a connection object is used to cancel a transaction?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. commit()<br>\nB. rollback()<br>\nC. cancel()<br>\nD. undo()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>rollback()<\/code> method reverts the database to the state before the transaction began.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q27. What does the description attribute of a cursor object return?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. The SQL query string<br>\nB. Metadata about the columns in the result set<br>\nC. The number of rows<br>\nD. The database name\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>description<\/code> attribute returns a tuple of column information tuples, describing the result columns.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q28. Which exception indicates a violation of a database constraint, like a primary key conflict?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. ProgrammingError<br>\nB. IntegrityError<br>\nC. OperationalError<br>\nD. InterfaceError\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>IntegrityError<\/code> is raised when the relational integrity of the database is affected (e.g., duplicate key).<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q29. In PyMongo, what does the find() method return?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. A list of dictionaries<br>\nB. A single document<br>\nC. A Cursor object<br>\nD. A tuple\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>The <code>find()<\/code> method returns a Cursor instance which can be iterated over to access documents.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q30. Which Python function allows you to read SQL results directly into a Pandas DataFrame?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. pd.read_table()<br>\nB. pd.read_sql()<br>\nC. pd.import_sql()<br>\nD. pd.load_db()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>pd.read_sql()<\/code> is a Pandas function that executes a SQL query and loads the result into a DataFrame.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q31. What is the correct syntax to create a cursor object from a connection object conn?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. conn.create_cursor()<br>\nB. cursor = conn.cursor()<br>\nC. cursor(conn)<br>\nD. conn.new_cursor()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The standard DB-API method to create a cursor is calling <code>.cursor()<\/code> on the active connection object.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q32. Which library is an ORM (Object Relational Mapper) for Python?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. sqlite3<br>\nB. SQLAlchemy<br>\nC. psycopg2<br>\nD. PyMongo\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>SQLAlchemy provides a full suite of well-known enterprise-level persistence patterns and ORM capabilities.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q33. In SQLite, which data type is used to store floating-point numbers?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. INTEGER<br>\nB. TEXT<br>\nC. REAL<br>\nD. FLOAT\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>SQLite uses the <code>REAL<\/code> data type to store floating-point values.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q34. Which exception is raised when a SQL statement has a syntax error?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. DatabaseError<br>\nB. ProgrammingError<br>\nC. SyntaxError<br>\nD. DataError\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>ProgrammingError<\/code> is raised for errors caused by programming errors like incorrect SQL syntax.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q35. How do you install the MySQL connector for Python using pip?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. pip install mysql<br>\nB. pip install mysql-connector<br>\nC. pip install mysql-connector-python<br>\nD. pip install mysqlclient\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>The official connector package name is <code>mysql-connector-python<\/code>.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q36. Which dictionary-style placeholder does SQLite support for named parameters?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. :name<br>\nB. %(name)s<br>\nC. $name<br>\nD. @name\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>SQLite supports named placeholders using a colon followed by the name, like <code>:name<\/code>.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q37. In Pandas, what does the chunksize argument in read_sql() do?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Limits the total rows read<br>\nB. Returns an iterator for reading data in chunks<br>\nC. Skips rows<br>\nD. Sets the memory limit\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>chunksize<\/code> argument returns an iterator that yields DataFrames of the specified size.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q38. What is the default isolation level for a connection in sqlite3?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. None<br>\nB. SERIALIZABLE<br>\nC. READ COMMITTED<br>\nD. AUTOCOMMIT\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>By default, <code>sqlite3<\/code> uses SERIALIZABLE isolation, ensuring the highest level of isolation.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q39. Which method is used to convert a SQL result row into a dictionary instead of a tuple in sqlite3?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. cursor.set_factory(dict)<br>\nB. conn.row_factory = sqlite3.Row<br>\nC. cursor.as_dict(True)<br>\nD. fetchdict()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>Setting <code>row_factory<\/code> to <code>sqlite3.Row<\/code> allows accessing columns by name.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q40. What is the function of the executemany() method in Python DB-API?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Executes multiple different SQL queries<br>\nB. Executes a SQL query against multiple parameter sequences<br>\nC. Executes a script<br>\nD. Manages multiple database connections\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>It prepares a database operation and executes it against all parameter sequences found in the sequence.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q41. Which of the following represents a connection string URI for SQLite?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. sqlite:\/\/\/filename.db<br>\nB. sqlite:filename.db<br>\nC. file:sqlite:\/\/filename.db<br>\nD. db:sqlite:\/\/filename.db\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>The standard URI format for SQLite often uses three slashes for relative paths (e.g., <code>sqlite:\/\/\/example.db<\/code>).<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q42. What is the correct way to insert a document into a MongoDB collection using PyMongo?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. collection.add()<br>\nB. collection.insert()<br>\nC. collection.insert_one()<br>\nD. collection.save()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>In newer versions of PyMongo, <code>insert_one()<\/code> is used to insert a single document.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q43. Which of the following is a valid argument for the sqlite3.connect() function?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. timeout<br>\nB. user<br>\nC. password<br>\nD. port\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br><code>timeout<\/code> specifies how long the connection should wait for a lock to go away.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q44. Which method allows you to write a Pandas DataFrame to a SQL database?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. df.to_sql()<br>\nB. df.write_sql()<br>\nC. df.export_sql()<br>\nD. df.save_sql()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>The <code>to_sql()<\/code> method writes records stored in a DataFrame to a SQL database.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q45. What exception is raised when the connection to the database fails?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. ConnectionError<br>\nB. OperationalError<br>\nC. InterfaceError<br>\nD. DatabaseError\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>OperationalError<\/code> covers errors related to the database operation, such as connection failures.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q46. Which method is used to delete records from a MongoDB collection?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. collection.discard()<br>\nB. collection.remove()<br>\nC. collection.delete_one()<br>\nD. collection.erase()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br><code>delete_one()<\/code> deletes a single document matching the filter criteria.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q47. In SQLAlchemy, what is the role of the Session class?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. To create tables<br>\nB. To manage the database connection and transactions<br>\nC. To define models<br>\nD. To generate SQL strings\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The Session is the ORM&#8217;s handle to the database, managing transactions and object states.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q48. What is the purpose of the isolation_level parameter in sqlite3.connect()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. To set the database encryption level<br>\nB. To control the transaction locking behavior<br>\nC. To limit user access<br>\nD. To set the file permissions\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>isolation_level<\/code> parameter controls how transactions are handled (e.g., None for autocommit).<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q49. How can you handle database errors in Python gracefully?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Using if statements<br>\nB. Using try&#8230;except blocks<br>\nC. Using assertions<br>\nD. Using debug mode\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>Wrap database code in <code>try...except<\/code> blocks to catch specific database exceptions.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q50. Which method is used to check if a table exists in an SQLite database?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. sqlite3.table_exists()<br>\nB. Querying sqlite_master table<br>\nC. conn.check_table()<br>\nD. cursor.table_list()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>You can query the <code>sqlite_master<\/code> system table to check if a table name exists.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q51. What does the &#8216;autocommit&#8217; feature in a database connection imply?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Automatic rollback on error<br>\nB. Every SQL statement is a transaction and committed immediately<br>\nC. Automatic connection closing<br>\nD. Batch processing of queries\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>In autocommit mode, each SQL statement is treated as a transaction and is committed immediately.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q52. Which function is used to retrieve the ID of the last inserted row in SQLite?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. cursor.last_row_id()<br>\nB. conn.insert_id()<br>\nC. cursor.lastrowid<br>\nD. db.get_id()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>The attribute <code>cursor.lastrowid<\/code> stores the row ID of the last successfully inserted row.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q53. In PyMongo, how do you select only specific fields from a document?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Using the filter parameter<br>\nB. Using the projection parameter<br>\nC. Using the fields parameter<br>\nD. Using the select parameter\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The projection parameter (a dictionary) in <code>find()<\/code> specifies which fields to include or exclude.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q54. Which DB-API exception is raised for errors due to division by zero or value out of range?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. DataError<br>\nB. DatabaseError<br>\nC. ProgrammingError<br>\nD. OperationalError\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br><code>DataError<\/code> handles errors related to data processing, such as numeric overflow or division by zero.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q55. What is the first argument passed to the sqlite3.connect() function?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Host address<br>\nB. File path of the database<br>\nC. User name<br>\nD. Port number\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>For SQLite, the first argument is the path to the database file (or <code>:memory:<\/code>).<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q56. Which of the following is a primary advantage of using an ORM like SQLAlchemy?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Direct SQL execution<br>\nB. Database abstraction and portability<br>\nC. Slower performance<br>\nD. Manual transaction handling\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>ORMs allow developers to work with Python objects, abstracting away the underlying SQL and database specifics.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q57. How do you filter results in a MongoDB query using PyMongo?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Passing a WHERE clause string<br>\nB. Passing a dictionary to find()<br>\nC. Using filter() method<br>\nD. Using query strings\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>You pass a query dictionary to the <code>find()<\/code> method to filter documents.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q58. In Pandas to_sql(), what does the parameter if_exists=&#8217;replace&#8217; do?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Replaces rows with same ID<br>\nB. Drops the table before inserting new values<br>\nC. Raises a ValueError<br>\nD. Appends data\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>It drops the existing table and creates a new one before inserting the DataFrame data.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q59. What is the output type of cursor.fetchone() if no rows are available?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. []<br>\nB. ()<br>\nC. None<br>\nD. False\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>If no more rows are available, <code>fetchone()<\/code> returns <code>None<\/code>.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q60. Which exception is raised when the database encounters an internal error?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. InternalError<br>\nB. DatabaseError<br>\nC. SystemError<br>\nD. OperationalError\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br><code>InternalError<\/code> is raised when the database encounters an internal error (e.g., cursor not valid).<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q61. Which of the following correctly explains the concept of &#8216;Connection Pooling&#8217;?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Keeping connections open for reuse to improve performance<br>\nB. Storing data in memory<br>\nC. Closing connections immediately after use<br>\nD. Encrypting database connections\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>Connection pooling maintains a cache of database connections to reduce overhead of creating new connections.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q62. How do you connect to a PostgreSQL database using Python?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. import postgres<br>\nB. import psycopg2<br>\nC. import pgsql<br>\nD. import postgresql\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>psycopg2<\/code> is the most popular PostgreSQL adapter for Python.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q63. Which method is used to rename a column in SQLite?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. ALTER TABLE &#8230; RENAME COLUMN<br>\nB. CHANGE COLUMN<br>\nC. MODIFY COLUMN<br>\nD. RENAME COLUMN\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>The SQL syntax <code>ALTER TABLE table_name RENAME COLUMN old TO new<\/code> is used in SQLite.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q64. What is the return value of cursor.execute() for a SELECT statement?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. List of rows<br>\nB. The cursor object itself<br>\nC. Number of rows<br>\nD. True\/False\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>execute()<\/code> method returns the cursor object, allowing method chaining, though results are fetched separately.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q65. Which SQLAlchemy function is used to establish a connection to the database?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. sqlalchemy.connect()<br>\nB. sqlalchemy.create_engine()<br>\nC. sqlalchemy.start()<br>\nD. sqlalchemy.open()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>create_engine()<\/code> produces an Engine object based on a URL, which handles connections.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q66. In PyMongo, what is the difference between insert_one() and insert_many()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. insert_one is faster<br>\nB. insert_many inserts a list of documents<br>\nC. insert_one creates a collection<br>\nD. insert_many creates a database\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>insert_many()<\/code> accepts a list of documents and inserts them in a single operation.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q67. Which parameter in Pandas read_sql() prevents SQL injection by passing parameters safely?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. safe_query<br>\nB. params<br>\nC. injection_safe<br>\nD. security\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>params<\/code> parameter passes arguments safely to the underlying database driver.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q68. What does the &#8216;detect_types&#8217; parameter in sqlite3.connect() do?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Detects primary keys<br>\nB. Detects data types of columns automatically<br>\nC. Detects table names<br>\nD. Detects foreign keys\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>It controls how types are converted when fetching data, e.g., parsing date\/timestamp strings.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q69. Which module is typically used to connect Python to Oracle databases?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. oradb<br>\nB. cx_Oracle<br>\nC. oracle-python<br>\nD. pyoracle\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>cx_Oracle<\/code> (now evolving into python-oracledb) is the standard module for Oracle Database connectivity.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q70. Which Python method executes an SQL script stored in a file?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. run_script()<br>\nB. executescript()<br>\nC. load_sql()<br>\nD. batch_execute()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>executescript()<\/code> is used to execute multiple SQL statements at once in SQLite.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q71. What is the primary key constraint violation categorized as in Python DB-API?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. ProgrammingError<br>\nB. DataError<br>\nC. IntegrityError<br>\nD. InternalError\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>Integrity constraints like unique or primary key violations raise <code>IntegrityError<\/code>.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q72. How can you convert a SQLite row result to a standard Python dictionary?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. dict(row)<br>\nB. row.to_dict()<br>\nC. cursor.dict_fetch()<br>\nD. No direct method\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>If the row_factory is set to <code>sqlite3.Row<\/code>, <code>dict(row)<\/code> converts it to a dictionary.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q73. In MongoDB, what function sorts the result documents?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. order_by()<br>\nB. sort()<br>\nC. sort_by()<br>\nD. arrange()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>In PyMongo, the <code>sort()<\/code> method is called on a cursor to sort documents.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q74. What does the &#8216;check_same_thread&#8217; argument do in sqlite3.connect()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Checks for thread safety<br>\nB. Restricts connection usage to the creating thread<br>\nC. Allows multi-threading<br>\nD. Checks for duplicate threads\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>By default, SQLite checks that the connection is used in the same thread that created it.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q75. Which library component is essential for defining database schema in SQLAlchemy ORM?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. declarative_base<br>\nB. schema_builder<br>\nC. database_model<br>\nD. table_def\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br><code>declarative_base()<\/code> creates a base class for declarative class definitions.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q76. What type of object does the Pandas read_sql_query() function return?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. SQLCursor<br>\nB. DataFrame<br>\nC. List of tuples<br>\nD. Dictionary\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>It reads the result of a SQL query directly into a Pandas DataFrame.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q77. Which exception is raised for errors related to the database interface rather than the database itself?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. DatabaseError<br>\nB. InterfaceError<br>\nC. OperationalError<br>\nD. ProgrammingError\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br><code>InterfaceError<\/code> is raised for errors related to the database module interface, not the database engine.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q78. How do you limit the number of rows returned in a MongoDB query?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. limit()<br>\nB. take()<br>\nC. top()<br>\nD. max_rows()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>The <code>limit()<\/code> method on a cursor restricts the number of documents returned.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q79. What is the syntax to update data in a SQLite table using Python?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. table.update()<br>\nB. cursor.execute(&#8220;UPDATE &#8230;&#8221;)<br>\nC. conn.update()<br>\nD. db.modify()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>Standard SQL UPDATE statements are executed via the cursor&#8217;s <code>execute()<\/code> method.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q80. Which Python module provides support for DB-API 2.0?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. All database connectors (sqlite3, psycopg2, etc.)<br>\nB. Only sqlite3<br>\nC. The dbapi module<br>\nD. The sys module\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>Most standard Python database connectors adhere to the DB-API 2.0 specification (PEP 249).<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q81. Which function is used to determine the number of rows modified?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. cursor.rowcount<br>\nB. cursor.rownumber<br>\nC. conn.affected_rows<br>\nD. len(cursor)\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br><code>cursor.rowcount<\/code> returns the number of rows affected by the last execute.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q82. What happens if you try to fetch data after closing the cursor?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Returns empty list<br>\nB. Returns None<br>\nC. Raises ProgrammingError or InterfaceError<br>\nD. Creates a new connection\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>Attempting to operate on a closed cursor raises an error because the resource is no longer available.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q83. In SQLAlchemy, what is &#8216;MetaData&#8217; used for?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Storing user data<br>\nB. Managing database schema information<br>\nC. Handling connections<br>\nD. Query optimization\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>MetaData is a registry that stores information about tables, columns, and schema definitions.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q84. Which method is used to group data in MongoDB aggregation?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. group()<br>\nB. aggregate()<br>\nC. collect()<br>\nD. combine()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>Aggregation operations are performed using the <code>aggregate()<\/code> method on a collection.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q85. Which of the following is a &#8216;NoSQL&#8217; database?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. MySQL<br>\nB. PostgreSQL<br>\nC. MongoDB<br>\nD. SQLite\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> C<br>MongoDB is a document-oriented NoSQL database, while others listed are relational SQL databases.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q86. How do you select specific columns using Pandas read_sql?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Use the columns parameter<br>\nB. Use SQL SELECT statement in the query<br>\nC. Use the filter parameter<br>\nD. Set projection=True\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The SQL query string passed to <code>read_sql<\/code> determines which columns are returned.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q87. What is the function of conn.commit() inside a try block?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. To start a transaction<br>\nB. To save successful changes<br>\nC. To handle errors<br>\nD. To create a backup\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>It commits the transaction if the code executes successfully without raising exceptions.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q88. Which argument is required to connect to a remote MySQL server?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. file path<br>\nB. host<br>\nC. memory<br>\nD. driver\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>host<\/code> argument (IP address or hostname) specifies the location of the remote database server.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q89. What is the correct way to pass multiple parameters to cursor.execute()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. execute(query, param1, param2)<br>\nB. execute(query, (param1, param2))<br>\nC. execute(query, [param1, param2])<br>\nD. execute(query + params)\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>Parameters must be passed as a sequence (tuple or list) in the second argument.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q90. In SQLAlchemy, which method is used to add an object to the session?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. session.insert()<br>\nB. session.add()<br>\nC. session.save()<br>\nD. session.attach()\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>session.add()<\/code> method places an object into the session to be persisted.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q91. Which method efficiently handles bulk inserts in Pandas DataFrames?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. df.to_sql(method=&#8217;multi&#8217;)<br>\nB. df.to_sql(fast=True)<br>\nC. df.to_sql(batch=True)<br>\nD. df.to_sql(quick=True)\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>The <code>method='multi'<\/code> argument passes multiple values in a single INSERT statement.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q92. What is the purpose of the &#8216;factory&#8217; parameter in sqlite3.connect?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. To create connections<br>\nB. To specify a custom Connection class<br>\nC. To create tables<br>\nD. To generate keys\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The factory parameter allows you to provide a subclass of Connection.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q93. How is the &#8216;LIKE&#8217; operator used in a parameterized query in Python?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. LIKE ? with % in the parameter<br>\nB. LIKE %?%<br>\nC. LIKE &#8216;%?%&#8217;<br>\nD. LIKE {param}\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>The wildcard characters <code>%<\/code> should be part of the parameter value, not the query string.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q94. Which exception is raised when a table referenced in a query does not exist?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. TableNotFoundError<br>\nB. OperationalError<br>\nC. SchemaError<br>\nD. LookupError\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>Errors like &#8220;no such table&#8221; usually result in an <code>OperationalError<\/code> in sqlite3.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q95. In PyMongo, what does _id represent in a document?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. A random string<br>\nB. The primary key field<br>\nC. A foreign key<br>\nD. The document size\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The <code>_id<\/code> field serves as the unique identifier (primary key) for documents in a collection.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q96. Which Python feature is commonly used to manage database resources automatically?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Garbage Collection<br>\nB. Context Managers (with statement)<br>\nC. Decorators<br>\nD. Generators\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>Context managers ensure that connections or cursors are closed even if errors occur.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q97. What is the role of the &#8216;backups&#8217; module in Python SQL interaction?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. It is a standard module for database backups<br>\nB. It does not exist in the standard library<br>\nC. It creates database links<br>\nD. It handles encryption\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>There is no standard &#8216;backups&#8217; module; backup logic is usually handled by specific database tools or custom scripts.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q98. Which of the following is true regarding cursor.arraysize?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. Specifies the number of rows to fetch at a time<br>\nB. Specifies the number of columns<br>\nC. Specifies the data type<br>\nD. Specifies the connection speed\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>The <code>arraysize<\/code> attribute suggests the number of rows to fetch with <code>fetchmany()<\/code>.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q99. What command is used to remove a table from an SQLite database?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. REMOVE TABLE<br>\nB. DROP TABLE<br>\nC. DELETE TABLE<br>\nD. TRUNCATE TABLE\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> B<br>The SQL standard <code>DROP TABLE table_name<\/code> command removes the table structure.<\/p>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Q100. Why is it recommended to use &#8216;finally&#8217; block in database operations?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\nA. To execute code regardless of exceptions<br>\nB. To raise errors<br>\nC. To print variables<br>\nD. To start transactions\n<\/p>\n\n\n\n<details><summary><strong>Show Answer<\/strong><\/summary>\n<p><strong>Answer:<\/strong> A<br>The <code>finally<\/code> block ensures resources like database connections are closed even if an error occurs.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations, you made it this far! I hope you went through all 100 Python SQL &amp; Database MCQs and were able to solve most of them. If not, or if some questions are difficult, that\u2019s okay, all you need is a little revision, and you\u2019ll be ready to go.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are completely new to SQL in Python, check out this beginner-friendly guide: <a href=\"https:\/\/www.askpython.com\/python-modules\/sql-in-python\">SQL in Python<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also explore these helpful tutorials to strengthen your understanding: <a href=\"https:\/\/www.askpython.com\/python-modules\/flask\/flask-mysql-database\">Flask + MySQL Database Connection<\/a>, <a href=\"https:\/\/www.askpython.com\/python-modules\/flask\/flask-postgresql\">Flask PostgreSQL with SQLAlchemy<\/a>, <a href=\"https:\/\/www.askpython.com\/python-modules\/python-mysql-tutorial\">Python MySQL Complete Guide<\/a>, <a href=\"https:\/\/www.askpython.com\/python-modules\/django\/django-mysql\">Django MySQL Setup<\/a>, and <a href=\"https:\/\/www.askpython.com\/python-modules\/django\/django-postgresql\">Django PostgreSQL Setup<\/a>. These will give you practical knowledge of working with databases in real-world applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At last, make sure to bookmark this page for later revision. Just press\u00a0<strong>Ctrl + D on Windows<\/strong>\u00a0or\u00a0<strong>Cmd + D on Mac<\/strong>\u00a0so you can easily revisit these Python SQL &amp; Database MCQs anytime you need.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python database programming isn\u2019t just about writing queries, it\u2019s also about understanding the right tools. From sqlite3 to MySQL and PostgreSQL (via mysql-connector-python and psycopg2), and even NoSQL with pymongo, each plays an important role. You also need a basic understanding of SQLAlchemy and pandas. In this article, I\u2019ve combined all these concepts into a [&hellip;]<\/p>\n","protected":false},"author":54,"featured_media":66436,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-65964","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/65964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/54"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=65964"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/65964\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/66436"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=65964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=65964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=65964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}