MCQs on Integration with Databases | Elixir

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

  1. 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
  2. Which SQL database is commonly used with Ecto in Elixir?
    • A) MySQL
    • B) PostgreSQL
    • C) SQLite
    • D) Oracle
  3. 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
  4. 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
  5. 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

  1. 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
  2. Which of the following defines the structure of a table in Ecto?
    • A) Schema
    • B) Migration
    • C) Query
    • D) Repo
  3. 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
  4. 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
  5. 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

  1. 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)
  2. 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
  3. 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)
  4. Which clause is used in Ecto queries to order records by a column?
    • A) order_by
    • B) sort_by
    • C) order
    • D) by
  5. 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)

  1. 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
  2. Which Elixir package is commonly used for interacting with Redis?
    • A) Redix
    • B) ExRedis
    • C) Redisex
    • D) ElixirRedis
  3. 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)
  4. 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")
  5. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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

QnoAnswer
1B) A package for working with SQL databases
2B) PostgreSQL
3A) Database management and migrations
4B) mix ecto.gen.migration
5A) Using Ecto.Repo
6A) To define database structure and associations
7A) Schema
8C) mix ecto.migrate
9A) To modify the database schema
10D) All of the above
11A) Repo.all(MyApp.MyModel)
12A) Repo.get/2
13A) from(record in MyApp.MyModel, where: record.column == value)
14A) order_by
15D) from(record in MyApp.MyModel, limit: 10)
16A) Using the Redix library
17A) Redix
18A) Redix.start_link("redis://localhost:6379")
19A) Redix.set("key", "value")
20D) All of the above
21A) Repo.transaction/1
22A) To ensure that a set of operations are executed atomically
23A) It automatically commits changes unless there is an error
24A) Repo.query/2
25A) To insert a new record into the database
26A) belongs_to :user, MyApp.User
27A) Using Repo.preload/2
28B) Using many_to_many :tags, MyApp.Tag
29A) To group multiple database operations into a single transaction
30A) Repo.delete/1

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top