ComputerAdvanced Topics In Ruby MCQs
Practice Advanced Topics In Ruby MCQs for competitive exams.
Advanced Topics In Ruby MCQs
Practice questions from this topic.
What is the purpose of the Kernel#autoload method in Ruby?
- A. It automatically loads a specified file when a constant is accessed for the first time
- B. It loads all files in a directory when the program starts
- C. It reloads a file every time it is accessed
- D. It loads a file asynchronously to improve performance
Correct Answer: A
What does the term "aliasing" refer to in Ruby?
- A. Creating a copy of an existing method or variable
- B. Renaming an existing method or variable
- C. Deleting an existing method or variable
- D. Creating an alternate name for an existing method or variable
Correct Answer: D
What is the purpose of the method_missing method in Ruby's BasicObject class?
- A. It is called when a method is defined on a class object that already exists
- B. It is called when a method is called on a class object that does not exist
- C. It is called when a method is defined on an object that already exists
- D. It is called when a method is called on an object that does not respond to the method
Correct Answer: D
What is the purpose of the Module#attr_reader, Module#attr_writer, and Module#attr_accessor methods in Ruby?
- A. They define getter, setter, and both getter and setter methods for instance variables respectively
- B. They define methods for reading, writing, and both reading and writing instance variables respectively
- C. They define methods for reading instance variables, writing class variables, and both respectively
- D. They define methods for reading, writing, and both reading and writing class variables respectively
Correct Answer: A
What is a "class variable" in Ruby?
- A. A variable that is initialized with the value of a constant
- B. A variable that is shared among all instances of a class
- C. A variable that is dynamically assigned based on the instance's state
- D. None of the above
Correct Answer: B
What does the "splat" operator (*) do when used in method definitions in Ruby?
- A. It allows a method to accept a variable number of arguments as an array
- B. It repeats a block of code a specified number of times
- C. It creates a shallow copy of an array or hash
- D. It performs a bitwise operation on a numerical value
Correct Answer: A
In Ruby, what is the purpose of the Module#prepend method?
- A. It includes a module in the inheritance chain after the class itself
- B. It extends a module to add new functionality
- C. It creates a copy of the module and appends it to the end of the inheritance chain
- D. It includes a module in the inheritance chain before the class itself
Correct Answer: D
What is a "magic comment" in Ruby?
- A. A comment that is automatically removed during code compilation
- B. A comment that automatically generates documentation for the code
- C. A special comment that affects the behavior of the Ruby interpreter
- D. A comment with a mystical or cryptic meaning
Correct Answer: C
What is the purpose of the const_missing method in Ruby?
- A. It is called when a constant is accessed that is already defined
- B. It is called when a constant is accessed that is not defined
- C. It is called when a constant is defined that is not accessed
- D. None of the above
Correct Answer: B
Which Ruby module is commonly used for method delegation?
- A. Delegator
- B. Delegate
- C. Forwardable
- D. Proxy
Correct Answer: C
What does the send method in Ruby do?
- A. It returns a Method object representing the specified method
- B. It checks if the object responds to the specified method
- C. It sends a message to the Ruby interpreter to execute a piece of code
- D. It invokes a method dynamically on an object
Correct Answer: D
What is the primary difference between module_function and extend self in Ruby?
- A. module_function makes the specified methods accessible as module methods, while extend self makes them accessible as instance methods
- B. module_function makes the specified methods private, while extend self makes them public
- C. module_function creates a new module, while extend self extends the current module
- D. module_function makes the specified methods singleton methods, while extend self makes them instance methods
Correct Answer: A