CommerceClasses And Objects In Ruby MCQs
Practice Classes And Objects In Ruby MCQs for competitive exams.
Classes And Objects In Ruby MCQs
Practice questions from this topic.
What is the method used to define instance methods in Ruby?
- A. def method_name
- B. def self.method_name
- C. def class.method_name
- D. def method_name.self
Correct Answer: A
What is the method used to define class methods in Ruby?
- A. def self.method_name
- B. def class.method_name
- C. def method_name.self
- D. def method_name.static
Correct Answer: A
What is the method used to check if an object is an instance of a particular class in Ruby?
- A. instance_of?
- B. is_instance?
- C. is_a?
- D. object_of?
Correct Answer: C
What does the super keyword do in Ruby?
- A. Calls the method of the superclass with the same name
- B. Calls the method of the subclass with the same name
- C. Exits the method
- D. Returns a value
Correct Answer: A
What is the method used to inherit from a superclass in Ruby?
- A. <
- B. -<
- C. extends
- D. inherit
Correct Answer: A
What is the purpose of the initialize method in Ruby?
- A. To initialize class variables
- B. To define instance methods
- C. To create object instances
- D. To initialize object properties
Correct Answer: D
What does the attr_accessor method do in Ruby?
- A. Creates getter and setter methods for an instance variable
- B. Creates only getter methods for an instance variable
- C. Creates only setter methods for an instance variable
- D. Creates private methods for an instance variable
Correct Answer: A
Which of the following is used to access instance variables in Ruby?
- A. @variable
- B. $variable
- C. @@variable
- D. variable
Correct Answer: A
How do you define a constructor in Ruby?
- A. def initialize
- B. def constructor
- C. def init
- D. def constructor()
Correct Answer: A
Which keyword is used to create a new instance of a class in Ruby?
- A. new
- B. create
- C. instantiate
- D. instance
Correct Answer: A
What is the keyword used to define a class in Ruby?
- A. class
- B. def class
- C. class def
- D. define_class
Correct Answer: A