0
0
Rubyprogramming~20 mins

Reject for inverse filtering in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reject Mastery Badge
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 code using reject?
Consider the following Ruby code that uses reject to filter an array. What will be printed?
Ruby
numbers = [1, 2, 3, 4, 5]
result = numbers.reject { |n| n.even? }
puts result.inspect
A[2, 4]
B[1, 3, 5]
C[1, 2, 3, 4, 5]
D[]
Attempts:
2 left
💡 Hint
Remember, reject removes elements for which the block returns true.
Predict Output
intermediate
2:00remaining
What does this code output when using reject with a hash?
Given this Ruby code using reject on a hash, what is the output?
Ruby
scores = { alice: 90, bob: 75, carol: 85 }
filtered = scores.reject { |name, score| score < 80 }
puts filtered.inspect
A{:bob=>75}
B{}
C{:alice=>90, :bob=>75, :carol=>85}
D{:alice=>90, :carol=>85}
Attempts:
2 left
💡 Hint
Reject removes pairs where the block returns true.
🔧 Debug
advanced
2:00remaining
Why does this reject code raise an error?
This Ruby code raises an error. What is the cause?
Ruby
arr = [1, 2, 3]
result = arr.reject do |x|
  x > 1
end
puts result.inspect
ANo error; code runs and outputs [1]
BSyntaxError due to missing 'end' keyword
CTypeError because reject expects a symbol argument
DRuntimeError because block returns nil
Attempts:
2 left
💡 Hint
Check if the block syntax is correct and what reject expects.
Predict Output
advanced
2:00remaining
What is the output of reject with a complex condition?
What does this Ruby code print?
Ruby
words = ['apple', 'banana', 'cherry', 'date']
result = words.reject { |w| w.length <= 5 }
puts result.inspect
A["banana", "cherry"]
B["apple", "date"]
C["apple", "banana", "cherry", "date"]
D[]
Attempts:
2 left
💡 Hint
Reject removes words with length less than or equal to 5.
Predict Output
expert
3:00remaining
What is the output of this nested reject code?
Analyze this Ruby code with nested reject calls. What is printed?
Ruby
data = { a: [1, 2, 3], b: [4, 5, 6], c: [7, 8, 9] }
filtered = data.reject do |key, arr|
  arr.reject { |x| x.even? }.empty?
end
puts filtered.inspect
A}]9 ,8 ,7[>=c: ,]6 ,5 ,4[>=b: ,]3 ,2 ,1[>=a:{
B:a=>[1, 2, 3], :b=>[4, 5, 6], :c=>[7, 8, 9]}
C{:a=>[1, 2, 3], :b=>[4, 5, 6], :c=>[7, 8, 9]}
D{}
Attempts:
2 left
💡 Hint
Check what the inner reject returns and when the outer reject removes a key.