Kotlin Flashcards

1
Q

What is an inline function?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When does an inline function work best?

A

For functions with lambda parameters, especially when the function body is small.

Smaller function bodies minimize overhead and improve performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is not recommended when using inline functions?

A

Using them for large functions that are called in many places.

This can lead to code duplication and increased program size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a const?

A

A value that is resolved at compile time

This means the value must be known at the time of compilation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where can const be used?

A

Inside a companion object or at the top-level

It can only be used with val declarations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What types can const be?

A

Primitive types like Int, Boolean, or String

Complex data types cannot be used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a performance benefit of using const?

A

It removes the overhead of accessing the variable at runtime, which improves performance

This leads to faster execution of code that uses const.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a limitation of const?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is reified?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What happens to generic type information after compilation?

A

After compilation, the generic type is replaced by its upper bound or Any

This is known as type erasure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True or False: The compiler retains generic type information at runtime without using reified.

A

False

Without reified, the compiler erases generic type information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Fill in the blank: The compiler erases the generic type, so its information is not available at _______.

A

runtime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the effect of using reified on generic types?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the internal keyword?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the open keyword do in Kotlin?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of the lateinit keyword?

A

It’s used when you want to initialize a variable later

This allows for variables that cannot be initialized at the time of declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What type of variable can the lateinit keyword be used with?

A

Only with ‘var’

‘val’ variables cannot use lateinit as they must be initialized at declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Can a variable declared with lateinit be a primitive type?

A

No

The lateinit modifier cannot be used with primitive types like Int or Double.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What must be true about a variable declared with lateinit?

A

The variable must be non-nullable

This ensures that the variable will eventually be initialized before use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is a lazy keyword?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How does using the lazy keyword improve performance?

A

Initialization happens only when needed.

This means resources are not wasted on initializing variables that may never be used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What type of variable must a lazy keyword work with?

A

val

Using val indicates that the variable is immutable once initialized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What happens to a lazy variable after it is initialized?

A

All future access will return the same value.

This ensures consistency in the value returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Does the lazy keyword ensure thread safety?

A

Yes, it ensures thread safety by default.

This means that multiple threads can safely access the variable without causing issues.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is multidex?

A

It is used to overcome the limit on a large number of methods by allowing code to be split into multiple .dex files

However, it can slow down the app startup time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is @JvmStatic in Kotlin?

A

It is used to make functions and properties inside a companion object static in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

How are functions and properties inside a companion object accessed in Java without @JvmStatic?

A

Without @JvmStatic, they are not truly static and can only be accessed through the companion object in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How does @JvmStatic affect function and property accessibility in Java?

A

With @JvmStatic, the compiler generates static functions and properties at the class level, making them directly accessible in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is @JvmField in Kotlin?

A

It is used to instruct the compiler not to generate getter and setter methods for variables, making them directly accessible in Java and eliminating the function overhead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is @JvmOverloads in Kotlin?

A

It is used to instruct the compiler to generate overloads for a function with default parameters, since Java does not support default parameter values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

When is the init block called in Kotlin?

A
  • First, the primary constructor is called.
  • Second, all init blocks are called in the order they are defined in the class.
  • Finally, the secondary constructor is called (if present).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What does == (double equals) do in Kotlin?

A

It checks if two objects have the same contents, similar to equals in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What does === (triple equals) do in Kotlin?

A

It checks if two objects refer to the same instance, similar to == in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is a Companion Object in Kotlin?

A

It is tied to a class and defines static members within the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

When is a Companion Object created?

A

It is created when the class is loaded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What is an extension function in Kotlin?

A

It is used to add a new function to a class without modifying the class’s source code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

How do extension functions work internally?

A

They are compiled as static methods and take an instance of the class as a parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What is noinline in Kotlin?

A

It is used to prevent a lambda parameter from being inlined in an inline function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What are the limitations of an inlined lambda in Kotlin?

A
  • An inlined lambda can’t be passed to another noinline function.
  • An inlined lambda can’t be stored in a variable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What is crossinline in Kotlin?

A

It is used to prevent a non-local return inside a lambda in an inline function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

What problem does crossinline solve?

A

A local return inside an inlined lambda can cause the calling function to exit early. crossinline ensures that the lambda cannot use non-local returns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

What is a higher-order function in Kotlin?

A

It is a function that takes a lambda as a parameter or returns a lambda as the result.

44
Q

What is infix in Kotlin?

A

It is used to define a function that is called in a more readable way without using dots and parentheses.

45
Q

What are the requirements for defining an infix function in Kotlin?

A
  • It is defined like an extension function.
  • It must have exactly one parameter.
  • The parameter cannot have a default value.
46
Q

What is an inner class in Kotlin?

A

It is used to define a nested class that can access all properties of the outer class, regardless of their visibilities.

47
Q

What is an inline class (value class) in Kotlin?

A

It is used to wrap a single value in a class without runtime overhead, providing type safety and improving performance by avoiding object allocations.

48
Q

What are the features of an inline class (value class) in Kotlin?

A
  • It must have exactly one primary constructor parameter.
  • It can’t inherit from another class but can implement interfaces.
  • It can declare properties, but they can’t have backing fields.
  • It can also declare functions.
49
Q

What is a sealed class in Kotlin?

A

It is a type of class that restricts the class hierarchy to a limited set of subclasses, which must be declared in the same file.

50
Q

What benefit does a sealed class provide in Kotlin?

A

It enables exhaustive checks in the when expression, ensuring all subclasses are handled, improving safety.

51
Q

What is Kotlin DSL?

A

It allows you to define configurations and tasks using Kotlin, providing a more readable and type-safe way to write Gradle tasks and UI components in Compose.

52
Q

How does Kotlin DSL compare to using Groovy in Gradle?

A
  • It is easier to read.
  • It may have a slower startup time because the code needs to be compiled to bytecode at build time.
53
Q

What’s the difference between an abstract class and an interface in Kotlin?

A
  • Abstract class can have abstract and concrete methods, while interface can have abstract and default methods.
  • Abstract class can have state with or without default values, but interface can only have state declarations.
  • Abstract class can have constructors, but interface can’t.
  • A class can extend only one abstract class but can implement multiple interfaces.
  • Abstract class is used when a class needs to share state with its subclasses, while interface is used to define behaviors that can be implemented by other classes.
54
Q

What’s the difference between overloading and overriding in Kotlin?

A
  • Overloading allows a class to define multiple methods with the same name but different parameters.
  • Overriding means that a subclass provides an implementation of a method from its superclass.
55
Q

What is polymorphism in Kotlin?

A

Polymorphism allows a method to behave differently based on the object calling it. For example, both Dog and Cat inherit from Animal, but they provide different implementations of the makeSound() method.

56
Q

What is inheritance in Kotlin?

A

Inheritance allows a class to inherit properties and methods from its superclass.

57
Q

What are generics in Kotlin?

A

Generics allow defining classes and functions that can work with multiple data types, helping write reusable code.

58
Q

How do generics improve Kotlin code?

A

They ensure type safety at compile time.

59
Q

What is synchronized in Kotlin?

A

It ensures only one thread can execute a section of code at a time, preventing race conditions and guaranteeing thread safety.

60
Q

What is volatile in Kotlin?

A

It ensures that a variable is always read from and written to the main memory, making the latest values visible to all threads.

61
Q

What is a limitation of volatile in Kotlin?

A

It doesn’t guarantee atomicity. For example, in the case of x = x + 1, Thread B could read the outdated value of x before Thread A writes the result back.

62
Q

What is concurrency in Kotlin?

A

Concurrency allows tasks to run during overlapping time periods, but tasks don’t necessarily run simultaneously. It focuses on efficient task management.

63
Q

What is parallelism in Kotlin?

A

Parallelism enables tasks to run simultaneously, focusing on speeding up execution.

64
Q

What is atomicity in Kotlin?

A

It ensures that when a variable is being updated, no other thread can interfere with the operation until it is complete.

65
Q

How does atomicity work in Kotlin?

A
  • x = 0 + 1 is not atomic.
  • AtomicInteger(0).incrementAndGet() is atomic, ensuring the update is completed without interference from other threads.
66
Q

What is a shallow copy in Kotlin?

A

A shallow copy creates a new object or collection but only copies the references to nested objects within it. Examples include DataClass.copy() and Collections.toList().

67
Q

What is a deep copy in Kotlin?

A

A deep copy creates a new object or collection and also copies all nested objects within it, ensuring that no references to the original nested objects remain.

68
Q

What is an anonymous class in Kotlin?

A

It is a class that has no name and is declared and instantiated at the same time.

69
Q

When is an anonymous class typically used in Kotlin?

A

It’s used when you want a one-time implementation, often for things like event listeners or callbacks.

70
Q

What is hashCode() in Kotlin?

A

It is used to generate an ID that helps hash-based collections, such as HashMap and HashSet, efficiently store and retrieve objects.

71
Q

What is the relationship between equals() and hashCode() in Kotlin?

A

If equals() returns true for two objects, their hashCode() values must be the same.

72
Q

What does static mean in Kotlin?

A

It indicates that a field or method belongs to a class itself rather than to any instances of the class, meaning it is shared across all instances of the class.

73
Q

What is reflection in Kotlin?

A

It is used to inspect and modify the structure of a class at runtime, such as accessing methods, fields, and metadata.

74
Q

What are the features of reflection in Kotlin?

A
  • It can inspect metadata such as the class name.
  • It allows dynamic object creation.
  • It can invoke methods and manipulate field values regardless of visibility.
75
Q

What is the disadvantage of using reflection in Kotlin?

A

It leads to slower performance and makes the code harder to debug.

76
Q

How do you create a custom annotation using KSP in Kotlin?

A
  • Create a submodule for the custom annotation.
  • Add the KSP plugin and dependency in the build.gradle file.
  • Define the custom annotation class.
  • Create an annotation processor to find all symbols annotated with the custom annotation and make them perform the desired actions.
  • Create an annotation processor provider to provide the processor.
  • Register the provider by creating a file named com.google.devtools.ksp.processing.XXX in yourmodule/src/main/resources/META-INF/services/, and the file content should be com.google.devtools.ksp.processing.XXX.
77
Q

What’s the difference between KAPT and KSP in Kotlin?

A
  • Both are used for annotation processing to generate code.
  • KAPT is Java-based, so it’s tied to JVM, while KSP is designed specifically for Kotlin.
  • KAPT is slower than KSP since it waits until all symbols are resolved.
78
Q

What is Dependency Injection?

A

It’s a design pattern where an object receives its dependencies from an external source instead of creating them itself.

79
Q

What are the benefits of Dependency Injection?

A
  • Simplifies dependency management
  • Improves testability
  • Reduces coupling between components
80
Q

How does Dependency Injection improve testability?

A

It allows dependencies to be easily replaced with mock implementations, making unit testing more effective.

81
Q

What is the Builder design pattern?

A

It’s used to separate the creation logic from the object itself, allowing complex objects to be constructed step by step, improving flexibility and reusability.

82
Q

How does the Builder pattern improve flexibility?

A

It allows complex objects to be built incrementally, making it easier to customize and reuse construction logic.

83
Q

Can you give some examples of the Builder pattern?

A
  • AlertDialog
  • Notification
  • Glide
84
Q

What is the Singleton design pattern?

A

It ensures that a class has only one instance, providing global access to it and reducing memory usage.

85
Q

What is the benefit of using the Singleton pattern?

A

It ensures that only one instance of a class exists, reducing resource consumption and providing a single point of access.

86
Q

Can you give some examples of the Singleton pattern?

A
  • Application
  • Room Database
87
Q

What is the Factory design pattern?

A

It provides an interface or method to create objects without directly calling their constructors, so clients don’t need to know how objects are created, making the code easier to maintain and extend.

88
Q

What is the benefit of using the Factory pattern?

A

It decouples object creation from usage, making the code more maintainable and extensible by hiding object creation logic.

89
Q

Can you give some examples of the Factory pattern?

A
  • ViewModelProviderFactory
  • Fragment instantiation
90
Q

What is the Observer design pattern?

A

It defines a one-to-many relationship, where data updates notify all its observers, decoupling the data from the observers.

91
Q

What is the benefit of using the Observer pattern?

A

It allows decoupling of the data source and its observers, making the code easier to maintain and adapt to changes.

92
Q

Can you give some examples of the Observer pattern?

A
  • LiveData
  • EventBus
93
Q

What is the Repository design pattern?

A

It provides a way to abstract data access and offer a clean interface without exposing data sources, allowing clients to focus on business logic without needing to know how data is fetched.

94
Q

What is the benefit of using the Repository pattern?

A

It separates data access logic from the rest of the application, providing a clean and unified interface for data operations and improving maintainability.

95
Q

Can you give an example of the Repository pattern?

A

Data Repository

96
Q

What is the Adapter design pattern?

A

It acts as a bridge between two incompatible objects, converting one into another to make them work together.

97
Q

What is the benefit of using the Adapter pattern?

A

It allows incompatible interfaces to work together without modifying the original objects, making the code more flexible and reusable.

98
Q

Can you give some examples of the Adapter pattern?

A
  • RecyclerView Adapter
  • ViewPager Adapter
99
Q

What is the Facade design pattern?

A

It provides a simplified interface to a complex system, reducing coupling and making the system easier to use.

100
Q

What is the benefit of using the Facade pattern?

A

It simplifies interactions with a complex system by providing a single, unified interface, improving usability and maintainability

101
Q

Can you give an example of the Facade pattern?

A

MediaPlayer

102
Q

What are Covariance and Contravariance used for in generics?

A

Both are used to define the inheritance relationship between generic types.

103
Q

What is Covariance?

A

Covariance retains the inheritance relationship. For example, if A is a subclass of B, the generic type T<a> is also a subclass of the generic type T<b>.</b></a>

104
Q

What is Contravariance?

A

Contravariance reverses the inheritance relationship. For example, if A is a subclass of B, the generic type T<b> is a subclass of the generic type T<a>.</a></b>

105
Q

What’s an Annotation Processor?

A

it’s a tool that runs during compilation can generate code, modify existing code, and perform another action based on annotations