0
0
Kotlinprogramming~20 mins

Generic constraints with where clause in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Constraints Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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)
A[2, 4, 6, 8, 10]
BCompilation error due to casting
C[6, 8, 10]
D[3, 4, 5]
Attempts:
2 left
💡 Hint
Consider how casting from Double to T works in Kotlin generics.
📝 Syntax
intermediate
1: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?
Afun <T> process(item: T : CharSequence, T : Comparable<T>) { println(item.length) }
Bfun <T where T : CharSequence, T : Comparable<T>> process(item: T) { println(item.length) }
Cfun <T> process(item: T) where T : CharSequence, T : Comparable<T> { println(item.length) }
Dfun <T : CharSequence, T : Comparable<T>> process(item: T) { println(item.length) }
Attempts:
2 left
💡 Hint
Remember the correct placement of the where clause in Kotlin generic declarations.
optimization
advanced
2: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() }
}
AReturn List<T> instead of Double to avoid sumOf
BChange return type to Double and keep code as is
CChange generic constraint to only Number and remove Comparable
DUse 'items.filter { it.toDouble() > threshold.toDouble() }.sumOf { it.toDouble() }' and keep constraints
Attempts:
2 left
💡 Hint
Think about how to compare generic numbers safely.
🔧 Debug
advanced
1: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)
ABecause 123 is Int and does not satisfy CharSequence constraint
BBecause Comparable<T> constraint is missing
CBecause where clause syntax is incorrect
DBecause item.length is not accessible for generic T
Attempts:
2 left
💡 Hint
Check the type of argument passed and the constraints on T.
🧠 Conceptual
expert
2: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?
ATo specify multiple constraints on a generic type when simple syntax is insufficient
BTo declare default values for generic types
CTo restrict generic types to only nullable types
DTo enable runtime type checking of generic parameters
Attempts:
2 left
💡 Hint
Think about why Kotlin allows where clauses beyond simple generic constraints.