MCQs on Working with Databases | Ruby

Learn how Ruby interacts with databases such as SQLite, PostgreSQL, and MySQL. Explore using ActiveRecord and Sequel for effective database management, queries, and migrations within Ruby applications.


Introduction to SQLite, PostgreSQL, and MySQL (Questions 1-10)

  1. Which of the following is a relational database management system?
    a) MongoDB
    b) SQLite
    c) Redis
    d) Cassandra
  2. What is the primary advantage of using SQLite in Ruby?
    a) It supports large-scale applications
    b) It is serverless and self-contained
    c) It is highly scalable
    d) It works only with PostgreSQL
  3. Which of the following databases is a commonly used open-source RDBMS in Ruby on Rails applications?
    a) PostgreSQL
    b) MongoDB
    c) MySQL
    d) Both a and c
  4. Which database does the ActiveRecord ORM in Ruby on Rails support out of the box?
    a) SQLite
    b) PostgreSQL
    c) MySQL
    d) All of the above
  5. What does psql refer to in the context of PostgreSQL?
    a) PostgreSQL Client Command Line Tool
    b) A method to manage databases
    c) A GUI tool for database administration
    d) An extension for PostgreSQL
  6. Which of the following commands initializes a new SQLite database file?
    a) sqlite3 create database.db
    b) sqlite3 database.db
    c) create_database sqlite3.db
    d) init_db sqlite3.db
  7. What is the default SQL syntax for creating a table in PostgreSQL?
    a) CREATE TABLE table_name
    b) CREATE DATABASE table_name
    c) BEGIN TABLE table_name
    d) CREATE COLUMN table_name
  8. Which of the following is NOT a feature of MySQL?
    a) Support for ACID properties
    b) Complex joins
    c) Limited foreign key constraints
    d) Schema-less structure
  9. Which of the following statements is true about PostgreSQL?
    a) It is an object-relational database system
    b) It does not support transactions
    c) It is designed for scalability on a small scale
    d) It is primarily NoSQL
  10. Which of these is a common use case for MySQL?
    a) Complex data analytics
    b) Web applications that require fast read access
    c) Big data processing
    d) Document storage

Using ActiveRecord and Sequel (Questions 11-20)

  1. What is ActiveRecord in Ruby on Rails used for?
    a) ORM (Object-Relational Mapping)
    b) Query generation
    c) Migration management
    d) All of the above
  2. Which of the following does Sequel provide that ActiveRecord does NOT?
    a) Database migrations
    b) A more flexible query interface
    c) Built-in validation
    d) Automated scaffolding
  3. In ActiveRecord, what does Model.all return?
    a) A single record from the database
    b) A query object
    c) All records in the database
    d) A list of SQL queries
  4. How do you establish a connection to a PostgreSQL database in Ruby using ActiveRecord?
    a) ActiveRecord::Base.establish_connection('postgres://user:password@localhost/dbname')
    b) ActiveRecord::Base.connect('postgres://user:password@localhost/dbname')
    c) ActiveRecord::Base.open('postgres://user:password@localhost/dbname')
    d) ActiveRecord::Base.connection('postgres://user:password@localhost/dbname')
  5. In Sequel, which of the following method calls establishes a database connection?
    a) DB.connect('postgres://user:password@localhost/dbname')
    b) DB.open('postgres://user:password@localhost/dbname')
    c) DB.establish_connection('postgres://user:password@localhost/dbname')
    d) DB.create_connection('postgres://user:password@localhost/dbname')
  6. What is the purpose of migrations in ActiveRecord?
    a) To manage the database schema changes
    b) To handle data transformation
    c) To execute raw SQL queries
    d) To perform data backups
  7. How does Sequel handle database transactions?
    a) Using DB.transaction block
    b) Using DB.execute_transaction
    c) Using DB.begin_transaction
    d) Using DB.start_transaction
  8. Which of the following ActiveRecord methods is used to insert a new record into a table?
    a) Model.new
    b) Model.create
    c) Model.insert
    d) Model.save
  9. What is the Sequel method for executing a raw SQL query?
    a) DB.raw
    b) DB.execute
    c) DB.run
    d) DB.sql
  10. How do you define associations between models in ActiveRecord?
    a) Using belongs_to, has_many, and has_one
    b) Using has_and_belongs_to
    c) Using connect_to
    d) Using connect

Additional Questions on ActiveRecord and Sequel (Questions 21-30)

  1. How do you fetch records that match a specific condition in ActiveRecord?
    a) Model.where(condition)
    b) Model.select(condition)
    c) Model.find(condition)
    d) Model.query(condition)
  2. In Sequel, how do you fetch a single record matching a condition?
    a) DB[:table].where(condition).first
    b) DB[:table].find(condition)
    c) DB[:table].get(condition)
    d) DB[:table].one(condition)
  3. Which of the following is the correct way to update a record in ActiveRecord?
    a) Model.update(field: value)
    b) Model.update!(field: value)
    c) Model.update_attributes(field: value)
    d) Model.set(field: value)
  4. How do you delete a record from a database using ActiveRecord?
    a) Model.destroy
    b) Model.remove
    c) Model.delete
    d) Model.drop
  5. In Sequel, which method would you use to check if a table exists in the database?
    a) DB.table_exists?(:table_name)
    b) DB[:table_name].exists?
    c) DB.table(:table_name).exists?
    d) DB[:table_name].has?(:exists)
  6. Which of the following methods is used to define a database index in Sequel?
    a) add_index
    b) create_index
    c) DB.create_index
    d) create_table
  7. What does Model.find_by in ActiveRecord do?
    a) Finds the first record matching the conditions
    b) Finds all records matching the conditions
    c) Finds a record by its ID
    d) Creates a record if not found
  8. In Sequel, how do you set the primary key for a table?
    a) primary_key :id
    b) DB.create_table(:table_name, primary_key: :id)
    c) set_primary_key(:id)
    d) table(:table_name).primary_key(:id)
  9. Which method in ActiveRecord is used to define a has_many relationship?
    a) has_many :model
    b) belongs_to :model
    c) has_many(:model)
    d) has_many(:model).through
  10. How do you create a migration file in Rails?
    a) rails generate migration create_table_name
    b) rails generate model table_name
    c) rails create migration table_name
    d) rails new migration table_name

Answer Key

QNoAnswer (Option with Text)
1b) SQLite
2b) It is serverless and self-contained
3d) Both a and c
4d) All of the above
5a) PostgreSQL Client Command Line Tool
6b) sqlite3 database.db
7a) CREATE TABLE table_name
8d) Schema-less structure
9a) It is an object-relational database system
10b) Web applications that require fast read access
11d) All of the above
12b) A more flexible query interface
13c) All records in the database
14a) ActiveRecord::Base.establish_connection('postgres://user:password@localhost/dbname')
15a) DB.connect('postgres://user:password@localhost/dbname')
16a) To manage the database schema changes
17a) Using DB.transaction block
18b) Model.create
19b) DB.execute
20a) Using belongs_to, has_many, and has_one
21a) Model.where(condition)
22a) DB[:table].where(condition).first
23c) Model.update_attributes(field: value)
24c) Model.delete
25a) DB.table_exists?(:table_name)
26c) DB.create_index
27a) Finds the first record matching the conditions
28a) primary_key :id
29a) has_many :model
30a) rails generate migration create_table_name

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