Bird
0
0

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?

hard📝 Application Q15 of 15
Kotlin - Operators and Expressions
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
Aif (loggedIn || isAdmin) { println("Access granted") }
Bif (!loggedIn || !isAdmin) { println("Access granted") }
Cif (!loggedIn && isAdmin) { println("Access granted") }
Dif (loggedIn && isAdmin) { println("Access granted") }
Step-by-Step Solution
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]
Quick Trick: Use && to require both true for access [OK]
Common Mistakes:
MISTAKES
  • Using || instead of &&
  • Negating conditions wrongly
  • Allowing access if only one is true

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes