Bird
0
0

What is the output of the following Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Arrays
What is the output of the following Ruby code?
arr = [1, 2, 3, 4]
puts arr.length
A4
B3
C[1, 2, 3, 4]
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand what arr.length returns

    The length method returns the number of elements in the array, which is 4 here.
  2. Step 2: Confirm output of puts

    puts prints the number 4 to the screen.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    length returns count = 4 [OK]
Quick Trick: length returns total elements count [OK]
Common Mistakes:
MISTAKES
  • Confusing length with last element
  • Expecting array output instead of count
  • Thinking length returns index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes