0
0
Kotlinprogramming~20 mins

Partition for splitting in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Partition Mastery
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 partition example?
Consider the following Kotlin code that uses the partition function to split a list of integers into even and odd numbers. What will be printed?
Kotlin
val numbers = listOf(1, 2, 3, 4, 5, 6)
val (evens, odds) = numbers.partition { it % 2 == 0 }
println("Evens: $evens")
println("Odds: $odds")
AEvens: [1, 3, 5]\nOdds: [2, 4, 6]
BEvens: [2, 4, 6]\nOdds: [1, 3, 5]
CEvens: [1, 2, 3]\nOdds: [4, 5, 6]
DEvens: []\nOdds: [1, 2, 3, 4, 5, 6]
Attempts:
2 left
💡 Hint
Remember, partition splits the list into two lists: one where the condition is true, and one where it is false.
Predict Output
intermediate
2:00remaining
What is the size of the lists after partition?
Given this Kotlin code, how many elements are in the first and second lists after partitioning by the condition it > 3?
Kotlin
val data = listOf(2, 3, 4, 5, 6)
val (greaterThanThree, notGreaterThanThree) = data.partition { it > 3 }
println("greaterThanThree size: ${greaterThanThree.size}")
println("notGreaterThanThree size: ${notGreaterThanThree.size}")
AgreaterThanThree size: 3\nnotGreaterThanThree size: 2
BgreaterThanThree size: 0\nnotGreaterThanThree size: 5
CgreaterThanThree size: 5\nnotGreaterThanThree size: 0
DgreaterThanThree size: 2\nnotGreaterThanThree size: 3
Attempts:
2 left
💡 Hint
Count how many numbers in the list are greater than 3.
Predict Output
advanced
2:00remaining
What is the output when partitioning a list of strings by length?
Look at this Kotlin code that partitions a list of strings by whether their length is greater than 3. What will be printed?
Kotlin
val words = listOf("cat", "house", "dog", "elephant")
val (longWords, shortWords) = words.partition { it.length > 3 }
println("Long words: $longWords")
println("Short words: $shortWords")
ALong words: []\nShort words: [cat, house, dog, elephant]
BLong words: [cat, dog]\nShort words: [house, elephant]
CLong words: [cat, house, dog]\nShort words: [elephant]
DLong words: [house, elephant]\nShort words: [cat, dog]
Attempts:
2 left
💡 Hint
Check the length of each word carefully.
Predict Output
advanced
2:00remaining
What error occurs when using partition incorrectly?
What error will this Kotlin code produce?
Kotlin
val numbers = listOf(1, 2, 3)
val (evens, odds) = numbers.partition { it % 2 == 0 }
println(evens[3])
ANoSuchElementException
BNullPointerException
CIndexOutOfBoundsException
DClassCastException
Attempts:
2 left
💡 Hint
Check the size of the evens list and the index accessed.
🧠 Conceptual
expert
2:00remaining
Which option correctly describes the behavior of Kotlin's partition function?
Choose the statement that best describes what Kotlin's partition function does when called on a collection.
AIt splits the collection into two lists: the first contains elements matching the predicate, the second contains elements not matching it.
BIt filters the collection and returns a single list with elements matching the predicate.
CIt removes all elements that match the predicate from the original collection.
DIt sorts the collection into ascending and descending order lists.
Attempts:
2 left
💡 Hint
Think about how partition divides elements based on a condition.