0
0
Rubyprogramming~20 mins

Local variables and naming conventions in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Local Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A15
B10
C5
DError: undefined local variable
Attempts:
2 left
💡 Hint
Look at how the variable 'number' is updated before printing.
🧠 Conceptual
intermediate
1:30remaining
Valid local variable names in Ruby
Which of the following is a valid local variable name in Ruby?
A_tempValue
B2variable
CVariableName
D$globalVar
Attempts:
2 left
💡 Hint
Local variables must start with a lowercase letter or underscore.
🔧 Debug
advanced
2:00remaining
Identify the error in local variable usage
What error does this Ruby code produce?
Ruby
def test
  puts value
  value = 5
end
test
ANo error, prints 5
BTypeError: nil can't be coerced into Integer
CSyntaxError: unexpected local variable assignment
DNameError: undefined local variable or method `value' for main:Object
Attempts:
2 left
💡 Hint
Consider when local variables are initialized and when they are used.
📝 Syntax
advanced
1:00remaining
Correct local variable assignment syntax
Which option correctly assigns a local variable in Ruby?
AlocalVar := 10
BlocalVar => 10
ClocalVar = 10
DlocalVar == 10
Attempts:
2 left
💡 Hint
Assignment uses a single equal sign.
🚀 Application
expert
2: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
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Count all variables assigned inside the method, including inside conditionals.