Bird
Raised Fist0
Kotlinprogramming~20 mins

Boolean type and logical operators in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Boolean Logic Mastery
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 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)
}
Atrue
Bfalse
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Remember that !b means 'not b', and && has higher precedence than ||.
❓ Predict Output
intermediate
2: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)
}
Atrue
Bfalse
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
&& has higher precedence than || in Kotlin.
🔧 Debug
advanced
2: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)
}
ANo error, prints false
BTypeError
CUnresolved reference error
DSyntaxError
Attempts:
2 left
💡 Hint
Look carefully at the expression assigned to c.
❓ Predict Output
advanced
2: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)
}
ACompilation error
Bfalse
Ctrue
DRuntime exception
Attempts:
2 left
💡 Hint
Evaluate the equality first, then apply negation and logical operators.
🧠 Conceptual
expert
3: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)
}
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Remember that && has higher precedence than || in Kotlin filter conditions.

Practice

(1/5)
1. What type of value does the Kotlin Boolean type store?
easy
A. Decimal numbers
B. True or false values
C. Text strings
D. Numbers only

Solution

  1. Step 1: Understand the Boolean type

    The Boolean type in Kotlin is designed to hold only two possible values: true or false.
  2. Step 2: Identify the correct value type

    Since Boolean stores true or false, it cannot store numbers or text strings.
  3. Final Answer:

    True or false values -> Option B
  4. Quick Check:

    Boolean = true/false [OK]
Hint: Boolean means true or false only [OK]
Common Mistakes:
  • Thinking Boolean stores numbers
  • Confusing Boolean with String
  • Assuming Boolean can hold multiple values
2. Which of the following is the correct syntax for the logical AND operator in Kotlin?
easy
A. &&
B. ||
C. and
D. !

Solution

  1. Step 1: Recall Kotlin logical operators

    Kotlin uses && for logical AND, || for OR, and ! for NOT.
  2. Step 2: Match operator to AND

    The operator && combines two Boolean expressions and returns true only if both are true.
  3. Final Answer:

    && -> Option A
  4. Quick Check:

    AND operator = && [OK]
Hint: AND is double ampersand && in Kotlin [OK]
Common Mistakes:
  • Using || instead of && for AND
  • Writing 'and' as a keyword
  • Confusing NOT (!) with AND
3. What is the output of the following Kotlin code?
val a = true
val b = false
println(a || b && !b)
medium
A. false
B. null
C. true
D. Compilation error

Solution

  1. Step 1: Evaluate NOT operator

    !b means !false, which is true.
  2. Step 2: Evaluate AND operator

    b && !b means false && true, which is false.
  3. Step 3: Evaluate OR operator

    a || (result of AND) means true || false, which is true.
  4. Final Answer:

    true -> Option C
  5. Quick Check:

    Logical operators follow precedence: !, &&, then || [OK]
Hint: Remember operator precedence: ! > && > || [OK]
Common Mistakes:
  • Ignoring operator precedence
  • Evaluating OR before AND
  • Confusing !b value
4. Identify the error in this Kotlin code snippet:
val x = true
val y = false
val result = x & y
println(result)
medium
A. Using single & instead of && for logical AND
B. Missing semicolon after println
C. Boolean variables cannot be assigned like this
D. No error, code runs fine

Solution

  1. Step 1: Check operator usage

    In Kotlin, logical AND uses &&, not single &. Single & is bitwise AND, not for Booleans.
  2. Step 2: Understand error cause

    Using & with Booleans causes a type mismatch error.
  3. Final Answer:

    Using single & instead of && for logical AND -> Option A
  4. Quick Check:

    Logical AND = &&, not & [OK]
Hint: Use && for Boolean AND, not single & [OK]
Common Mistakes:
  • Using & instead of && for logical AND
  • Thinking semicolon is required
  • Assuming Boolean assignment is invalid
5. Given the Kotlin variables:
val isSunny = false
val hasUmbrella = true
val isWarm = true

Which expression correctly checks if it is either sunny or warm, and you have an umbrella if not sunny?
hard
A. isSunny && isWarm || hasUmbrella
B. (isSunny || isWarm) && hasUmbrella
C. (isSunny && hasUmbrella) || isWarm
D. isSunny || (isWarm && hasUmbrella)

Solution

  1. Step 1: Understand the condition

    We want true if it is sunny OR if it is warm AND you have an umbrella when it is not sunny.
  2. Step 2: Translate condition to logic

    The expression should be: isSunny OR (isWarm AND hasUmbrella). This means if sunny, no umbrella needed; if not sunny, then warm and umbrella required.
  3. Final Answer:

    isSunny || (isWarm && hasUmbrella) -> Option D
  4. Quick Check:

    Use parentheses to group AND inside OR [OK]
Hint: Group AND inside OR with parentheses for correct logic [OK]
Common Mistakes:
  • Ignoring parentheses changing logic
  • Assuming umbrella needed when sunny
  • Mixing AND and OR without grouping
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard