Elixir is a powerful language for building concurrent, distributed applications, and integrating with databases is a key aspect of working with Elixir. In this set of 30 multiple-choice questions, we’ll cover topics like Ecto and SQL databases, designing schemas and migrations, querying and filtering data, and working with NoSQL databases such as Redis. These concepts are vital for developing robust applications.
MCQs on Integration with Databases in Elixir
Working with Ecto and SQL Databases
What is Ecto in Elixir?
A) A database engine
B) A package for working with SQL databases
C) A framework for handling HTTP requests
D) A process management tool
Which SQL database is commonly used with Ecto in Elixir?
A) MySQL
B) PostgreSQL
C) SQLite
D) Oracle
What is the primary purpose of Ecto in Elixir applications?
A) Database management and migrations
B) HTTP request handling
C) Concurrency management
D) Message passing between processes
Which command in Ecto is used to generate a new migration file?
A) mix ecto.migrate
B) mix ecto.gen.migration
C) mix ecto.create
D) mix ecto.create_migration
How do you define a connection to a SQL database in Ecto?
A) Using Ecto.Repo
B) Using Ecto.Schema
C) Using Ecto.Migration
D) Using Ecto.Query
Designing Schemas and Migrations
What is the role of schemas in Ecto?
A) To define database structure and associations
B) To create HTTP routes
C) To manage process states
D) To handle concurrent tasks
Which of the following defines the structure of a table in Ecto?
A) Schema
B) Migration
C) Query
D) Repo
Which command is used to apply migrations in Elixir?
A) mix ecto.create
B) mix ecto.rollback
C) mix ecto.migrate
D) mix ecto.alter
What is the change function used for in Ecto migrations?
A) To modify the database schema
B) To query data
C) To create a new schema
D) To connect to the database
How would you define a foreign key relationship between two tables in Ecto?
A) Using belongs_to and has_many associations in schemas
B) By manually adding foreign key constraints in migrations
C) Using add_foreign_key in migrations
D) All of the above
Querying and Filtering with Ecto
How do you retrieve all records from a table using Ecto?
A) Repo.all(MyApp.MyModel)
B) Repo.fetch(MyApp.MyModel)
C) Repo.select(MyApp.MyModel)
D) Repo.find(MyApp.MyModel)
Which Ecto function is used to find a single record by its ID?
A) Repo.get/2
B) Repo.find/2
C) Repo.query/2
D) Repo.select/2
How would you filter records in Ecto based on a column value?
A) from(record in MyApp.MyModel, where: record.column == value)
B) Repo.filter(MyApp.MyModel, column == value)
C) Repo.query("SELECT * FROM table WHERE column = value")
D) MyApp.MyModel.filter(column == value)
Which clause is used in Ecto queries to order records by a column?
A) order_by
B) sort_by
C) order
D) by
How do you limit the number of records returned by a query in Ecto?
A) Repo.limit/2
B) limit(10)
C) Repo.limit(10)
D) from(record in MyApp.MyModel, limit: 10)
Elixir and NoSQL Databases (e.g., Redis)
How can Elixir interact with Redis?
A) Using the Redix library
B) Using the Ecto library
C) Using the Phoenix library
D) Using the Postgrex library
Which Elixir package is commonly used for interacting with Redis?
A) Redix
B) ExRedis
C) Redisex
D) ElixirRedis
How would you connect to a Redis server in Elixir?
A) Redix.start_link("redis://localhost:6379")
B) Redis.connect("localhost:6379")
C) Redis.connect(:redis)
D) Redis.start_link(:local)
How do you set a key-value pair in Redis using Elixir and Redix?
A) Redix.set("key", "value")
B) Redis.set("key", "value")
C) Redix.put("key", "value")
D) Redix.insert("key", "value")
Which of the following operations is supported by Redix for Redis in Elixir?
A) SET
B) GET
C) DEL
D) All of the above
Database Connection and Transactions
Which function is used to start a transaction in Ecto?
A) Repo.transaction/1
B) Repo.start_transaction/1
C) Repo.begin/1
D) Repo.connect/1
What is the purpose of using a transaction in Ecto?
A) To ensure that a set of operations are executed atomically
B) To handle errors in the database
C) To optimize query performance
D) To cache query results
Which of the following is true about Ecto’s transaction handling?
A) It automatically commits changes unless there is an error
B) It allows only read operations
C) It must be manually committed
D) It does not support rollbacks
How can you execute raw SQL queries in Ecto?
A) Repo.query/2
B) Ecto.query/2
C) Repo.sql/2
D) Ecto.execute/2
What is the purpose of using Ecto’s Repo.insert/2 function?
A) To insert a new record into the database
B) To update an existing record
C) To execute raw SQL inserts
D) To add a foreign key constraint
Working with Associations in Ecto
Which Ecto macro is used to define a belongs_to association in a schema?
A) belongs_to :user, MyApp.User
B) has_many :posts, MyApp.Post
C) has_one :profile, MyApp.Profile
D) has_many :comments, MyApp.Comment
How do you preload associated records in Ecto?
A) Using Repo.preload/2
B) Using Repo.fetch/2
C) Using Repo.load/2
D) Using Repo.get/2
How would you define a many-to-many association in Ecto?
A) Using has_many :many_to_many
B) Using many_to_many :tags, MyApp.Tag
C) Using has_many :through
D) Using many_to_many :through, :join_table
What is the purpose of Ecto.Multi in Elixir?
A) To group multiple database operations into a single transaction
B) To define multiple schema relations
C) To handle migrations
D) To manage database connections
How can you delete a record in Ecto?
A) Repo.delete/1
B) Repo.remove/1
C) Repo.destroy/1
D) Repo.remove_record/1
Answer Key
Qno
Answer
1
B) A package for working with SQL databases
2
B) PostgreSQL
3
A) Database management and migrations
4
B) mix ecto.gen.migration
5
A) Using Ecto.Repo
6
A) To define database structure and associations
7
A) Schema
8
C) mix ecto.migrate
9
A) To modify the database schema
10
D) All of the above
11
A) Repo.all(MyApp.MyModel)
12
A) Repo.get/2
13
A) from(record in MyApp.MyModel, where: record.column == value)
14
A) order_by
15
D) from(record in MyApp.MyModel, limit: 10)
16
A) Using the Redix library
17
A) Redix
18
A) Redix.start_link("redis://localhost:6379")
19
A) Redix.set("key", "value")
20
D) All of the above
21
A) Repo.transaction/1
22
A) To ensure that a set of operations are executed atomically
23
A) It automatically commits changes unless there is an error
24
A) Repo.query/2
25
A) To insert a new record into the database
26
A) belongs_to :user, MyApp.User
27
A) Using Repo.preload/2
28
B) Using many_to_many :tags, MyApp.Tag
29
A) To group multiple database operations into a single transaction