0
0
Rubyprogramming~20 mins

Why arrays are fundamental in Ruby - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby array code?

Consider the following Ruby code that manipulates arrays. What will it print?

Ruby
arr = [1, 2, 3, 4]
arr << 5
arr.pop
puts arr.length
A3
B5
C6
D4
Attempts:
2 left
💡 Hint

Remember that << adds an element and pop removes the last element.

🧠 Conceptual
intermediate
1:30remaining
Why are arrays important in Ruby?

Which of the following best explains why arrays are fundamental in Ruby?

AThey are immutable and cannot be changed after creation.
BThey are used only for mathematical calculations.
CThey allow storing multiple values in an ordered list accessible by index.
DThey replace all other data types in Ruby.
Attempts:
2 left
💡 Hint

Think about how you keep a list of things in order.

🔧 Debug
advanced
2:00remaining
What error does this Ruby array code raise?

Look at this Ruby code snippet. What error will it raise when run?

Ruby
arr = [10, 20, 30]
puts arr[5].to_s.length
ANo error, prints 0
BNoMethodError
CIndexError
DTypeError
Attempts:
2 left
💡 Hint

What does Ruby return when you access an array index that does not exist?

Predict Output
advanced
2:30remaining
What is the output of this Ruby array transformation?

What will this Ruby code print?

Ruby
arr = [1, 2, 3, 4]
new_arr = arr.map { |x| x * 2 if x.even? }
puts new_arr.compact.sum
A12
B20
C10
D14
Attempts:
2 left
💡 Hint

Remember that map keeps the same size and compact removes nil values.

🚀 Application
expert
3:00remaining
How many items are in the resulting array after this Ruby code?

Given this Ruby code, how many items does result contain?

Ruby
arr = [1, 2, 3, 4, 5]
result = arr.select.with_index { |val, idx| val > idx }
A3
B5
C2
D4
Attempts:
2 left
💡 Hint

Check each element: is the value greater than its index?