Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Variables and Data Types
Identify the error in this Ruby code snippet:
def calculate
  total = 10
  Total = 20
  puts Total + total
end
calculate
ASyntaxError due to variable naming
BRuntime error: undefined variable
CNo error, outputs 30
DNo error, outputs 10
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable types and naming

    'total' is a local variable, 'Total' is a constant. Both are defined before use.
  2. Step 2: Evaluate the expression in puts

    Adding 'Total' (20) and 'total' (10) results in 30. No syntax or runtime errors occur.
  3. Final Answer:

    No error, outputs 30 -> Option C
  4. Quick Check:

    Constant + local variable sums correctly [OK]
Quick Trick: Constants uppercase, locals lowercase; both can be used together [OK]
Common Mistakes:
MISTAKES
  • Assuming case-insensitive variables
  • Expecting syntax error from uppercase variable
  • Confusing constants with local variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes