0
0
Rubyprogramming~20 mins

Instance variables (@) in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Instance Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using instance variables?
Consider the following Ruby class and code. What will be printed when this code runs?
Ruby
class Dog
  def initialize(name)
    @name = name
  end

  def speak
    "Woof! My name is #{@name}."
  end
end

fido = Dog.new("Fido")
puts fido.speak
AWoof! My name is @name.
BWoof! My name is .
CError: undefined method `speak' for nil:NilClass
DWoof! My name is Fido.
Attempts:
2 left
💡 Hint
Instance variables start with @ and belong to the object instance.
Predict Output
intermediate
2:00remaining
What is the value of @count after running this Ruby code?
Look at this Ruby class that uses an instance variable @count. What will be the value of @count for the object after these calls?
Ruby
class Counter
  def initialize
    @count = 0
  end

  def increment
    @count += 1
  end

  def current
    @count
  end
end

c = Counter.new
c.increment
c.increment
c.current
A2
B1
C0
Dnil
Attempts:
2 left
💡 Hint
Each call to increment adds 1 to @count.
🔧 Debug
advanced
2:00remaining
Why does this Ruby code not print the name when accessing @name?
This Ruby code tries to print @name but does not print the name. Why? What is the output?
Ruby
class Person
  def initialize(name)
    name = name
  end

  def greet
    "Hello, my name is #{@name}."
  end
end

p = Person.new("Alice")
puts p.greet
AOutput: Hello, my name is . (nil interpolated as empty string)
BOutput: Hello, my name is Alice.
COutput: Hello, my name is @name.
DOutput: Hello, my name is . (empty string)
Attempts:
2 left
💡 Hint
Check how the instance variable @name is assigned in initialize.
🧠 Conceptual
advanced
2:00remaining
What happens to instance variables when you create multiple objects?
If you create two objects from the same Ruby class, each with different values for an instance variable @value, what will happen when you access @value on each object?
AInstance variables are global, so @value is shared across all objects and classes.
BRuby raises an error if two objects have instance variables with the same name.
CEach object has its own separate @value, so they can hold different values independently.
DBoth objects share the same @value, so changing one changes the other.
Attempts:
2 left
💡 Hint
Think about how instance variables belong to each object instance.
Predict Output
expert
2:00remaining
What is the output of this Ruby code using instance variables and inheritance?
Analyze this Ruby code with inheritance and instance variables. What will be printed?
Ruby
class Animal
  def initialize(name)
    @name = name
  end

  def speak
    "I am an animal named #{@name}."
  end
end

class Cat < Animal
  def speak
    super + " Meow!"
  end
end

kitty = Cat.new("Whiskers")
puts kitty.speak
AI am a cat named Whiskers. Meow!
BI am an animal named Whiskers. Meow!
CI am an animal named . Meow!
DError: undefined method `speak' for Cat
Attempts:
2 left
💡 Hint
The Cat class calls super to use the Animal's speak method and adds its own text.