0
0
Rubyprogramming~20 mins

Logical operators (&&, ||, !) in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logical Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of combined logical operators
What is the output of this Ruby code?
Ruby
a = true
b = false
puts (a && !b) || (b && !a)
Atrue
BSyntaxError
Cnil
Dfalse
Attempts:
2 left
💡 Hint
Remember that ! reverses the boolean value and && has higher precedence than ||.
Predict Output
intermediate
2:00remaining
Result of negation and OR
What will this Ruby code print?
Ruby
x = false
puts !x || x
Anil
Bfalse
Ctrue
DRuntimeError
Attempts:
2 left
💡 Hint
Check what !x evaluates to when x is false.
Predict Output
advanced
2:00remaining
Complex logical expression output
What is the output of this Ruby code snippet?
Ruby
a = true
b = false
c = true
puts a && b || !c && a
Atrue
Bfalse
Cnil
DSyntaxError
Attempts:
2 left
💡 Hint
Remember operator precedence: && before ||, and ! before both.
Predict Output
advanced
2:00remaining
Output of negation with multiple operators
What does this Ruby code print?
Ruby
x = true
y = false
puts !(x || y) && (x && !y)
Afalse
Btrue
CTypeError
Dnil
Attempts:
2 left
💡 Hint
Evaluate inside parentheses first, then apply negation and AND.
🧠 Conceptual
expert
2:00remaining
Understanding short-circuit evaluation
In Ruby, which statement about the expression `false && (some_method)` is true?
Asome_method is called only if it returns false
Bsome_method is called only if it returns true
Csome_method is always called because && evaluates both sides
Dsome_method is never called because false && anything is false
Attempts:
2 left
💡 Hint
Think about how Ruby stops evaluating && when the first value is false.