Home »
MCQs
Peewee MCQs (Multiple-Choice Questions)
Peewee is a lightweight and expressive Python Object-Relational Mapping (ORM) library that provides a simple way to work with relational databases using Python objects. It supports databases such as SQLite, MySQL, MariaDB, and PostgreSQL and provides APIs for defining models, querying data, managing relationships, and handling transactions.
Peewee MCQs
These Peewee MCQs and answers cover important concepts including models, fields, database connections, CRUD operations, queries, relationships, joins, transactions, aggregation, pagination, and other commonly used Peewee features.
List of Peewee MCQs
Practice these Peewee multiple-choice questions to test your knowledge of the Peewee Python ORM and its database-related features.
1. What is Peewee?
- A Python web framework
- A Python Object-Relational Mapping library
- A Python machine learning library
- A Python testing framework
Answer: B) A Python Object-Relational Mapping library
Explanation:
Peewee is a small and expressive Python ORM that provides an interface for interacting with relational databases using Python objects and queries.
2. Which of the following is a database supported by Peewee?
- SQLite
- PostgreSQL
- MySQL
- All of the above
Answer: D) All of the above
Explanation:
Peewee provides database classes and drivers for commonly used relational databases including SQLite, PostgreSQL, MySQL, and MariaDB.
3. Which class is used as the base class for defining a Peewee model?
- Database
- Model
- Table
- Record
Answer: B) Model
Explanation:
Peewee models are normally defined by subclassing Model. The model represents a database table.
4. Which Peewee class is commonly used to connect to an SQLite database?
- SQLiteDatabase
- SqliteDatabase
- SQLiteConnection
- DatabaseSQLite
Answer: B) SqliteDatabase
Explanation:
SqliteDatabase is Peewee's database class for SQLite databases.
5. Which code correctly creates an in-memory SQLite database in Peewee?
SqliteDatabase(':memory:')
SQLite(':memory:')
Database.sqlite(':memory:')
Sqlite(':memory:')
Answer: A) SqliteDatabase(':memory:')
Explanation:
Peewee uses SqliteDatabase(':memory:') to create an SQLite database that exists only in memory.
6. What is the purpose of the Meta class inside a Peewee model?
- To define Python imports
- To configure model metadata such as the database
- To execute SQL queries
- To define application routes
Answer: B) To configure model metadata such as the database
Explanation:
A model's inner Meta class can specify configuration such as which database the model uses, table names, indexes, and other model options.
7. Which Peewee field is commonly used to store text?
- TextField
- StringColumn
- CharColumn
- TextColumn
Answer: A) TextField
Explanation:
TextField is used for storing text values in a Peewee model.
8. Which Peewee field is generally used to store integer values?
- NumberField
- IntegerField
- IntColumn
- NumericField
Answer: B) IntegerField
Explanation:
IntegerField represents integer-valued columns in a Peewee model.
9. What does a ForeignKeyField represent in Peewee?
- A database connection
- A relationship between models
- A Python function
- A transaction
Answer: B) A relationship between models
Explanation:
ForeignKeyField creates a foreign-key relationship between one model and another model.
10. Which parameter can be used to specify a reverse relationship name for a ForeignKeyField?
- reverse
- backref
- reverse_name
- related
Answer: B) backref
Explanation:
The backref parameter specifies the name of the reverse accessor created on the related model.
11. Which method is used to create a single record in a Peewee model?
- insert()
- create()
- add()
- new()
Answer: B) create()
Explanation:
The model's create() method can be used to insert a new record and return the created model instance.
12. Which method is used to create an INSERT query explicitly in Peewee?
- insert()
- add()
- create_query()
- put()
Answer: A) insert()
Explanation:
The insert() method creates an INSERT query. It is useful when inserting data through a query object.
13. Which method is used to save a model instance?
- store()
- commit()
- save()
- write()
Answer: C) save()
Explanation:
The save() method writes a model instance to the database, inserting or updating it depending on the instance state.
14. Which method is used to retrieve records using a SELECT query?
- select()
- read()
- fetch()
- retrieve()
Answer: A) select()
Explanation:
The select() method creates a SELECT query. Peewee SELECT queries are lazy and are executed when their results need to be consumed.
15. Which method is commonly used to add filtering conditions to a Peewee SELECT query?
- filter_by()
- where()
- condition()
- match()
Answer: B) where()
Explanation:
The where() method adds conditions to a Peewee query. Peewee also provides filter() for convenient filtering in appropriate cases.
16. Which expression correctly filters users whose username is "admin"?
User.select().where(User.username == 'admin')
User.select(User.username = 'admin')
User.where(username == 'admin')
User.select().filter(username: 'admin')
Answer: A) User.select().where(User.username == 'admin')
Explanation:
Peewee expressions use model fields with Python comparison operators, such as User.username == 'admin', inside where().
17. What does the get() method generally return?
- All matching rows
- A single matching row
- A database connection
- A table definition
Answer: B) A single matching row
Explanation:
The get() method executes a query and returns one row. If no matching row exists, Peewee raises a DoesNotExist exception.
18. Which method can be used to retrieve the first row of a query if it exists?
- first()
- top()
- head()
- initial()
Answer: A) first()
Explanation:
The first() method executes a query and returns its first row, or no row if the query produces no results.
19. Which method is used to create a DELETE query in Peewee?
- remove()
- delete()
- destroy()
- erase()
Answer: B) delete()
Explanation:
The delete() method creates a DELETE query. A where() condition can be added to limit which rows are deleted.
20. Which method is used to create an UPDATE query in Peewee?
- modify()
- update()
- change()
- edit()
Answer: B) update()
Explanation:
The update() method creates an UPDATE query. It can be combined with where() to restrict the rows affected.
21. What is the purpose of the execute() method on a Peewee query?
- It defines a model
- It executes the query
- It creates a database
- It closes the application
Answer: B) It executes the query
Explanation:
execute() executes a Peewee query and returns the appropriate result depending on the type of query.
22. What does the join() method do in Peewee?
- Creates a database
- Combines related tables in a query
- Deletes related rows
- Creates a model
Answer: B) Combines related tables in a query
Explanation:
The join() method is used to construct SQL JOIN operations between related models.
23. What type of SQL join does Peewee use by default when calling join()?
- INNER JOIN
- LEFT JOIN
- RIGHT JOIN
- FULL OUTER JOIN
Answer: A) INNER JOIN
Explanation:
When no join type is specified, Peewee uses an INNER JOIN.
24. Which method is used to group query results in Peewee?
- group()
- group_by()
- group_results()
- group_rows()
Answer: B) group_by()
Explanation:
The group_by() method adds a GROUP BY clause to a query.
25. Which function can be used to count rows in a Peewee query?
- fn.COUNT()
- fn.TOTAL()
- fn.ROWS()
- fn.NUMBER()
Answer: A) fn.COUNT()
Explanation:
Peewee's fn object can be used to construct SQL functions such as fn.COUNT().
26. Which Peewee function represents the SQL MAX aggregate function?
- fn.MAX()
- fn.LARGEST()
- fn.TOP()
- fn.HIGHEST()
Answer: A) fn.MAX()
Explanation:
fn.MAX() can be used to construct the SQL MAX function in a Peewee expression.
27. Which method is used to sort query results?
- sort_by()
- order_by()
- arrange()
- order()
Answer: B) order_by()
Explanation:
The order_by() method adds ordering to a Peewee query.
28. Which expression orders a Peewee query by username in descending order?
User.username.desc()
User.username.reverse()
User.username.down()
User.username.descending()
Answer: A) User.username.desc()
Explanation:
Peewee fields provide desc() for constructing descending-order expressions.
29. What does the limit() method do?
- Limits the number of rows returned
- Limits the database size
- Limits the number of columns in a model
- Limits database connections
Answer: A) Limits the number of rows returned
Explanation:
The limit() method adds a LIMIT clause to a query to restrict the number of rows returned.
30. Which method can be used with limit() to skip a number of rows?
- skip()
- offset()
- ignore()
- start()
Answer: B) offset()
Explanation:
The offset() method specifies the number of rows to skip before returning query results.
31. Which Peewee method is useful for implementing page-based pagination?
- page()
- paginate_page()
- pages()
- pagination()
Answer: A) page()
Explanation:
Peewee query objects provide paginate() for page-based pagination, using a page number and page size.
32. What is the purpose of a back-reference created by ForeignKeyField?
- It provides access to related records from the referenced model
- It creates a new database
- It deletes the foreign key
- It disables relationships
Answer: A) It provides access to related records from the referenced model
Explanation:
A foreign key with a backref creates a reverse accessor that can be used to query records related to a particular model instance.
33. If a Tweet model has user = ForeignKeyField(User, backref='tweets'), what does user.tweets represent?
- A database connection
- A query for tweets belonging to the user
- A list of database tables
- A new User object
Answer: B) A query for tweets belonging to the user
Explanation:
The tweets back-reference provides a pre-filtered query for Tweet records associated with that User instance.
34. Which Peewee field is used to define a date value?
- DateField
- CalendarField
- DayField
- TimeDateField
Answer: A) DateField
Explanation:
DateField is designed to represent date values in Peewee models.
35. Which Peewee field is appropriate for storing both date and time information?
- DateTimeField
- DateTimeValue
- TimestampFieldOnly
- CalendarDateField
Answer: A) DateTimeField
Explanation:
DateTimeField represents date and time values in a Peewee model.
36. Which Peewee field is designed for Boolean values?
- BooleanField
- BoolColumn
- TrueFalseField
- LogicalField
Answer: A) BooleanField
Explanation:
BooleanField is used to represent Boolean values such as True and False.
37. What does the null=True option generally allow for a Peewee field?
- The field can contain a SQL NULL value
- The field must always contain zero
- The field becomes a primary key
- The field is automatically indexed
Answer: A) The field can contain a SQL NULL value
Explanation:
Setting null=True allows the corresponding database column to contain SQL NULL values.
38. What does the unique=True option specify for a Peewee field?
- Values in the field must be unique
- The field must be nullable
- The field must contain integers
- The field becomes a foreign key
Answer: A) Values in the field must be unique
Explanation:
The unique=True option specifies a uniqueness constraint for the field.
39. Which method can be used to create database tables from Peewee models?
- create_tables()
- make_tables()
- generate_tables()
- build_tables()
Answer: A) create_tables()
Explanation:
A Peewee database object provides create_tables() to create tables for specified models.
40. Which method can be used to drop tables represented by Peewee models?
- remove_tables()
- drop_tables()
- delete_tables()
- destroy_tables()
Answer: B) drop_tables()
Explanation:
The database object's drop_tables() method can be used to remove specified model tables from the database.
41. What is the purpose of database.atomic() in Peewee?
- To execute operations within an atomic transaction
- To create a model automatically
- To generate SQL documentation
- To close a database connection
Answer: A) To execute operations within an atomic transaction
Explanation:
database.atomic() provides a transaction context in which multiple database operations can be treated as an atomic unit.
42. Which syntax correctly uses a Peewee atomic transaction?
with database.atomic():
with atomic.database():
database.with_atomic()
atomic(database):
Answer: A) with database.atomic():
Explanation:
Peewee supports transaction management using the context-manager syntax with database.atomic():.
43. What happens when an exception occurs inside an atomic transaction?
- The transaction can be rolled back
- All database tables are deleted
- The database is permanently closed
- The model is automatically recreated
Answer: A) The transaction can be rolled back
Explanation:
Peewee's atomic transaction context provides transaction handling so that an exception can cause the current transaction to be rolled back.
44. Which Peewee object is used to construct SQL functions such as COUNT and MAX?
- sql
- fn
- functions
- aggregate
Answer: B) fn
Explanation:
Peewee's fn object is used to construct SQL function expressions such as fn.COUNT(), fn.MAX(), and other database functions.
45. What is the purpose of distinct() in a Peewee query?
- To return distinct query results
- To delete duplicate records
- To create a unique database
- To sort records alphabetically
Answer: A) To return distinct query results
Explanation:
The distinct() method adds DISTINCT behavior to a SELECT query so duplicate result rows can be eliminated according to the query.
46. What is the purpose of a Window expression in Peewee?
- To perform calculations over related sets of rows
- To open a desktop window
- To create a database connection
- To display query results in a GUI
Answer: A) To perform calculations over related sets of rows
Explanation:
Peewee supports SQL window functions, which can perform calculations across a set or window of rows while retaining individual result rows.
47. What does a Peewee Select query generally represent?
- A database connection
- A SQL SELECT operation
- A database schema file
- A Python package
Answer: B) A SQL SELECT operation
Explanation:
A Peewee SELECT query represents a database SELECT operation and can be composed using methods such as where(), join(), order_by(), and limit().
48. What does it mean that a Peewee SELECT query is lazy?
- The query is not sent to the database until its results are needed
- The query can never be executed
- The query automatically runs in the background
- The query only works with SQLite
Answer: A) The query is not sent to the database until its results are needed
Explanation:
Peewee SELECT queries are generally lazy. Database execution occurs when the result is iterated, indexed, sliced, or otherwise consumed in a way that requires execution.
49. Which command-line utility can Peewee use to generate model definitions from an existing database?
- pwiz
- peewee-gen
- modelizer
- db2model
Answer: A) pwiz
Explanation:
Peewee provides the pwiz model generator for generating Peewee model definitions from an existing database.
50. Which statement best describes Peewee?
- It is only a database server
- It is a Python ORM with a compact query-building API
- It is a Python operating system
- It is a JavaScript database framework
Answer: B) It is a Python ORM with a compact query-building API
Explanation:
Peewee is a small and expressive Python ORM that provides model definitions, fields, relationships, SQL query construction, database operations, and transaction support.
Advertisement
Advertisement