Challenge - 5 Problems
Generic Constraints Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a generic function with where clause filtering
What is the output of the following Kotlin function when called with a list of integers [1, 2, 3, 4, 5]?
Kotlin
fun <T> filterAndDouble(items: List<T>): List<T> where T : Number, T : Comparable<T> { return items.filter { it.toDouble() > 2 }.map { (it.toDouble() * 2) as T } } val result = filterAndDouble(listOf(1, 2, 3, 4, 5)) println(result)
Attempts:
2 left
💡 Hint
Consider how casting from Double to T works in Kotlin generics.
✗ Incorrect
The cast '(it.toDouble() * 2) as T' is unsafe because T is a generic type constrained to Number and Comparable, but Kotlin cannot guarantee the cast from Double to T at runtime, causing a ClassCastException or compilation error.
📝 Syntax
intermediate1:30remaining
Identify the correct syntax for generic constraints with where clause
Which of the following Kotlin function declarations correctly uses a where clause to constrain generic types?
Attempts:
2 left
💡 Hint
Remember the correct placement of the where clause in Kotlin generic declarations.
✗ Incorrect
Option C correctly places the where clause after the generic parameter list to specify multiple constraints. Options A, B, and C have syntax errors or incorrect constraint placements.
❓ optimization
advanced2:30remaining
Optimize generic function with multiple constraints and filtering
Given this Kotlin function, which option optimizes it to avoid unnecessary casting and improve type safety?
Kotlin
fun <T> sumIfGreaterThan(items: List<T>, threshold: T): Double where T : Number, T : Comparable<T> { return items.filter { it > threshold }.sumOf { it.toDouble() } }
Attempts:
2 left
💡 Hint
Think about how to compare generic numbers safely.
✗ Incorrect
Comparing generic types with '>' requires Comparable, but comparing their Double values is safer and avoids issues with generic comparisons. Option D converts both to Double for comparison and summation, improving safety and correctness.
🔧 Debug
advanced1:30remaining
Debug the compilation error in generic function with where clause
Why does the following Kotlin function cause a compilation error?
Kotlin
fun <T> printLength(item: T) where T : CharSequence, T : Comparable<T> { println(item.length) } printLength(123)
Attempts:
2 left
💡 Hint
Check the type of argument passed and the constraints on T.
✗ Incorrect
The function requires T to be CharSequence and Comparable, but 123 is an Int which does not implement CharSequence, causing a compilation error.
🧠 Conceptual
expert2:00remaining
Understanding the purpose of where clause in Kotlin generics
What is the main purpose of using a where clause in Kotlin generic functions or classes?
Attempts:
2 left
💡 Hint
Think about why Kotlin allows where clauses beyond simple generic constraints.
✗ Incorrect
The where clause allows specifying multiple constraints on a generic type parameter, especially when the simple colon syntax cannot express all constraints clearly.