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)
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
Which Ruby feature allows you to define methods dynamically?
a) Singleton methods
b) Method missing
c) Metaprogramming
d) Blocks
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
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
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
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)
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
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
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
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
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
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)
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
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
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
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
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
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)
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
Which keyword is used to declare a refinement in Ruby?
a) refine
b) module
c) def
d) extend
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
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
How can you apply a refinement to a class in Ruby?
a) using ModuleName
b) include ModuleName
c) extend ModuleName
d) apply ModuleName
Which of the following is an example of applying refinements in Ruby?
a) using MyModule
b) extend MyModule
c) include MyModule
d) class MyModule
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
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
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
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
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
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
Qno
Answer
1
b) Writing code that generates other code
2
c) Metaprogramming
3
a) define_method
4
c) It invokes the method_missing method
5
b) It creates a new method during runtime
6
b) Metaprogramming
7
a) A class for a specific object
8
c) class << obj; def method; end; end
9
b) They enable defining methods unique to a single object
10
c) class << obj; def method; end; end
11
a) It throws an error
12
c) class << object
13
b) It handles calls to undefined methods
14
b) Handle method calls that don’t exist in the object
15
a) It invokes the parent class’s version of the method
16
b) A NoMethodError exception
17
a) define_method
18
a) To handle method calls dynamically at runtime
19
a) To modify the behavior of existing classes within a module or class
20
a) refine
21
b) They only apply inside a specific scope or module
22
a) To change a method’s behavior locally within a module
23
a) using MyModule
24
a) using MyModule
25
b) The changes only affect the scope where using is declared
26
b) The original method will be called
27
a) They only modify existing methods within a module or class locally
28
a) It applies the refinement’s methods to the current scope
29
a) They modify core classes globally
30
a) Refinements are scoped, whereas monkey patching affects global state