Bird
0
0

You want to create a class Car with a method start that returns "Engine started". Which code correctly declares this class and method?

hard📝 Application Q15 of 15
Ruby - Classes and Objects
You want to create a class Car with a method start that returns "Engine started". Which code correctly declares this class and method?
A<pre>Car class def start "Engine started" end end</pre>
B<pre>class car def start return Engine started end end</pre>
C<pre>class Car def start "Engine started" end end</pre>
D<pre>class Car def start() puts "Engine started" end end</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Check class declaration and naming

    class Car
      def start
        "Engine started"
      end
    end
    correctly uses class Car with capitalized name and ends with end.
  2. Step 2: Check method definition and return value

    class Car
      def start
        "Engine started"
      end
    end
    defines method start returning the string "Engine started".
    class Car
      def start()
        puts "Engine started"
      end
    end
    uses puts which prints but returns nil, so not exactly the same.
  3. Final Answer:

    class Car def start "Engine started" end end -> Option C
  4. Quick Check:

    Class with capital name and method returning string [OK]
Quick Trick: Class name capitalized; method returns string without puts [OK]
Common Mistakes:
  • Using lowercase class names
  • Using puts instead of return string
  • Incorrect class declaration syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes