Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q6 of 15
Kotlin - Collections Fundamentals
Identify the error in this Kotlin code snippet:
val items = listOf("x", "y")
if (items.size = 2) {
    println("Two items")
}
AMissing parentheses around condition
BUsing listOf instead of mutableListOf
CIncorrect println syntax
DUsing assignment (=) instead of comparison (==) in if condition
Step-by-Step Solution
Solution:
  1. Step 1: Check if condition syntax

    The condition uses = which is assignment, not comparison.
  2. Step 2: Correct comparison operator

    Use == to compare items.size to 2.
  3. Final Answer:

    Using assignment (=) instead of comparison (==) in if condition -> Option D
  4. Quick Check:

    Use == for comparisons, not = [OK]
Quick Trick: Use == for comparisons, not = [OK]
Common Mistakes:
MISTAKES
  • Using = instead of ==
  • Assuming listOf must be mutable
  • Missing braces in if statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes