0
0
Rubyprogramming~20 mins

Predicate methods (ending with ?) in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Predicate Master
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 a predicate method?
Consider the following Ruby code snippet. What will it print?
Ruby
def even_or_odd?(num)
  num.even?
end

puts even_or_odd?(7)
A7
Btrue
Cfalse
Dnil
Attempts:
2 left
💡 Hint
Remember, the method checks if the number is even.
Predict Output
intermediate
2:00remaining
What does this predicate method return?
What will be the output of this Ruby code?
Ruby
class String
  def has_vowels?
    !!(self =~ /[aeiou]/i)
  end
end

puts "sky".has_vowels?
Atrue
Bfalse
Cnil
D0
Attempts:
2 left
💡 Hint
Check if the string contains any vowels.
🔧 Debug
advanced
2:00remaining
Why does this predicate method raise an error?
This Ruby code raises an error. What is the cause?
Ruby
def positive?(num)
  num > 0 ? true : false
end

puts positive?(nil)
ANoMethodError because nil can't be compared with >
BSyntaxError due to missing colon in ternary
CReturns false because nil is not positive
DReturns true because nil is treated as zero
Attempts:
2 left
💡 Hint
Think about what happens when you compare nil with a number.
🧠 Conceptual
advanced
1:30remaining
What is the purpose of predicate methods ending with '?' in Ruby?
Why do Ruby methods often end with a question mark '?'? Choose the best explanation.
AThey indicate the method modifies the object
BThey are used only for methods that ask user input
CThey are private methods by convention
DThey always return a boolean value indicating true or false
Attempts:
2 left
💡 Hint
Think about what the '?' symbol suggests in English.
Predict Output
expert
2:30remaining
What is the output of this complex predicate method?
Analyze the code and determine what it prints.
Ruby
class Array
  def all_even?
    all?(&:even?)
  end
end

arr = [2, 4, 6, 7]
puts arr.all_even?
Afalse
BNoMethodError
Cnil
Dtrue
Attempts:
2 left
💡 Hint
Check if every element in the array is even.