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?
ABy using the keyword generic
BBy adding () after the function name
CBy adding <T> before the function name
DBy writing fun generic()
✗ 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?
AA placeholder for any type
BA specific type like Int
CA function name
DA variable
✗ Incorrect
is a placeholder that can represent any type when the function is used.
Can a generic function accept different types each time it is called?
ANo, it only accepts one type
BOnly if the types are strings
COnly if the types are numbers
DYes, it can accept any type
✗ Incorrect
Generic functions are designed to accept any type, making them flexible.
Which of these is a correct generic function declaration?
Afun <T> show(item: T) { println(item) }
Bfun show<T>(item: T) { println(item) }
Cfun show(item: T) { println(item) }
Dfun <T> show(item) { println(item) }
✗ 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?
Afun <T U> example(a: T, b: U) { }
Bfun <T, U> example(a: T, b: U) { }
Cfun <T; U> example(a: T, b: U) { }
Dfun example<T, U>(a: T, b: U) { }
✗ 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.
Practice
(1/5)
1. What is the purpose of declaring a generic function in Kotlin using <T> before the function name?
easy
A. To declare a function that returns a string only
B. To restrict the function to only work with integers
C. To make the function run faster
D. To allow the function to work with any type specified when called
Solution
Step 1: Understand generic type declaration
Declaring <T> before a function name means the function can accept any type as input.
Step 2: Purpose of generic functions
This makes the function reusable for different data types without rewriting it for each type.
Final Answer:
To allow the function to work with any type specified when called -> Option D
Quick Check:
Generic functions = reusable for any type [OK]
Hint: Generic <T> means any type can be used [OK]
Common Mistakes:
Thinking generic restricts to one type
Confusing generic with function overloading
Assuming generic improves speed automatically
2. Which of the following is the correct syntax to declare a generic function named printItem that takes a parameter of any type T in Kotlin?
easy
A. fun <T> printItem(item: T) { println(item) }
B. fun printItem<T>(item) { println(item) }
C. fun printItem(T item) { println(item) }
D. fun printItem(item: <T>) { println(item) }
Solution
Step 1: Identify correct generic syntax
In Kotlin, generic type parameters are declared before the function name using <T>.
Step 2: Check parameter declaration
The parameter must specify its type as T, so item: T is correct.
Final Answer:
fun <T> printItem(item: T) { println(item) } -> Option A
Quick Check:
Generic syntax = <T> before function name [OK]
Hint: Generic type <T> goes before function name [OK]
Common Mistakes:
Placing <T> after function name
Not specifying parameter type as T
Using parentheses incorrectly around <T>
3. What will be the output of the following Kotlin code?
fun <T> identity(value: T): T {
return value
}
fun main() {
println(identity(123))
println(identity("Hello"))
}
medium
A. 123
123
B. 123
Hello
C. Hello
Hello
D. Compilation error
Solution
Step 1: Understand the generic function behavior
The function identity returns the same value it receives, regardless of type.
Step 2: Trace the calls in main()
Calling identity(123) returns 123, and identity("Hello") returns "Hello".
Final Answer:
123
Hello -> Option B
Quick Check:
Generic function returns input value [OK]
Hint: Generic function returns input as is [OK]
Common Mistakes:
Expecting type conversion
Thinking output repeats first call
Assuming compilation error due to mixed types
4. Identify the error in this Kotlin generic function declaration:
fun printValue<T>(value: T) {
println(value)
}
medium
A. Generic type parameter <T> must be declared before the function name
B. Parameter type cannot be generic
C. Function must return a value
D. println cannot print generic types
Solution
Step 1: Check generic syntax rules
In Kotlin, generic type parameters must be declared before the function name, not after.
Step 2: Analyze the given code
The code declares printValue<T>, which is incorrect syntax.
Final Answer:
Generic type parameter <T> must be declared before the function name -> Option A
Quick Check:
Generic <T> goes before function name [OK]
Hint: Place <T> before function name, not after [OK]
Common Mistakes:
Putting <T> after function name
Assuming generic parameters go in parentheses
Thinking generic functions must return a value
5. You want to write a generic function swap that swaps two elements in a mutable list at given indices. Which of the following is the correct Kotlin function declaration and implementation?
hard
A. fun swap<T>(list: MutableList<T>, i: Int, j: Int) {
val temp = list[i]
list[i] = list[j]
list[j] = temp
}
B. fun <T> swap(list: List<T>, i: Int, j: Int) {
val temp = list[i]
list[i] = list[j]
list[j] = temp
}
C. fun <T> swap(list: MutableList<T>, i: Int, j: Int) {
val temp = list[i]
list[i] = list[j]
list[j] = temp
}
D. fun swap(list: MutableList<Any>, i: Int, j: Int) {
val temp = list[i]
list[i] = list[j]
list[j] = temp
}
Solution
Step 1: Correct generic syntax and parameter types
The generic type parameter <T> must be declared before the function name. The list must be mutable to allow swapping.
Step 2: Verify implementation correctness
The function swaps elements at indices i and j using a temporary variable, which is correct.
Final Answer:
fun <T> swap(list: MutableList<T>, i: Int, j: Int) { val temp = list[i]; list[i] = list[j]; list[j] = temp } -> Option C
Quick Check:
Generic swap needs <T> before name and MutableList<T> [OK]
Hint: Use <T> before function and MutableList<T> for mutability [OK]
Common Mistakes:
Putting <T> after function name
Using immutable List instead of MutableList
Using MutableList<Any> losing type safety
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.