Bird
0
0

Which of the following is the correct syntax to define a class method named display inside a Ruby class Car?

easy📝 Syntax Q3 of 15
Ruby - Class Methods and Variables
Which of the following is the correct syntax to define a class method named display inside a Ruby class Car?
Adef display; end
Bdef @display; end
Cdef Car.display; end
Ddef self.display; end
Step-by-Step Solution
Solution:
  1. Step 1: Recall class method definition syntax

    Class methods inside a class are defined by prefixing the method name with self..
  2. Step 2: Check each option

    def self.display; end uses def self.display; end, which is the correct syntax for a class method.
  3. Final Answer:

    def self.display; end -> Option D
  4. Quick Check:

    Class method syntax = def self.method_name [OK]
Quick Trick: Use def self.method_name to define class methods [OK]
Common Mistakes:
  • Omitting self. prefix for class methods
  • Using instance method syntax for class methods
  • Trying to define class methods with instance variable syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes