Recall & Review
beginner
What is the main purpose of generics in Kotlin?
Generics allow you to write flexible and reusable code by letting you work with different data types while keeping type safety.
Click to reveal answer
beginner
How do generics help prevent runtime errors?
Generics check types at compile time, so you catch type mismatches early and avoid errors when the program runs.
Click to reveal answer
intermediate
Explain type safety in the context of generics.
Type safety means the program knows exactly what type of data it is working with, so it prevents mixing incompatible types, reducing bugs.
Click to reveal answer
beginner
What happens if you try to add a wrong type to a generic collection in Kotlin?
The compiler will show an error and stop you from adding the wrong type, ensuring only the correct type is used.
Click to reveal answer
intermediate
Why is using generics better than using 'Any' type in Kotlin?
Using generics keeps the specific type information, so the compiler can check types and avoid mistakes, while 'Any' loses type details and risks errors.
Click to reveal answer
What does generics in Kotlin mainly help with?
✗ Incorrect
Generics ensure type safety by checking types during compilation, preventing type errors.
What error do generics help avoid?
✗ Incorrect
Generics catch type mismatches before running the program, avoiding runtime type errors.
If a Kotlin list is declared as List<String>, what happens if you try to add an Int?
✗ Incorrect
The compiler enforces the type and stops adding a wrong type like Int to List.
Why is type safety important in programming?
✗ Incorrect
Type safety prevents bugs by ensuring only compatible types are used together.
What is a downside of not using generics and using 'Any' type instead?
✗ Incorrect
Using 'Any' loses type information, so the compiler cannot check types, increasing error risk.
Explain in your own words why generics provide type safety in Kotlin.
Think about how the compiler helps catch mistakes before running the program.
You got /3 concepts.
Describe a real-life example where generics help avoid mistakes in a Kotlin program.
Imagine a box that only accepts one kind of item and rejects others.
You got /3 concepts.