0
0
Rubyprogramming~20 mins

Boolean values (true, false, nil) in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Boolean Mastery in Ruby
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?
Consider the following Ruby code snippet. What will it print when run?
Ruby
value = nil
if value
  puts "true branch"
else
  puts "false branch"
end
Atrue branch
BSyntaxError
Cfalse branch
Dnil
Attempts:
2 left
💡 Hint
Remember that in Ruby, only false and nil are treated as false in conditionals.
Predict Output
intermediate
2:00remaining
What does this expression return?
What is the value of the variable result after running this Ruby code?
Ruby
result = (false || nil) && true
Anil
Bfalse
Ctrue
DSyntaxError
Attempts:
2 left
💡 Hint
Recall how Ruby evaluates && and || with false and nil.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
This Ruby code raises an error. What is the cause?
Ruby
def check_value(val)
  if val == true || val == false || val == nil
    puts "Valid boolean or nil"
  else
    puts "Invalid value"
  end
end

check_value(nil)
ANoMethodError because nil has no == method
BSyntaxError due to incorrect use of || operator
CTypeError because nil cannot be compared with ==
DNo error; code runs and prints "Valid boolean or nil"
Attempts:
2 left
💡 Hint
Check how Ruby compares values with == and how nil behaves.
Predict Output
advanced
2:00remaining
What is the output of this Ruby code involving nil and boolean?
What does this Ruby code print?
Ruby
value = false
puts value || "default"
Adefault
Bnil
Cfalse
Dtrue
Attempts:
2 left
💡 Hint
Remember how || returns the first truthy value.
🧠 Conceptual
expert
2:00remaining
Which Ruby value is treated as false in conditionals?
In Ruby, which of the following values are treated as false in conditional expressions?
AOnly false
BBoth false and nil
Cfalse, nil, and 0
DOnly nil
Attempts:
2 left
💡 Hint
Think about what Ruby considers falsey.