Bird
0
0

Find the mistake in this Ruby code:

medium📝 Debug Q7 of 15
Ruby - Arrays
Find the mistake in this Ruby code:
arr = [10, 20, 30]
puts arr.first(2)
Aarr.first(2) returns an array, puts expects string
Barr.first(2) is invalid syntax
Carr.first(2) returns nil
DNo mistake, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Understand arr.first(2) return type

    arr.first(2) returns an array of first two elements: [10, 20].
  2. Step 2: Check puts behavior with arrays

    puts prints each element of the array on a new line without error.
  3. Final Answer:

    No mistake, code runs fine -> Option D
  4. Quick Check:

    puts can print arrays element-wise [OK]
Quick Trick: puts prints arrays element-wise without error [OK]
Common Mistakes:
MISTAKES
  • Thinking puts needs string only
  • Believing arr.first(2) is invalid
  • Expecting nil return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes