Kotlin Flashcards
What is an inline function?
It instructs the compiler to insert the body of the function wherever the function gets used, which helps reduce function overhead and speeds up execution.
Inline functions work best for small function bodies.
When does an inline function work best?
For functions with lambda parameters, especially when the function body is small.
Smaller function bodies minimize overhead and improve performance.
What is not recommended when using inline functions?
Using them for large functions that are called in many places.
This can lead to code duplication and increased program size.
What is a const?
A value that is resolved at compile time
This means the value must be known at the time of compilation.
Where can const be used?
Inside a companion object or at the top-level
It can only be used with val
declarations.
What types can const be?
Primitive types like Int, Boolean, or String
Complex data types cannot be used.
What is a performance benefit of using const?
It removes the overhead of accessing the variable at runtime, which improves performance
This leads to faster execution of code that uses const.
What is a limitation of const?
It cannot be used if the value is not static or if it is a complex data type
Static values are those that do not change during the execution of a program.
What is reified?
Reified is a keyword that tells the compiler to retain the type information and insert it where it’s used
This allows the generic type information to be available at runtime.
What happens to generic type information after compilation?
After compilation, the generic type is replaced by its upper bound or Any
This is known as type erasure.
True or False: The compiler retains generic type information at runtime without using reified.
False
Without reified, the compiler erases generic type information.
Fill in the blank: The compiler erases the generic type, so its information is not available at _______.
runtime
What is the effect of using reified on generic types?
It allows the type information to be retained and used at runtime
This is particularly useful in certain programming scenarios, such as with inline functions.
What is the internal keyword?
It is used to restrict the visibility of a function, class, variable to the same module
The internal keyword ensures that certain elements are not accessible from outside their defining module.
What does the open keyword do in Kotlin?
Makes a class extendable or a function overridable
In Kotlin, classes and functions are final by default, meaning they cannot be inherited or overridden unless explicitly marked with the open keyword.
What is the purpose of the lateinit keyword?
It’s used when you want to initialize a variable later
This allows for variables that cannot be initialized at the time of declaration.
What type of variable can the lateinit keyword be used with?
Only with ‘var’
‘val’ variables cannot use lateinit as they must be initialized at declaration.
Can a variable declared with lateinit be a primitive type?
No
The lateinit modifier cannot be used with primitive types like Int or Double.
What must be true about a variable declared with lateinit?
The variable must be non-nullable
This ensures that the variable will eventually be initialized before use.
What is a lazy keyword?
It’s used when you want a variable to be initialized only when it is first accessed instead of at the time of declaration.
This approach helps in optimizing performance by delaying initialization.
How does using the lazy keyword improve performance?
Initialization happens only when needed.
This means resources are not wasted on initializing variables that may never be used.
What type of variable must a lazy keyword work with?
val
Using val
indicates that the variable is immutable once initialized.
What happens to a lazy variable after it is initialized?
All future access will return the same value.
This ensures consistency in the value returned.
Does the lazy keyword ensure thread safety?
Yes, it ensures thread safety by default.
This means that multiple threads can safely access the variable without causing issues.