MCQs on Working with Gems and Libraries | Ruby

This chapter focuses on understanding Ruby Gems, installing and using them, and creating your own simple Gem. Master these core concepts to streamline your Ruby programming workflow.


Topic 1: What are Gems?

  1. What is a Ruby gem?
    a) A Ruby framework for building applications
    b) A packaged library or tool to extend Ruby functionality
    c) A method of organizing Ruby code
    d) A type of object in Ruby
  2. Which command is used to list installed gems in Ruby?
    a) gem list
    b) gem show
    c) gem info
    d) gem display
  3. Which file is typically used to list the dependencies of a Ruby gem?
    a) Gemfile
    b) gemspec
    c) gemspecs
    d) Gemfile.lock
  4. What does the gem command allow you to do?
    a) Manage Ruby installations
    b) Install, update, and uninstall gems
    c) Compile Ruby code
    d) Edit Ruby scripts
  5. What is the primary purpose of gems in Ruby?
    a) To help organize code into smaller modules
    b) To add third-party functionality to Ruby applications
    c) To manage Ruby versions
    d) To optimize Ruby performance
  6. What is a .gemspec file used for?
    a) To specify gem dependencies
    b) To configure environment variables
    c) To define gem metadata for a gem
    d) To lock gem versions
  7. Which command would you use to create a new gem?
    a) gem new
    b) gem create
    c) bundle gem
    d) ruby gem create
  8. How can you check the version of a gem installed in your environment?
    a) gem -v
    b) gem version
    c) gem list [gem_name]
    d) gem check
  9. How are gems typically installed in a Ruby project?
    a) By adding the gem’s name to the Gemfile
    b) Using the install command in the terminal
    c) Using gem install [gem_name]
    d) All of the above
  10. Which of the following is an example of a Ruby gem that provides testing capabilities?
    a) rails
    b) rspec
    c) rails
    d) sinatra

Topic 2: Installing and Using Gems

  1. Which command installs a gem from the RubyGems repository?
    a) gem install [gem_name]
    b) gem add [gem_name]
    c) gem get [gem_name]
    d) gem fetch [gem_name]
  2. How do you install gems specified in a Gemfile?
    a) gem install --bundler
    b) bundle install
    c) gem install gemfile
    d) gem install --file Gemfile
  3. What is the purpose of the bundler gem?
    a) It installs Ruby versions
    b) It manages gem dependencies for projects
    c) It helps create new gems
    d) It optimizes gem installations
  4. What does the gem uninstall [gem_name] command do?
    a) Removes the gem and all of its dependencies
    b) Uninstalls the gem from the project
    c) Uninstalls a gem globally
    d) Both b and c
  5. How do you update a gem to its latest version?
    a) gem update [gem_name]
    b) gem install --latest [gem_name]
    c) gem upgrade [gem_name]
    d) gem refresh [gem_name]
  6. How can you install a gem from a local file?
    a) gem install file:[path_to_gem]
    b) gem add file:[path_to_gem]
    c) gem local install [gem_name]
    d) gem install --local
  7. How do you install a specific version of a gem?
    a) gem install [gem_name] --version [version_number]
    b) gem install [gem_name]:[version_number]
    c) gem install [gem_name]@version [version_number]
    d) gem version install [gem_name]
  8. Which of the following is a valid way to require a gem in your Ruby script?
    a) require 'gem_name'
    b) gem 'gem_name'
    c) include 'gem_name'
    d) use 'gem_name'
  9. What does the gem list command do?
    a) Shows all available gems in the system
    b) Lists installed gems and their versions
    c) Displays gem dependencies for the project
    d) Lists gems in the Gemfile
  10. Which command checks if a gem is up-to-date?
    a) gem status
    b) gem check
    c) gem outdated
    d) gem update --check

Topic 3: Creating a Simple Gem

  1. How do you create a basic structure for a new gem?
    a) bundle gem [gem_name]
    b) gem create [gem_name]
    c) gem new [gem_name]
    d) rails gem create [gem_name]
  2. What is the default directory structure generated by the bundle gem command?
    a) lib/, tests/, README.md
    b) lib/, spec/, Gemfile
    c) src/, docs/, config/
    d) src/, tests/, Gemfile
  3. What is the primary purpose of the .gemspec file in a gem?
    a) To specify gem dependencies and metadata
    b) To store Ruby code for the gem
    c) To test the gem
    d) To store the gem’s documentation
  4. Which directory contains the main functionality of the gem?
    a) lib/
    b) bin/
    c) src/
    d) docs/
  5. How do you test the gem in your local environment before publishing it?
    a) gem push
    b) gem build
    c) rake gem:test
    d) gem install .
  6. What is the purpose of the rake command in gem development?
    a) To compile Ruby code
    b) To install gems locally
    c) To automate tasks like testing and building the gem
    d) To list available gems
  7. How do you specify a gem’s version in the .gemspec file?
    a) spec.version = '1.0.0'
    b) version = '1.0.0'
    c) gem.version = '1.0.0'
    d) spec.version('1.0.0')
  8. How do you install a local gem during development?
    a) gem install .
    b) gem install --path .
    c) bundle install .
    d) gem install [gem_name]
  9. How do you push a gem to RubyGems after it’s created?
    a) gem push [gem_name].gem
    b) gem publish [gem_name].gem
    c) bundle push [gem_name].gem
    d) gem upload [gem_name].gem
  10. What is the typical extension of a Ruby gem file?
    a) .gem
    b) .rb
    c) .zip
    d) .tar

Answers Table

QNoAnswer
1b) A packaged library or tool to extend Ruby functionality
2a) gem list
3a) Gemfile
4b) Install, update, and uninstall gems
5b) To add third-party functionality to Ruby applications
6c) To define gem metadata for a gem
7c) bundle gem
8c) gem list [gem_name]
9d) All of the above
10b) rspec
11a) gem install [gem_name]
12b) bundle install
13b) It manages gem dependencies for projects
14d) Both b and c
15a) gem update [gem_name]
16a) gem install file:[path_to_gem]
17a) gem install [gem_name] --version [version_number]
18a) require 'gem_name'
19b) Lists installed gems and their versions
20c) gem outdated
21a) bundle gem [gem_name]
22b) lib/, spec/, Gemfile
23a) To specify gem dependencies and metadata
24a) lib/
25d) gem install .
26c) To automate tasks like testing and building the gem
27a) spec.version = '1.0.0'
28a) gem install .
29a) gem push [gem_name].gem
30a) .gem

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