Bird
0
0

Which of the following is the correct way to negate a boolean variable flag in Kotlin?

easy📝 Syntax Q3 of 15
Kotlin - Operators and Expressions
Which of the following is the correct way to negate a boolean variable flag in Kotlin?
Aval result = !flag
Bval result = not flag
Cval result = flag.not
Dval result = ~flag
Step-by-Step Solution
Solution:
  1. Step 1: Recall the negation operator in Kotlin

    The logical NOT operator is ! placed before the boolean variable.
  2. Step 2: Evaluate each option

    val result = !flag uses !flag which is correct. val result = not flag uses 'not' keyword which is invalid syntax. val result = flag.not uses flag.not which is invalid (missing ()). val result = ~flag uses ~ which is bitwise NOT, invalid for booleans.
  3. Final Answer:

    val result = !flag -> Option A
  4. Quick Check:

    Negation operator = ! [OK]
Quick Trick: Use ! before variable to negate boolean [OK]
Common Mistakes:
MISTAKES
  • Using ~ instead of !
  • Trying 'not' keyword
  • Confusing method call with operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes