Bird
0
0

Find the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Variables and Data Types
Find the error in this Ruby code snippet:
num = 7.5
puts num.to_i.to_f
ASyntaxError due to method chaining
BNo error, output is 7.0
CRuntimeError because to_i cannot be chained
DOutput is 7.5, no conversion happened
Step-by-Step Solution
Solution:
  1. Step 1: Understand method chaining on numbers

    Ruby allows chaining methods like to_i and to_f on numbers without error.
  2. Step 2: Evaluate the code output

    num.to_i converts 7.5 to 7 (integer), then to_f converts 7 to 7.0 (float). So output is 7.0.
  3. Final Answer:

    No error, output is 7.0 -> Option B
  4. Quick Check:

    Method chaining works, output 7.0 [OK]
Quick Trick: to_i then to_f converts float to integer then float again. [OK]
Common Mistakes:
  • Thinking method chaining causes syntax error
  • Expecting original float output
  • Confusing to_i and to_f effects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes