Challenge - 5 Problems
Logical Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of combined logical operators
What is the output of this Ruby code?
Ruby
a = true b = false puts (a && !b) || (b && !a)
Attempts:
2 left
💡 Hint
Remember that ! reverses the boolean value and && has higher precedence than ||.
✗ Incorrect
a is true, b is false. !b is true, so (a && !b) is true. (b && !a) is false. true || false is true.
❓ Predict Output
intermediate2:00remaining
Result of negation and OR
What will this Ruby code print?
Ruby
x = false puts !x || x
Attempts:
2 left
💡 Hint
Check what !x evaluates to when x is false.
✗ Incorrect
!x is true because x is false. true || false is true.
❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
Remember operator precedence: && before ||, and ! before both.
✗ Incorrect
a && b is false (true && false). !c is false (!true). So false || false && true is false || (false && true) = false || false = false.
❓ Predict Output
advanced2:00remaining
Output of negation with multiple operators
What does this Ruby code print?
Ruby
x = true y = false puts !(x || y) && (x && !y)
Attempts:
2 left
💡 Hint
Evaluate inside parentheses first, then apply negation and AND.
✗ Incorrect
x || y is true, so !(x || y) is false. x && !y is true && true = true. false && true is false.
🧠 Conceptual
expert2:00remaining
Understanding short-circuit evaluation
In Ruby, which statement about the expression `false && (some_method)` is true?
Attempts:
2 left
💡 Hint
Think about how Ruby stops evaluating && when the first value is false.
✗ Incorrect
Ruby uses short-circuit evaluation: if the first value in && is false, it does not evaluate the second.