Courseiva

CCNA Database Access Questions

34 questions · Database Access · All types, answers revealed

1
MCQeasy

You are using sqlite3 in Python and need to ensure that data integrity is maintained when performing multiple related INSERT operations. Which method should you call on the connection object to commit your changes?

A.connection.save()
B.connection.flush()
C.cursor.apply()
D.connection.commit()
AnswerD

commit() is the standard method to finalize a transaction in sqlite3.

Why this answer

The commit() method is used to save the current transaction to the database.

2
MCQeasy

Which Python library is the standard built-in interface for SQLite databases?

A.pymongo
B.sqlite3
C.sqlalchemy
D.db-api
AnswerB

sqlite3 is the standard library module.

Why this answer

The 'sqlite3' module is part of the Python standard library.

3
MCQmedium

How do you retrieve the number of documents matched by a PyMongo 'find' operation without fetching all documents into memory?

A.cursor.count()
B.collection.size()
C.len(list(cursor))
D.collection.count_documents(filter)
AnswerD

count_documents is the modern, non-deprecated method.

Why this answer

The count_documents() method on a collection is the recommended way to get a count.

4
MCQeasy

Which of the following best describes the benefit of using parameterized queries in sqlite3?

A.They allow for multiple database connections.
B.They improve query execution speed by pre-compiling.
C.They prevent SQL injection attacks.
D.They automatically format the output as JSON.
AnswerC

Parameterized queries ensure input is treated as data, not code.

Why this answer

Parameterized queries prevent SQL injection by separating the query logic from the data.

5
MCQeasy

In sqlite3, how do you handle a database error such as a constraint violation?

A.Check the connection status after every query.
B.Check the return value of the execute() method.
C.The library crashes the program automatically.
D.Use a try-except block to catch sqlite3.Error.
AnswerD

sqlite3 raises specific exceptions for database errors.

Why this answer

You should wrap database operations in a try-except block and catch sqlite3.Error or its subclasses.

6
Multi-Selectmedium

Which TWO of these are valid parameters for the MongoClient constructor?

Select 2 answers
A.table_prefix
B.host
C.use_sql
D.cache_size
E.username
AnswersB, E

The host parameter is valid.

Why this answer

MongoClient accepts host and port, or a connection string.

7
MCQmedium

What is the purpose of the 'upsert=True' option in MongoDB update operations?

A.It creates a new document if no match is found.
B.It prevents duplicates.
C.It sorts the results before updating.
D.It forces an atomic update across a cluster.
AnswerA

Upsert is a combination of update and insert.

Why this answer

If no document matches the query, a new document is created with the update criteria.

8
MCQhard

You are implementing a custom type in SQLAlchemy. Which method must be overridden to convert data from the database type back to a Python object?

A.load_dialect_impl
B.process_result_value
C.bind_processor
D.process_bind_param
AnswerB

This converts DB results into Python objects.

Why this answer

The process_result_value method is responsible for type conversion during retrieval.

9
MCQmedium

Which object in SQLAlchemy is responsible for maintaining a collection of loaded objects and managing their lifecycle?

A.Engine
B.Session
C.Query
D.MetaData
AnswerB

The session tracks object states like 'persistent' and 'dirty'.

Why this answer

The Session object acts as a workspace for objects and tracks their state.

10
MCQhard

You are using SQLAlchemy with an asynchronous driver (asyncio). Which object must be used to perform database operations asynchronously?

A.Engine.async
B.AsyncSession
C.None of the above
D.AsyncConnection
E.Session
AnswerB

AsyncSession supports awaitable methods for database access.

Why this answer

AsyncSession is the required class for asynchronous session management in SQLAlchemy.

11
Multi-Selecthard

Which THREE of these are valid relationship loading strategies in SQLAlchemy?

Select 3 answers
A.eager
B.subquery
C.joined
D.direct
E.lazy
AnswersB, C, E

Subquery loading uses a separate query with a subselect.

Why this answer

lazy, joined, and subquery are three common loading strategies.

12
MCQmedium

In SQLAlchemy, what does the 'metadata.create_all(engine)' command do?

A.It creates tables for all models bound to the metadata.
B.It connects to the database and tests the latency.
C.It drops all tables and recreates them.
D.It populates the tables with initial data.
AnswerA

This is the primary function of create_all.

Why this answer

It inspects the defined ORM models and emits CREATE TABLE statements to the database.

13
MCQeasy

