Challenge - 5 Problems
Set Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin code using setOf?
Consider the following Kotlin code snippet. What will be printed when it runs?
Kotlin
val fruits = setOf("apple", "banana", "apple", "orange") println(fruits.size)
Attempts:
2 left
💡 Hint
Remember that sets do not allow duplicate elements.
✗ Incorrect
The setOf function creates an immutable set that automatically removes duplicates. Since "apple" appears twice, the set size is 3.
❓ Predict Output
intermediate2:00remaining
What happens when you add an element to a setOf collection?
Look at this Kotlin code. What will happen when it runs?
Kotlin
val numbers = setOf(1, 2, 3) numbers.add(4)
Attempts:
2 left
💡 Hint
setOf creates an immutable set.
✗ Incorrect
The set created by setOf is immutable. Calling add() causes a runtime UnsupportedOperationException.
❓ Predict Output
advanced2:00remaining
What is the output of this Kotlin code using mutableSetOf?
Analyze this Kotlin code and determine what it prints.
Kotlin
val mutableSet = mutableSetOf("cat", "dog") mutableSet.add("bird") mutableSet.remove("dog") println(mutableSet.size)
Attempts:
2 left
💡 Hint
mutableSetOf creates a set you can change.
✗ Incorrect
Initially, the set has 2 elements. Adding "bird" makes 3, then removing "dog" leaves 2 elements.
🧠 Conceptual
advanced2:00remaining
Which statement about setOf and mutableSetOf is true?
Choose the correct statement about Kotlin's setOf and mutableSetOf functions.
Attempts:
2 left
💡 Hint
Think about whether you can add or remove elements after creation.
✗ Incorrect
setOf creates an immutable set that cannot be changed after creation. mutableSetOf creates a mutable set that can be changed.
🔧 Debug
expert3:00remaining
Why does this Kotlin code throw an exception?
This Kotlin code throws an exception at runtime. What is the cause?
Kotlin
val mySet = setOf("a", "b", "c") mySet.remove("b")
Attempts:
2 left
💡 Hint
Check if the set is mutable or immutable.
✗ Incorrect
setOf creates an immutable set. Calling remove() on it causes UnsupportedOperationException at runtime.