Bird
0
0

You want to create a Ruby class Student that stores a name and grade. Which initialize method correctly sets these attributes?

hard📝 Application Q8 of 15
Ruby - Classes and Objects
You want to create a Ruby class Student that stores a name and grade. Which initialize method correctly sets these attributes?
Adef initialize(name) name = @name grade = @grade end
Bdef initialize(name, grade) @name = name @grade = grade end
Cdef initialize @name = name @grade = grade end
Ddef initialize(name, grade) name = name grade = grade end
Step-by-Step Solution
Solution:
  1. Step 1: Check parameter usage and assignments

    def initialize(name, grade) @name = name @grade = grade end correctly takes two parameters and assigns them to instance variables with @.
  2. Step 2: Identify incorrect assignments in other options

    Options A, C, and D either assign backwards or do not use instance variables properly.
  3. Final Answer:

    def initialize(name, grade) @name = name @grade = grade end -> Option B
  4. Quick Check:

    Use @var = param to store values in initialize [OK]
Quick Trick: Assign parameters to @variables inside initialize [OK]
Common Mistakes:
  • Assigning param to param
  • Assigning instance var to param
  • Missing parameters in initialize

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes