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:
Step 1: Recall the negation operator in Kotlin
The logical NOT operator is ! placed before the boolean variable.
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.
Final Answer:
val result = !flag -> Option A
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
Master "Operators and Expressions" in Kotlin
9 interactive learning modes - each teaches the same concept differently