When using the sqlite3 module, what is the primary purpose of using a context manager (the 'with' statement) on a connection object?

A.To automatically commit or rollback transactions.
B.To lock the database file for exclusive access.
C.To automatically close the connection upon exit.
D.To parse SQL queries for syntax errors.
AnswerA

The 'with' statement handles transaction control automatically.

Why this answer

In sqlite3, the connection context manager automatically commits or rolls back transactions.

14
Multi-Selecteasy

Which TWO are common pitfalls when working with SQLite databases in a multi-threaded environment?

Select 2 answers
A.Sharing a single connection object across threads
B.Excessive database locking (database is locked)
C.Memory leaks in the cursor object
D.SQL injection via parameterized queries
E.Incorrect SQL syntax
AnswersA, B

Standard sqlite3 connections are not thread-safe.

Why this answer

SQLite connections are not thread-safe by default, and lock contention can occur.

15
Multi-Selecthard

Which THREE are valid cascade options in SQLAlchemy?

Select 3 answers
A.save-update
B.merge
C.select
D.delete
E.join
AnswersA, B, D

Standard cascade.

Why this answer

delete, save-update, and merge are common cascade operations.

16
Multi-Selectmedium

Which THREE operations are supported by the PyMongo 'bulk_write' method?

Select 3 answers
A.FindOne
B.DeleteOne
C.UpdateOne
D.InsertOne
E.AggregateOne
AnswersB, C, D

DeleteOne is a valid bulk write operation.

Why this answer

bulk_write handles InsertOne, UpdateOne, and DeleteOne operations.

17
Multi-Selecteasy

Which THREE methods are part of the PEP 249 Python DB-API 2.0 interface?

Select 3 answers
A.get_json
B.commit
C.execute
D.filter_by
E.fetchone
AnswersB, C, E

Standard method on connection.

Why this answer

execute, fetchone, and commit are standard parts of the DB-API specification.

18
Multi-Selecthard

Which THREE features are provided by SQLAlchemy's Unit of Work pattern?

Select 3 answers
A.Direct translation of HTTP requests to SQL
B.Identity mapping to ensure object uniqueness
C.Tracking object changes (dirty checking)
D.Flushing changes to the DB in optimal order
E.Automatic creation of tables
AnswersB, C, D

Identity maps keep object uniqueness per session.

Why this answer

The Unit of Work pattern manages changes to objects and ensures they are flushed to the DB in the correct order.

19
MCQhard

In SQLAlchemy, how do you prevent an object from being saved to the database during a session.commit()?

A.session.delete(obj)
B.session.detach(obj)
C.session.rollback()
D.session.expunge(obj)
AnswerD

expunge removes the object from the session's persistence management.

Why this answer

You can use session.expunge(obj) to remove the object from the session's management.

20
Multi-Selectmedium

Which THREE are characteristics of BSON (Binary JSON) as used by MongoDB?

Select 3 answers
A.It is optimized for efficient traversal
B.It is deprecated in newer MongoDB versions
C.It is exactly the same as JSON
D.It supports more data types than JSON
E.It is binary-encoded
AnswersA, D, E

The binary structure allows for fast parsing.

Why this answer

BSON is binary-encoded, supports more types than JSON, and is optimized for traversal.

21
Multi-Selecteasy

Which TWO are common methods to handle BSON Date objects in PyMongo?

Select 2 answers
A.Using bson.datetime.Datetime
B.Using standard Python datetime objects
C.Using the 'timestamp' helper method
D.Using ISODate strings
E.Using the bson.codec_options for custom handling
AnswersB, E

PyMongo handles datetime natively.

Why this answer

datetime.datetime objects are automatically converted to BSON Dates.

22
MCQmedium

Which PyMongo method should be used to update a single document if you want to modify specific fields without replacing the entire document?

A.update_one
B.upsert
C.save
D.replace_one
E.update_many
AnswerA

update_one uses operators like $set to modify fields.

Why this answer

The update_one method with the $set operator allows updating specific fields.

23
MCQhard

In a PyMongo application, what happens when you use 'insert_many' with 'ordered=False'?

A.The driver ignores all errors.
B.Documents are inserted in parallel.
C.The driver attempts to insert all documents regardless of individual failures.
D.The operation stops at the first error.
AnswerC

Unordered inserts continue processing even if one document fails.

Why this answer

If 'ordered' is False, the driver continues to insert subsequent documents even if one fails.

24
Multi-Selectmedium

