Bird
0
0

Which statement best describes the behavior of Ruby class variables (@@) when accessed from different subclasses?

easy📝 Conceptual Q2 of 15
Ruby - Class Methods and Variables
Which statement best describes the behavior of Ruby class variables (@@) when accessed from different subclasses?
AEach subclass has its own independent copy of the class variable.
BClass variables are shared and reflect the last assigned value from any subclass.
CClass variables cannot be accessed from subclasses.
DClass variables are copied to subclasses only when explicitly duplicated.
Step-by-Step Solution
Solution:
  1. Step 1: Recall class variable sharing

    Ruby class variables are shared among a class and all its subclasses, not copied independently.
  2. Step 2: Understand value assignment impact

    When any subclass changes the class variable, the change is visible to all classes in the hierarchy.
  3. Final Answer:

    Class variables are shared and reflect the last assigned value from any subclass. -> Option B
  4. Quick Check:

    Shared class variable value = last assignment visible [OK]
Quick Trick: Class variables reflect last change across subclasses [OK]
Common Mistakes:
  • Assuming subclasses have separate class variable copies
  • Believing class variables are private to classes
  • Confusing class variables with instance variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes