0
0
Kotlinprogramming~20 mins

Set creation (setOf, mutableSetOf) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Set Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A3
B4
C2
DCompilation error
Attempts:
2 left
💡 Hint
Remember that sets do not allow duplicate elements.
Predict Output
intermediate
2: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)
ACompilation error: add() is not available
B4 is added to the set
CRuntime exception: UnsupportedOperationException
DThe set remains unchanged silently
Attempts:
2 left
💡 Hint
setOf creates an immutable set.
Predict Output
advanced
2: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)
A3
B1
CCompilation error
D2
Attempts:
2 left
💡 Hint
mutableSetOf creates a set you can change.
🧠 Conceptual
advanced
2:00remaining
Which statement about setOf and mutableSetOf is true?
Choose the correct statement about Kotlin's setOf and mutableSetOf functions.
AsetOf creates an immutable set, mutableSetOf creates a mutable set
BBoth setOf and mutableSetOf create mutable sets
CBoth setOf and mutableSetOf create immutable sets
DsetOf creates a mutable set, mutableSetOf creates an immutable set
Attempts:
2 left
💡 Hint
Think about whether you can add or remove elements after creation.
🔧 Debug
expert
3: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")
Aremove() requires a mutable list, not a set
Bremove() is not supported on immutable sets created by setOf
CThe element "b" does not exist in the set
DThe code has a syntax error
Attempts:
2 left
💡 Hint
Check if the set is mutable or immutable.