Which TWO of the following are valid ways to execute a raw SQL query in SQLAlchemy?

Select 2 answers
A.Using engine.execute('SELECT * FROM table')
B.Using session.query(RawSQL('SELECT * FROM table'))
C.Using session.execute(text('SELECT * FROM table'))
D.Using session.commit('SELECT * FROM table')
E.Using table.run('SELECT * FROM table')
AnswersA, C

Engine execute is a common way for raw SQL execution.

Why this answer

SQLAlchemy allows executing raw SQL via text() with engine.execute() or session.execute().

25
MCQmedium

When connecting to a MongoDB instance using PyMongo, what is the role of the 'MongoClient' object?

A.It serves as the main connection handle to the MongoDB cluster.
B.It stores individual documents in memory.
C.It defines the schema for BSON documents.
D.It translates SQL queries into MQL.
AnswerA

MongoClient manages the connection pool and authentication.

Why this answer

The MongoClient acts as the entry point to the MongoDB server, allowing access to databases and collections.

26
MCQhard

When configuring a connection pool in SQLAlchemy, what does the 'pool_size' parameter define?

A.The number of retries before failure.
B.The timeout duration for queries.
C.Number of persistent connections kept in the pool.
D.Maximum number of simultaneous sessions.
AnswerC

pool_size controls the connection pool capacity.

Why this answer

pool_size defines the number of persistent connections to keep open in the pool.

27
MCQmedium

In SQLAlchemy, what is the difference between 'lazy='select'' and 'lazy='joined'' loading strategies?

A.joined loading causes N+1 query problems.
B.select loading executes a separate SQL statement when the attribute is first accessed.
C.joined loading is always faster.
D.select loading is the default for all relationships.
AnswerB

Lazy loading (select) emits a new query on access.

Why this answer

joined loading performs an outer join to fetch related objects in the same query, whereas select loading fetches them only when accessed.

28
MCQmedium

You are developing an application using SQLAlchemy and need to retrieve a single object by its primary key. Which method is preferred for this operation?

A.Model.find(pk)
B.session.get(Model, pk)
C.session.fetch(Model, pk)
D.session.query(Model).filter(Model.id == pk).first()
AnswerB

session.get() is designed specifically for primary key lookups.

Why this answer

The get() method on the Session object is the optimized way to retrieve an object by its primary key.

29
MCQhard

When using the MongoDB aggregation framework in PyMongo, which stage should be used to filter documents based on a condition?

A.$group
B.$project
C.$match
D.$filter
AnswerC

$match is the standard filtering stage.

Why this answer

The $match stage is used to filter documents in an aggregation pipeline.

30
MCQmedium

When performing bulk inserts in PyMongo, which method is the most efficient?

A.save
B.Looping insert_one
C.bulk_write
D.insert_many
AnswerD

insert_many is optimized for bulk operations.

Why this answer

insert_many is designed to send multiple documents in a single batch to the server.

31
MCQhard

You are using SQLAlchemy's relationship() function. What does 'back_populates' achieve?

A.It improves query performance.
B.It synchronizes the two sides of the relationship in Python memory.
C.It automatically cascades deletes.
D.It forces a database level foreign key constraint.
AnswerB

It enables bidirectional synchronization in the ORM.

Why this answer

It links two sides of a relationship so that changes in one are reflected in the other via the session's identity map.

32
MCQeasy

Which command-line tool is typically used to inspect the contents of a SQLite database file?

A.mysql
B.mongo
C.sqlite3
D.psql
AnswerC

sqlite3 provides the CLI tool for interaction.

Why this answer

The 'sqlite3' command-line interface is the standard tool to interact with .db files.

33
MCQmedium

When using SQLAlchemy Declarative Base, how do you define a table name that differs from the class name?

A.Set __tablename__ = 'name'
B.Set __table_name__ = 'name'
C.Map it in the metadata object.
D.Use the 'table' argument in the class definition.
AnswerA

This is the required attribute for table mapping.

Why this answer

The __tablename__ attribute is used to explicitly map a class to a specific table name.

34
MCQeasy

In SQL, which clause is used to filter results based on a condition after grouping?

A.HAVING
B.WHERE
C.ORDER BY
D.GROUP BY
AnswerA

HAVING is for post-group filtering.

Why this answer

The HAVING clause filters aggregated data, whereas WHERE filters raw data.

Ready to test yourself?

Try a timed practice session using only Database Access questions.