MCQs on Metaprogramming in Depth | Ruby

Metaprogramming in Ruby allows you to write code that manipulates code itself. These questions will help you understand defining methods dynamically, introspection, reflection, and designing DSLs in Ruby.


Topic 1: Defining Methods Dynamically

  1. Which Ruby method allows you to define methods dynamically?
    a) define_method()
    b) method()
    c) create_method()
    d) define_dynamic_method()
  2. How do you dynamically create a method in a Ruby class?
    a) class.method(:method_name)
    b) class.define_method(:method_name)
    c) class.add_method(:method_name)
    d) class.create_method(:method_name)
  3. Which of the following is the correct way to define a method dynamically using define_method?
    a) define_method(:new_method) { puts "Hello, world!" }
    b) method(:new_method) { puts "Hello, world!" }
    c) create_method(:new_method) { puts "Hello, world!" }
    d) add_method(:new_method) { puts "Hello, world!" }
  4. When defining methods dynamically, which block can access the receiver of the method?
    a) self
    b) block
    c) yield
    d) super
  5. How do you dynamically define a method with arguments in Ruby?
    a) define_method(:method_name) { |arg| puts arg }
    b) method(:method_name) { |arg| puts arg }
    c) create_method(:method_name) { |arg| puts arg }
    d) define_method(:method_name, arg) { puts arg }
  6. What happens when you try to define a method dynamically using an undefined symbol?
    a) An error is raised
    b) It silently ignores the method
    c) It defaults to a no-op method
    d) It reverts to a default method
  7. How can you call a dynamically defined method in Ruby?
    a) send(:method_name)
    b) call(:method_name)
    c) invoke(:method_name)
    d) run(:method_name)
  8. What is the primary use of dynamically defined methods in Ruby?
    a) To reduce code repetition
    b) To increase performance
    c) To create private methods
    d) To make classes immutable
  9. Which of the following can be done with define_method?
    a) Define private methods only
    b) Define methods with varying arguments
    c) Create new classes dynamically
    d) Define instance variables dynamically
  10. What is the purpose of send when calling a dynamically defined method?
    a) To execute the method as an object message
    b) To call a private method
    c) To bind a method to a specific class
    d) To define the method dynamically

Topic 2: Introspection and Reflection

  1. Which method in Ruby helps you check the class of an object?
    a) object.class
    b) object.inspect
    c) object.type
    d) object.to_class
  2. What method returns the list of instance variables for an object in Ruby?
    a) object.variables
    b) object.instance_variables
    c) object.variables_list
    d) object.instance_vars
  3. Which method is used to retrieve the list of methods defined for an object?
    a) object.methods
    b) object.method_list
    c) object.get_methods
    d) object.methods_defined
  4. What is the purpose of respond_to? in Ruby?
    a) To check if an object responds to a method
    b) To define a method for an object
    c) To find the class of an object
    d) To get the methods available to a class
  5. How do you determine if an object has a specific instance variable?
    a) object.has_instance_variable?(:var_name)
    b) object.has_variable?(:var_name)
    c) object.instance_variable_defined?(:var_name)
    d) object.variable_defined?(:var_name)
  6. Which of the following methods can be used to inspect an object in Ruby?
    a) object.inspect
    b) object.show
    c) object.inspect_method
    d) object.display
  7. What method would you use to get the list of all instance methods of a class?
    a) class.instance_methods
    b) class.methods
    c) class.get_methods
    d) class.methods_list
  8. Which method allows you to invoke a method by its name as a symbol?
    a) call_method(:method_name)
    b) send(:method_name)
    c) invoke(:method_name)
    d) call(:method_name)
  9. Which method is used to check if a method exists for an object in Ruby?
    a) object.method_defined?(:method_name)
    b) object.has_method?(:method_name)
    c) object.respond_to?(:method_name)
    d) object.method_exist?(:method_name)
  10. What is the function of instance_variable_get?
    a) It retrieves the value of an instance variable
    b) It sets the value of an instance variable
    c) It checks if an instance variable exists
    d) It defines a new instance variable

Topic 3: DSL (Domain-Specific Language) Design

  1. What is a Domain-Specific Language (DSL)?
    a) A language designed for general-purpose programming
    b) A language specialized in a specific problem domain
    c) A language that does not need to be compiled
    d) A library used for web development
  2. Which of the following is an advantage of using a DSL in Ruby?
    a) It reduces the amount of boilerplate code
    b) It improves execution speed
    c) It reduces memory usage
    d) It makes the language more secure
  3. In Ruby, what method can be used to implement a block of code within a DSL?
    a) yield
    b) block_call
    c) invoke_block
    d) def_block
  4. Which of the following is the primary goal of a DSL?
    a) To extend the functionality of Ruby
    b) To make the code more readable and expressive
    c) To reduce the need for external libraries
    d) To enhance the performance of Ruby applications
  5. What type of Ruby construct is commonly used in DSLs to make code more expressive?
    a) Blocks
    b) Arrays
    c) Hashes
    d) Classes
  6. How do you create a simple Ruby DSL for configuration purposes?
    a) By defining methods and using blocks
    b) By creating complex classes
    c) By using a gem like rails
    d) By modifying the Ruby syntax
  7. In Ruby DSLs, what does the self keyword typically refer to within a block?
    a) The object that the DSL is being applied to
    b) The Ruby interpreter
    c) The main program
    d) The method being called
  8. What is a key feature of Ruby that makes it ideal for creating DSLs?
    a) Dynamic typing
    b) Static typing
    c) Compilation
    d) Memory management
  9. Which of the following Ruby methods is commonly used to define an internal DSL?
    a) define_method
    b) self.method
    c) create_dsl
    d) set_dsl
  10. How do DSLs improve the usability of a program?
    a) By allowing a developer to write less code
    b) By making the code more readable and specific to a domain
    c) By reducing the program’s memory usage
    d) By improving the performance of algorithms

Answers Table

QnoAnswer (Option with the text)
1a) define_method()
2b) class.define_method(:method_name)
3a) define_method(:new_method) { puts "Hello, world!" }
4a) self
5a) `define_method() {
6a) An error is raised
7a) send(:method_name)
8a) To reduce code repetition
9b) Define methods with varying arguments
10a) To execute the method as an object message
11a) object.class
12b) object.instance_variables
13a) object.methods
14a) To check if an object responds to a method
15c) object.instance_variable_defined?(:var_name)
16a) object.inspect
17a) class.instance_methods
18b) send(:method_name)
19c) object.respond_to?(:method_name)
20a) It retrieves the value of an instance variable
21b) A language specialized in a specific problem domain
22a) It reduces the amount of boilerplate code
23a) yield
24b) To make the code more readable and expressive
25a) Blocks
26a) By defining methods and using blocks
27a) The object that the DSL is being applied to
28a) Dynamic typing
29a) define_method
30b) By making the code more readable and specific to a domain

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