Challenge - 5 Problems
Boolean Logic Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin code using logical operators?
Consider the following Kotlin code snippet. What will it print?
Kotlin
fun main() { val a = true val b = false val c = a && !b || b println(c) }
Attempts:
2 left
💡 Hint
Remember that !b means 'not b', and && has higher precedence than ||.
✗ Incorrect
The expression evaluates as (a && (!b)) || b. Since a is true and b is false, !b is true. So true && true is true, and true || false is true.
❓ Predict Output
intermediate2:00remaining
What does this Kotlin code print with mixed logical operators?
Analyze the output of this Kotlin program:
Kotlin
fun main() { val x = false val y = true val result = x || y && false println(result) }
Attempts:
2 left
💡 Hint
&& has higher precedence than || in Kotlin.
✗ Incorrect
The expression is x || (y && false). y && false is false, so x || false is false || false, which is false.
🔧 Debug
advanced2:00remaining
What error does this Kotlin code raise?
Examine this Kotlin snippet and identify the error it produces when compiled or run:
Kotlin
fun main() { val a = true val b = false val c = a && b || println(c) }
Attempts:
2 left
💡 Hint
Look carefully at the expression assigned to c.
✗ Incorrect
The expression ends abruptly with '||' without a right-hand operand, causing a syntax error.
❓ Predict Output
advanced2:00remaining
What is the output of this Kotlin code using logical negation and equality?
What will this Kotlin program print?
Kotlin
fun main() { val a = true val b = false val c = !(a == b) && (a || b) println(c) }
Attempts:
2 left
💡 Hint
Evaluate the equality first, then apply negation and logical operators.
✗ Incorrect
a == b is false, so !(false) is true. a || b is true. true && true is true.
🧠 Conceptual
expert3:00remaining
How many items are in the resulting list after filtering with logical operators?
Given this Kotlin code, how many elements does the list 'filtered' contain?
Kotlin
fun main() { val numbers = listOf(1, 2, 3, 4, 5, 6) val filtered = numbers.filter { it % 2 == 0 && it > 3 || it == 1 } println(filtered.size) }
Attempts:
2 left
💡 Hint
Remember that && has higher precedence than || in Kotlin filter conditions.
✗ Incorrect
The filter condition is (it % 2 == 0 && it > 3) || it == 1. Even numbers greater than 3 are 4 and 6, plus 1. So filtered list is [1,4,6], size 3.