Recall & Review
beginner
What is a generic function in Kotlin?
A generic function is a function that can work with any data type. It uses a placeholder type, usually written as <T>, so you can use the same function for different types without rewriting it.
Click to reveal answer
beginner
How do you declare a generic function in Kotlin?
You declare a generic function by adding a type parameter in angle brackets before the function name, like this: fun <T> functionName(param: T) { ... }
Click to reveal answer
beginner
Why use generic functions instead of regular functions?
Generic functions let you write one function that works with many types, so you avoid repeating code and make your code more flexible and reusable.Click to reveal answer
beginner
Example: What does this Kotlin function do?
fun <T> printItem(item: T) { println(item) }This function prints any item you give it. The <T> means it can accept any type, like a number, text, or even a list.
Click to reveal answer
intermediate
Can a generic function have more than one type parameter? How?
Yes, you can declare multiple type parameters separated by commas, like fun example(a: T, b: U) { ... } to work with two different types.
Click to reveal answer
How do you start declaring a generic function in Kotlin?
✗ Incorrect
Generic functions start with a type parameter in angle brackets, like , before the function name.
What does the <T> in a generic function represent?
✗ Incorrect
Can a generic function accept different types each time it is called?
✗ Incorrect
Generic functions are designed to accept any type, making them flexible.
Which of these is a correct generic function declaration?
✗ Incorrect
The correct syntax places before the function name and specifies the parameter type as T.
How do you declare a generic function with two type parameters?
✗ Incorrect
Multiple type parameters are separated by commas inside the angle brackets before the function name.
Explain how to declare and use a generic function in Kotlin with an example.
Think about how <T> works and how you can pass different types to the function.
You got /4 concepts.
Why are generic functions useful? Describe a real-life situation where they help.
Imagine writing one tool that works for many jobs instead of many tools for each job.
You got /4 concepts.