Challenge - 5 Problems
Local Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of local variable assignment and usage
What is the output of this Ruby code snippet?
Ruby
def example number = 10 number = number + 5 puts number end example
Attempts:
2 left
💡 Hint
Look at how the variable 'number' is updated before printing.
✗ Incorrect
The variable 'number' starts at 10, then 5 is added, so the output is 15.
🧠 Conceptual
intermediate1:30remaining
Valid local variable names in Ruby
Which of the following is a valid local variable name in Ruby?
Attempts:
2 left
💡 Hint
Local variables must start with a lowercase letter or underscore.
✗ Incorrect
In Ruby, local variables must start with a lowercase letter or underscore. '2variable' starts with a digit, 'VariableName' starts with uppercase (which is a constant), and '$globalVar' is a global variable.
🔧 Debug
advanced2:00remaining
Identify the error in local variable usage
What error does this Ruby code produce?
Ruby
def test puts value value = 5 end test
Attempts:
2 left
💡 Hint
Consider when local variables are initialized and when they are used.
✗ Incorrect
The variable 'value' is used before it is assigned, causing a NameError.
📝 Syntax
advanced1:00remaining
Correct local variable assignment syntax
Which option correctly assigns a local variable in Ruby?
Attempts:
2 left
💡 Hint
Assignment uses a single equal sign.
✗ Incorrect
In Ruby, local variables are assigned using a single equal sign '='.
🚀 Application
expert2:30remaining
Determine the number of local variables after execution
After running this Ruby method, how many local variables exist inside it?
Ruby
def count_vars a = 1 b = 2 if a < b c = a + b end d = c * 2 return d end count_vars
Attempts:
2 left
💡 Hint
Count all variables assigned inside the method, including inside conditionals.
✗ Incorrect
Variables a, b, c, and d are all assigned inside the method, so there are 4 local variables.