Bird
0
0

Which of the following is the correct way to define a class instance variable in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Class Methods and Variables
Which of the following is the correct way to define a class instance variable in Ruby?
Adef @count; @count; end
B@count = 0
C@@count = 0
Dself.@count = 0
Step-by-Step Solution
Solution:
  1. Step 1: Identify class instance variable syntax

    Class instance variables start with a single @ and are defined inside the class but outside methods.
  2. Step 2: Check each option

    @count = 0 uses @count = 0 correctly inside the class body. @@count = 0 uses class variables (@@), which are different. def @count; @count; end is invalid method syntax. self.@count = 0 is invalid syntax.
  3. Final Answer:

    @count = 0 -> Option B
  4. Quick Check:

    Class instance variable = single @ [OK]
Quick Trick: Class instance variables use single @ inside class body [OK]
Common Mistakes:
  • Using @@ instead of @ for class instance variables
  • Trying to use self.@ which is invalid
  • Defining variables inside methods without self

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes