Bird
0
0

Which of the following is the correct way to assign an instance variable inside a method?

easy📝 Syntax Q3 of 15
Ruby - Classes and Objects
Which of the following is the correct way to assign an instance variable inside a method?
Aname = "Alice"
B@name = "Alice"
C$name = "Alice"
Dname@ = "Alice"
Step-by-Step Solution
Solution:
  1. Step 1: Identify instance variable assignment syntax

    Instance variables start with '@' and are assigned like normal variables.
  2. Step 2: Check options

    Only @name = "Alice" uses '@' correctly before the variable name.
  3. Final Answer:

    @name = "Alice" -> Option B
  4. Quick Check:

    Instance variable assignment = @name = value [OK]
Quick Trick: Assign instance variables with @ before the name [OK]
Common Mistakes:
  • Omitting @ and creating local variables
  • Using $ which is for global variables
  • Placing @ after the variable name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes