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.
Which method is used to define a class method 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
Which keyword is used to define a class in Ruby?
- A. class MyClass
- B. class
- C. def class
- D. define class
Correct Answer: B
What is the method used to access instance variables in Ruby?
- A. Using accessor methods
- B. Using instance methods
- C. Using class methods
- D. Using global variables
Correct Answer: B
What is the method used to create a new instance of a class in Ruby?
- A. new
- B. create
- C. initialize
- D. instance
Correct Answer: A
What is the method used to define a class method 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
How do you access class variables in Ruby?
- A. Using accessor methods
- B. Using instance methods
- C. Using class methods
- D. Using global variables
Correct Answer: C
What is the syntax for defining a class in Ruby?
- A. class MyClass
- B. class
- C. def class
- D. define class
Correct Answer: B
Which keyword is used to call a method on an object in Ruby?
- A. call
- B. invoke
- C. execute
- D. none of the above
Correct Answer: D
What is the purpose of the attr_reader method in Ruby classes?
- A. To create only getter methods for instance variables
- B. To create only setter methods for instance variables
- C. To create both getter and setter methods for instance variables
- D. To create instance methods
Correct Answer: A
Which method is used to define getter and setter methods for instance variables in Ruby?
- A. attr_accessor
- B. get_set
- C. setter_getter
- D. set_get
Correct Answer: A
What is the syntax for defining an instance method in Ruby?
- A. def method_name
- B. def self.method_name
- C. def method_name.self
- D. def method_name.static
Correct Answer: A
What is the purpose of the attr_accessor method in Ruby classes?
- A. To create getter and setter methods for instance variables
- B. To define class methods
- C. To define instance methods
- D. To create new instances
Correct Answer: A