Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Variables and Data Types
What will be the output of this Ruby code?
def greet
  name = "Alice"
  Name = "Bob"
  puts name
end
greet
Aname
BAlice
CBob
DName
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable naming and scope

    In Ruby, local variables start with lowercase letters, constants start with uppercase. 'name' is local variable, 'Name' is a constant.
  2. Step 2: Check which variable is printed

    The code prints 'name', which is "Alice". The constant 'Name' is not used in the puts statement.
  3. Final Answer:

    Alice -> Option B
  4. Quick Check:

    Local variable 'name' prints "Alice" [OK]
Quick Trick: Local variables lowercase, constants uppercase; puts prints local variable [OK]
Common Mistakes:
  • Confusing constants with local variables
  • Assuming uppercase variable prints
  • Ignoring case sensitivity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes