MCQs on Modules and Mixins | Ruby

Explore Modules and Mixins in Ruby with this guide on defining and using modules, including, extending, and mixing modules, and how namespaces help organize and manage code efficiently in Ruby.


1. Defining and Using Modules

  1. What is a module in Ruby used for?
    • A) Defining classes
    • B) Grouping related methods and constants
    • C) Defining variables
    • D) Defining inheritance hierarchies
  2. How do you define a module in Ruby?
    • A) module ModuleName
    • B) class ModuleName
    • C) define ModuleName
    • D) module = ModuleName
  3. Which of the following is a valid way to include a module in a class?
    • A) class MyClass; include MyModule; end
    • B) class MyClass; extend MyModule; end
    • C) class MyClass; mixin MyModule; end
    • D) class MyClass; insert MyModule; end
  4. What does the module keyword in Ruby define?
    • A) A class
    • B) A constant
    • C) A group of methods and constants
    • D) A block of code
  5. Which method can be used to check if a class includes a particular module in Ruby?
    • A) .is_module?
    • B) .module?
    • C) .included_modules
    • D) .include?
  6. Which of the following is NOT a characteristic of modules in Ruby?
    • A) They can be inherited
    • B) They can hold methods
    • C) They can hold constants
    • D) They cannot instantiate objects
  7. How do you define a constant inside a module in Ruby?
    • A) CONSTANT_NAME = value
    • B) module CONSTANT_NAME = value
    • C) constant CONSTANT_NAME = value
    • D) constant value
  8. How do you use a method from a module inside a class in Ruby?
    • A) method_name
    • B) include method_name
    • C) module.method_name
    • D) self.method_name
  9. What is the purpose of the extend keyword when used with modules in Ruby?
    • A) To add methods to an object
    • B) To create a new class
    • C) To add methods to a class itself
    • D) To instantiate a new object
  10. Which of the following is a valid way to define a module method that can be used outside of the module?
    • A) def self.method_name
    • B) def method_name
    • C) def Module.method_name
    • D) def module.method_name

2. Including, Extending, and Mixing Modules

  1. What does the include keyword do when used with modules in Ruby?
    • A) It adds the module’s methods to the class’s instances
    • B) It adds the module’s methods to the class itself
    • C) It creates a subclass of the module
    • D) It replaces the class methods with module methods
  2. What is the difference between include and extend in Ruby modules?
    • A) include adds methods to the class itself, and extend adds methods to instances
    • B) include adds methods to instances, and extend adds methods to the class
    • C) include only works with methods, and extend works with constants
    • D) There is no difference between include and extend
  3. Which of the following would allow a module’s methods to be used as class methods in Ruby?
    • A) include
    • B) extend
    • C) mixin
    • D) prepend
  4. What happens when you use include in a Ruby class?
    • A) The class gets access to the methods of the module as class methods
    • B) The class gets access to the methods of the module as instance methods
    • C) The class creates a subclass of the module
    • D) The class replaces its methods with those of the module
  5. Which of the following is the correct way to mix a module into a class in Ruby?
    • A) class MyClass; include MyModule; end
    • B) module MyClass; extend MyModule; end
    • C) class MyClass; mix MyModule; end
    • D) module MyClass; include MyModule; end
  6. How can you include multiple modules into a Ruby class?
    • A) Use include for each module
    • B) Use extend for each module
    • C) Use mixin for each module
    • D) You can only include one module at a time
  7. What does the prepend keyword do in Ruby?
    • A) It includes a module before other modules or methods in the method lookup chain
    • B) It adds a module’s methods to the class as instance methods
    • C) It adds a module’s methods to the class as class methods
    • D) It creates a subclass of the module
  8. Which method allows you to check if a class has included a module in Ruby?
    • A) .include?
    • B) .included?
    • C) .module?
    • D) .has_module?
  9. What is the result of calling Module.new in Ruby?
    • A) It creates a new instance of a module
    • B) It creates a new class
    • C) It creates a new module
    • D) It defines a new constant
  10. Which of the following allows you to use a module as a class method in Ruby?
    • A) include
    • B) extend
    • C) prepend
    • D) mixin

3. Namespaces

  1. What is the purpose of using namespaces in Ruby?
    • A) To store multiple classes in a single module
    • B) To organize classes and modules to prevent name conflicts
    • C) To define classes and modules inside a class
    • D) To create methods inside a class
  2. How can you define a module inside a namespace in Ruby?
    • A) namespace ModuleName
    • B) module Namespace::ModuleName
    • C) namespace module Namespace.ModuleName
    • D) module Namespace[ModuleName]
  3. What is the result of calling Namespace::ModuleName.method_name in Ruby?
    • A) Accessing the method from the module in the namespace
    • B) Creating a new method in the namespace
    • C) Defining a new namespace inside the module
    • D) Raising an error
  4. How do you use a class inside a module that is part of a namespace in Ruby?
    • A) Namespace::ModuleName::ClassName.new
    • B) Namespace::ClassName.new
    • C) ModuleName::Namespace::ClassName.new
    • D) ModuleName::ClassName
  5. What is the correct way to define a constant inside a module in Ruby?
    • A) CONST_NAME = value
    • B) module CONST_NAME = value
    • C) namespace CONST_NAME = value
    • D) module CONST_NAME::value
  6. How do you access a constant from a module within a namespace in Ruby?
    • A) Namespace::ModuleName::CONSTANT_NAME
    • B) ModuleName::Namespace::CONSTANT_NAME
    • C) Namespace::CONSTANT_NAME
    • D) ModuleName::CONSTANT_NAME
  7. How can you ensure that a method defined in a namespace is not accessible globally?
    • A) Define the method within a module or class in a namespace
    • B) Define the method using private
    • C) Use the include keyword
    • D) Methods are always globally accessible
  8. What is the main benefit of using namespaces in Ruby?
    • A) To prevent name conflicts in large codebases
    • B) To make all methods private by default
    • C) To automatically include modules
    • D) To add method overrides
  9. How can you use a method from a module inside a namespace?
    • A) Namespace::ModuleName.method_name
    • B) Namespace.method_name
    • C) ModuleName.method_name
    • D) ModuleName::Namespace.method_name
  10. Which of the following is a valid namespace declaration in Ruby?
    • A) module Namespace::SubNamespace
    • B) namespace SubNamespace
    • C) namespace Namespace::SubNamespace
    • D) module Namespace[SubNamespace]

Answers

QnoAnswer
1B) Grouping related methods and constants
2A) module ModuleName
3A) class MyClass; include MyModule; end
4C) A group of methods and constants
5C) .included_modules
6A) They can be inherited
7A) CONSTANT_NAME = value
8C) module.method_name
9C) To add methods to a class itself
10A) def self.method_name
11A) It adds the module’s methods to the class’s instances
12B) include adds methods to instances, and extend adds methods to the class
13B) extend
14B) The class gets access to the methods of the module as instance methods
15A) class MyClass; include MyModule; end
16A) Use include for each module
17A) It includes a module before other modules or methods in the method lookup chain
18A) .include?
19C) It creates a new module
20B) extend
21B) To organize classes and modules to prevent name conflicts
22B) module Namespace::ModuleName
23A) Accessing the method from the module in the namespace
24A) Namespace::ModuleName::ClassName.new
25A) CONST_NAME = value
26A) Namespace::ModuleName::CONSTANT_NAME
27A) Define the method within a module or class in a namespace
28A) To prevent name conflicts in large codebases
29A) Namespace::ModuleName.method_name
30A) module Namespace::SubNamespace

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