MCQs on Advanced Object-Oriented Programming | Ruby

Dive deep into advanced Object-Oriented Programming (OOP) concepts in Ruby, including metaprogramming, singleton classes, dynamic method creation, and refinements. These advanced techniques help enhance flexibility and adaptability in Ruby code.


MCQs on Advanced Object-Oriented Programming in Ruby

Section 1: Metaprogramming Basics (6 Questions)

  1. What is metaprogramming in Ruby?
    • a) Writing code to manipulate objects at runtime
    • b) Writing code that generates other code
    • c) Using predefined libraries in Ruby
    • d) Compiling code into machine code
  2. Which Ruby feature allows you to define methods dynamically?
    • a) Singleton methods
    • b) Method missing
    • c) Metaprogramming
    • d) Blocks
  3. In Ruby, which method is used to define a method dynamically at runtime?
    • a) define_method
    • b) method_missing
    • c) new_method
    • d) create_method
  4. How does Ruby handle method calls to undefined methods in a class?
    • a) It throws a NoMethodError
    • b) It creates a dynamic method
    • c) It invokes the method_missing method
    • d) It automatically generates a default method
  5. What is the purpose of define_method in Ruby?
    • a) It defines a class-level method
    • b) It creates a new method during runtime
    • c) It executes a block dynamically
    • d) It modifies the object’s state
  6. What feature of Ruby allows you to define methods for objects on the fly?
    • a) Dynamic typing
    • b) Metaprogramming
    • c) Static typing
    • d) Operator overloading

Section 2: Singleton Classes and Methods (6 Questions)

  1. In Ruby, what is a singleton class?
    • a) A class for a specific object
    • b) A class that cannot be subclassed
    • c) A class that only has one method
    • d) A class for storing instance variables
  2. How do you define a singleton method in Ruby?
    • a) By using def object.method
    • b) By using def object.singleton_method
    • c) By defining a method within a class block
    • d) By using class << object syntax
  3. What is the advantage of using singleton classes in Ruby?
    • a) They allow multiple objects to share the same methods
    • b) They enable defining methods unique to a single object
    • c) They create reusable code for all objects
    • d) They allow for inheritance across all objects
  4. Which of the following syntax creates a singleton method for an object obj?
    • a) def obj.method
    • b) class obj; def method; end; end
    • c) class << obj; def method; end; end
    • d) def singleton.obj.method
  5. What is the result of calling a singleton method on an object that does not have it?
    • a) It throws an error
    • b) It creates the method dynamically
    • c) It invokes the method from a parent class
    • d) It returns nil
  6. What keyword can be used to access the singleton class of an object in Ruby?
    • a) singleton_class
    • b) object_class
    • c) class << object
    • d) unique_class

Section 3: Method Missing and Dynamic Method Creation (6 Questions)

  1. What is the role of the method_missing method in Ruby?
    • a) It generates an error when a method is missing
    • b) It handles calls to undefined methods
    • c) It automatically defines missing methods
    • d) It creates methods dynamically
  2. In Ruby, method_missing can be used to:
    • a) Raise an error when a method is not found
    • b) Handle method calls that don’t exist in the object
    • c) Create a default implementation of a method
    • d) Automatically modify an object’s state
  3. What does the super keyword do inside method_missing?
    • a) It invokes the parent class’s version of the method
    • b) It creates a new method dynamically
    • c) It returns the missing method name
    • d) It calls the default Ruby method for missing methods
  4. When would you typically use method_missing in Ruby?
    • a) To handle method calls on objects dynamically at runtime
    • b) To prevent method calls from affecting an object’s state
    • c) To define fixed methods in the class
    • d) To overload a method
  5. Which Ruby method allows you to dynamically define a method in a class or module?
    • a) define_method
    • b) method_missing
    • c) self.method
    • d) create_method
  6. What is the return value of the method_missing method in Ruby?
    • a) The result of the dynamically created method
    • b) A NoMethodError exception
    • c) The method’s name as a symbol
    • d) nil

Section 4: Refinements (6 Questions)

  1. What is the purpose of refinements in Ruby?
    • a) To modify the behavior of existing classes within a module or class
    • b) To define new classes dynamically
    • c) To add new methods to built-in classes
    • d) To optimize code execution for performance
  2. Which keyword is used to declare a refinement in Ruby?
    • a) refine
    • b) module
    • c) def
    • d) extend
  3. How do refinements affect the scope of changes to classes in Ruby?
    • a) They apply to all instances globally
    • b) They only apply inside a specific scope or module
    • c) They modify classes permanently
    • d) They are used to change method visibility
  4. When is the refine keyword used in Ruby?
    • a) To change a method’s behavior locally within a module
    • b) To redefine a method globally in Ruby
    • c) To create new types of data
    • d) To enhance method accessibility
  5. How can you apply a refinement to a class in Ruby?
    • a) using ModuleName
    • b) include ModuleName
    • c) extend ModuleName
    • d) apply ModuleName
  6. Which of the following is an example of applying refinements in Ruby?
    • a) using MyModule
    • b) extend MyModule
    • c) include MyModule
    • d) class MyModule
  7. What is the effect of refinements in Ruby on the classes outside the scope of the refinement?
    • a) The changes are visible globally
    • b) The changes only affect the scope where using is declared
    • c) The changes make the class immutable
    • d) The changes affect only subclasses
  8. What would happen if you attempt to call a method modified by refinement outside its scope?
    • a) Ruby will throw a NoMethodError
    • b) The original method will be called
    • c) The refined method will still work
    • d) Ruby will redefine the method
  9. Which of the following is true about refinements in Ruby?
    • a) They only modify existing methods within a module or class locally
    • b) They alter Ruby’s core classes permanently
    • c) They allow you to modify methods globally
    • d) They are used to overload operators
  10. What does using keyword do when applied to a refinement module?
    • a) It applies the refinement’s methods to the current scope
    • b) It overrides all existing methods
    • c) It includes the module globally
    • d) It adds methods to classes dynamically
  11. Which of the following is not a feature of refinements in Ruby?
    • a) They modify core classes globally
    • b) They apply only to a specific scope
    • c) They allow method overrides locally
    • d) They don’t affect other parts of the program
  12. How do refinements differ from monkey patching in Ruby?
    • a) Refinements are scoped, whereas monkey patching affects global state
    • b) Refinements modify classes permanently, while monkey patching does not
    • c) Refinements cannot modify existing methods, but monkey patching can
    • d) There is no difference between the two

Answer Key

QnoAnswer
1b) Writing code that generates other code
2c) Metaprogramming
3a) define_method
4c) It invokes the method_missing method
5b) It creates a new method during runtime
6b) Metaprogramming
7a) A class for a specific object
8c) class << obj; def method; end; end
9b) They enable defining methods unique to a single object
10c) class << obj; def method; end; end
11a) It throws an error
12c) class << object
13b) It handles calls to undefined methods
14b) Handle method calls that don’t exist in the object
15a) It invokes the parent class’s version of the method
16b) A NoMethodError exception
17a) define_method
18a) To handle method calls dynamically at runtime
19a) To modify the behavior of existing classes within a module or class
20a) refine
21b) They only apply inside a specific scope or module
22a) To change a method’s behavior locally within a module
23a) using MyModule
24a) using MyModule
25b) The changes only affect the scope where using is declared
26b) The original method will be called
27a) They only modify existing methods within a module or class locally
28a) It applies the refinement’s methods to the current scope
29a) They modify core classes globally
30a) Refinements are scoped, whereas monkey patching affects global state

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