Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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
What is the output of this Kotlin code using logical AND (&&)?
Consider the following Kotlin code snippet. What will it print?
Kotlin
fun main() {
    val a = true
    val b = false
    println(a && b)
}
Atrue
BRuntime exception
CCompilation error
Dfalse
Attempts:
2 left
💡 Hint
Remember that && returns true only if both sides are true.
❓ Predict Output
intermediate
2:00remaining
What does this Kotlin code print using logical OR (||)?
Look at this Kotlin code. What will be printed?
Kotlin
fun main() {
    val x = false
    val y = false
    println(x || y)
}
ARuntime exception
Bfalse
CCompilation error
Dtrue
Attempts:
2 left
💡 Hint
|| returns true if at least one operand is true.
❓ Predict Output
advanced
2:00remaining
What is the output of this Kotlin code using NOT (!) operator?
What will this Kotlin program print?
Kotlin
fun main() {
    val flag = true
    println(!flag)
}
Afalse
Btrue
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
The ! operator reverses the boolean value.
❓ Predict Output
advanced
2:00remaining
What is the output of this Kotlin code combining &&, ||, and !?
Analyze this Kotlin code and determine what it prints.
Kotlin
fun main() {
    val a = true
    val b = false
    val c = true
    println((a && !b) || (b && c))
}
ACompilation error
Bfalse
Ctrue
DRuntime exception
Attempts:
2 left
💡 Hint
Evaluate !b first, then &&, then ||.
🧠 Conceptual
expert
3:00remaining
Which Kotlin expression evaluates to false?
Given these variables: val p = false, val q = true, which expression evaluates to false?
A!p && !q
Bp && !q || q
Cp || q && !p
D!q && p || q
Attempts:
2 left
💡 Hint
Evaluate each expression step by step using operator precedence.

Practice

(1/5)
1. Which logical operator in Kotlin means AND and returns true only if both conditions are true?
easy
A. ||
B. !
C. &&
D. &

Solution

  1. Step 1: Understand the meaning of && operator

    The && operator checks if both conditions are true and returns true only then.
  2. Step 2: Compare with other operators

    The || operator means OR, ! means NOT, and & is a bitwise operator, not logical AND.
  3. Final Answer:

    && -> Option C
  4. Quick Check:

    AND operator = && [OK]
Hint: AND means both true, use && [OK]
Common Mistakes:
  • Confusing && with ||
  • Using & instead of &&
  • Thinking ! means AND
2. Which of the following is the correct syntax to negate a boolean variable isActive in Kotlin?
easy
A. !isActive
B. not isActive
C. isActive!
D. ~isActive

Solution

  1. Step 1: Recall Kotlin negation syntax

    In Kotlin, the NOT operator is written as ! before the variable.
  2. Step 2: Check each option

    !isActive uses !isActive which is correct. not isActive is invalid syntax, C places ! after variable which is wrong, D uses ~ which is bitwise NOT invalid for booleans.
  3. Final Answer:

    !isActive -> Option A
  4. Quick Check:

    Negate with ! before variable [OK]
Hint: Put ! before variable to negate [OK]
Common Mistakes:
  • Putting ! after variable
  • Using 'not' keyword incorrectly
  • Using bitwise ~ for negation
3. What is the output of this Kotlin code?
val a = true
val b = false
println(a && !b || b)
medium
A. false
B. true
C. Compilation error
D. null

Solution

  1. Step 1: Evaluate !b

    b is false, so !b is true.
  2. Step 2: Evaluate a && !b

    a is true, !b is true, so a && !b is true.
  3. Step 3: Evaluate (a && !b) || b

    True || false is true.
  4. Final Answer:

    true -> Option B
  5. Quick Check:

    True AND NOT false OR false = true [OK]
Hint: Evaluate NOT first, then AND, then OR [OK]
Common Mistakes:
  • Ignoring NOT precedence
  • Mixing AND and OR order
  • Assuming false output
4. Find the error in this Kotlin code snippet:
val x = true
val y = false
if x && y {
    println("Both true")
}
medium
A. Missing parentheses around condition in if statement
B. Using && instead of ||
C. Incorrect println syntax
D. Variables x and y not declared

Solution

  1. Step 1: Check if statement syntax

    Kotlin requires parentheses around the condition in if statements.
  2. Step 2: Identify the error

    The code uses if x && y without parentheses, which causes a syntax error.
  3. Final Answer:

    Missing parentheses around condition in if statement -> Option A
  4. Quick Check:

    if condition needs parentheses [OK]
Hint: Always put () around if conditions [OK]
Common Mistakes:
  • Omitting parentheses in if
  • Confusing && with ||
  • Wrong println usage
5. You want to check if a user is logged in and has admin rights before allowing access. Which Kotlin condition correctly uses logical operators for this?
val loggedIn = true
val isAdmin = false
// Choose the correct if condition
hard
A. if (loggedIn || isAdmin) { println("Access granted") }
B. if (!loggedIn || !isAdmin) { println("Access granted") }
C. if (!loggedIn && isAdmin) { println("Access granted") }
D. if (loggedIn && isAdmin) { println("Access granted") }

Solution

  1. Step 1: Understand the requirement

    Access is allowed only if user is logged in AND has admin rights.
  2. Step 2: Analyze each condition

    if (loggedIn && isAdmin) { println("Access granted") } uses && to require both loggedIn and isAdmin true. if (loggedIn || isAdmin) { println("Access granted") } allows access if either is true, which is wrong. The remaining options use negations or incorrect logic, which do not meet requirement.
  3. Final Answer:

    if (loggedIn && isAdmin) { println("Access granted") } -> Option D
  4. Quick Check:

    Use AND to require both conditions true [OK]
Hint: Use && to require both true for access [OK]
Common Mistakes:
  • Using || instead of &&
  • Negating conditions wrongly
  • Allowing access if only one is true
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